eoeooe commited on
Commit
f49cf7f
·
verified ·
1 Parent(s): 91d8e63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -8,8 +8,8 @@ from collections import defaultdict
8
  import re
9
 
10
  def ocr_with_boxes(image):
11
- # แปลง PIL → NumPy และกลับสี RGB→BGR แบบเร็ว
12
- img = np.array(image)[:, :, ::-1]
13
 
14
  # config ไทย + อังกฤษ
15
  custom_config = r'-l tha+eng --oem 1 --psm 6'
@@ -43,11 +43,11 @@ def ocr_with_boxes(image):
43
  cv2.putText(img, " ".join(lines[line_id]), (x_min, y_min-5),
44
  cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
45
 
46
- # รวมข้อความเป็นบรรทัด
47
  extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
48
  final_text = "\n".join(extracted_texts)
49
 
50
- # 🔧 แก้เว้นวรรคภาษาไทย
51
  final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
52
 
53
  # แปลงกลับเป็น PIL
@@ -61,7 +61,7 @@ demo = gr.Interface(
61
  fn=ocr_with_boxes,
62
  inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
63
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
64
- title="OCR ไทย + อังกฤษ (Tesseract + Faster)",
65
  description="OCR ไทย+อังกฤษ วาดกรอบบรรทัดและแก้เว้นวรรคภาษาไทยอัตโนมัติ"
66
  )
67
 
 
8
  import re
9
 
10
  def ocr_with_boxes(image):
11
+ # แปลง PIL → RGB → NumPy → BGR สำหรับ OpenCV
12
+ img = np.array(image.convert('RGB'))[:, :, ::-1].copy() # BGR uint8
13
 
14
  # config ไทย + อังกฤษ
15
  custom_config = r'-l tha+eng --oem 1 --psm 6'
 
43
  cv2.putText(img, " ".join(lines[line_id]), (x_min, y_min-5),
44
  cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
45
 
46
+ # รวมข้อความ
47
  extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
48
  final_text = "\n".join(extracted_texts)
49
 
50
+ # แก้เว้นวรรคภาษาไทย
51
  final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
52
 
53
  # แปลงกลับเป็น PIL
 
61
  fn=ocr_with_boxes,
62
  inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
63
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
64
+ title="OCR ไทย + อังกฤษ (Tesseract + Faster, Fixed OpenCV Error)",
65
  description="OCR ไทย+อังกฤษ วาดกรอบบรรทัดและแก้เว้นวรรคภาษาไทยอัตโนมัติ"
66
  )
67