eoeooe commited on
Commit
d03d800
·
verified ·
1 Parent(s): 400665c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -12,7 +12,7 @@ def ocr_with_boxes(image):
12
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
 
14
  # --- Preprocessing เพื่อให้ OCR อ่านโลโก้แม่นขึ้น ---
15
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # เป็น grayscale
16
  gray = cv2.adaptiveThreshold(
17
  gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
18
  cv2.THRESH_BINARY, 11, 2
@@ -22,40 +22,52 @@ def ocr_with_boxes(image):
22
  gray = cv2.erode(gray, kernel, iterations=1)
23
 
24
  # --- Tesseract config ---
25
- # oem 3 = LSTM neural net, psm 6 = assume a block of text
26
- # whitelist สำหรับตัวอักษรอังกฤษ + ตัวเลข
27
  custom_config = r'-l tha+eng --oem 3 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
28
 
29
  # OCR with bounding boxes
30
  data = pytesseract.image_to_data(gray, config=custom_config, output_type=Output.DICT)
31
-
32
  totalBox = len(data['text'])
33
  lines = defaultdict(list)
34
 
 
35
  for i in range(totalBox):
36
  if int(data['conf'][i]) > 0:
37
  text = data['text'][i].strip()
38
  if text != "":
39
  line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
40
- lines[line_id] += [text]
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- # วาดกรอบ
43
- x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
44
- cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
45
- cv2.putText(img, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
46
- 0.7, (0, 0, 255), 2, cv2.LINE_AA)
47
 
48
- # รวมข้อความให้เป็นบรรทัด
49
- extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
 
 
 
 
 
 
 
50
  final_text = "\n".join(extracted_texts)
51
 
52
- # 🔧 แก้เว้นวรรคภาษาไทย
53
  final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
54
 
55
  # แปลงกลับเป็น PIL
56
  img_out = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
57
  img_pil = Image.fromarray(img_out)
58
-
59
  return img_pil, final_text
60
 
61
  # Gradio UI
@@ -63,8 +75,8 @@ demo = gr.Interface(
63
  fn=ocr_with_boxes,
64
  inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
65
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
66
- title="OCR ไทย + อังกฤษ (Tesseract + Logo Fix)",
67
- description="อัปโหลดภาพ ระบบจะ OCR ไทย+อังกฤษ วาดกรอบ และแก้เว้นวรรคภาษาไทยอัตโนมัติ พร้อมปรับให้โลโก้ภาษาอังกฤษอ่านแม่นขึ้น"
68
  )
69
 
70
  if __name__ == "__main__":
 
12
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
 
14
  # --- Preprocessing เพื่อให้ OCR อ่านโลโก้แม่นขึ้น ---
15
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # grayscale
16
  gray = cv2.adaptiveThreshold(
17
  gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
18
  cv2.THRESH_BINARY, 11, 2
 
22
  gray = cv2.erode(gray, kernel, iterations=1)
23
 
24
  # --- Tesseract config ---
 
 
25
  custom_config = r'-l tha+eng --oem 3 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
26
 
27
  # OCR with bounding boxes
28
  data = pytesseract.image_to_data(gray, config=custom_config, output_type=Output.DICT)
 
29
  totalBox = len(data['text'])
30
  lines = defaultdict(list)
31
 
32
+ # จัดเรียงข้อความเป็นบรรทัด
33
  for i in range(totalBox):
34
  if int(data['conf'][i]) > 0:
35
  text = data['text'][i].strip()
36
  if text != "":
37
  line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
38
+ lines[line_id].append(i) # เก็บ index แทน text
39
+
40
+ # วาดกรอบรวมทุกคำในบรรทัด + margin
41
+ margin = 5 # เพิ่มให้กรอบไม่เล็กเกินไป
42
+ for line_id, indices in lines.items():
43
+ xs = [data['left'][i] for i in indices]
44
+ ys = [data['top'][i] for i in indices]
45
+ ws = [data['width'][i] for i in indices]
46
+ hs = [data['height'][i] for i in indices]
47
+
48
+ x, y = min(xs), min(ys)
49
+ w, h = max([xs[i]+ws[i] for i in range(len(xs))]) - x, max([ys[i]+hs[i] for i in range(len(ys))]) - y
50
 
51
+ cv2.rectangle(img, (x-margin, y-margin), (x + w+margin, y + h+margin), (0, 255, 0), 2)
 
 
 
 
52
 
53
+ # วางข้อความทั้งหมดในบรรทัดบนกรอบ
54
+ text_line = " ".join([data['text'][i] for i in indices])
55
+ cv2.putText(img, text_line, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
56
+ 0.7, (0, 0, 255), 2, cv2.LINE_AA)
57
+
58
+ # รวมข้อความทั้งหมด
59
+ extracted_texts = []
60
+ for indices in lines.values():
61
+ extracted_texts.append(" ".join([data['text'][i] for i in indices]))
62
  final_text = "\n".join(extracted_texts)
63
 
64
+ # แก้เว้นวรรคภาษาไทย
65
  final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
66
 
67
  # แปลงกลับเป็น PIL
68
  img_out = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
69
  img_pil = Image.fromarray(img_out)
70
+
71
  return img_pil, final_text
72
 
73
  # Gradio UI
 
75
  fn=ocr_with_boxes,
76
  inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
77
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
78
+ title="OCR ไทย + อังกฤษ (Tesseract + Line-based Bounding Box)",
79
+ description="OCR ไทย+อังกฤษ วาดกรอบรวมบรรทัด แก้เว้นวรรคไทย และครอบโลโก้ได้แม่นขึ้น"
80
  )
81
 
82
  if __name__ == "__main__":