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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -41
app.py CHANGED
@@ -11,63 +11,39 @@ def ocr_with_boxes(image):
11
  # แปลงจาก PIL → OpenCV
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
19
- )
20
- kernel = np.ones((2, 2), np.uint8)
21
- gray = cv2.dilate(gray, kernel, iterations=1)
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,8 +51,8 @@ demo = gr.Interface(
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__":
 
11
  # แปลงจาก PIL → OpenCV
12
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
 
14
+ # config ไทย +ังฤษ
15
+ custom_config = r'-l tha+eng --oem 3 --psm 6'
16
+
 
 
 
 
 
 
 
 
 
 
17
  # OCR with bounding boxes
18
+ data = pytesseract.image_to_data(img, config=custom_config, output_type=Output.DICT)
19
+
20
  totalBox = len(data['text'])
21
  lines = defaultdict(list)
22
 
 
23
  for i in range(totalBox):
24
  if int(data['conf'][i]) > 0:
25
  text = data['text'][i].strip()
26
  if text != "":
27
  line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
28
+ lines[line_id] += [text]
 
 
 
 
 
 
 
 
29
 
30
+ # วาดกรอบ
31
+ x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
32
+ cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
33
+ cv2.putText(img, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
34
+ 0.7, (0, 0, 255), 2, cv2.LINE_AA)
35
 
36
+ # รวมข้อความให้เป็นบรรทัด
37
+ extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
 
 
 
 
 
 
 
 
 
38
  final_text = "\n".join(extracted_texts)
39
 
40
+ # 🔧 แก้เว้นวรรคภาษาไทยตรงนี้เลย
41
  final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
42
 
43
  # แปลงกลับเป็น PIL
44
  img_out = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
45
  img_pil = Image.fromarray(img_out)
46
+
47
  return img_pil, final_text
48
 
49
  # Gradio UI
 
51
  fn=ocr_with_boxes,
52
  inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
53
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
54
+ title="OCR ไทย + อังกฤษ (Tesseract + Fix Spacing)",
55
+ description="อัปโหลดภาพ ระบบจะ OCR ไทย+อังกฤษ วาดกรอบ และแก้เว้นวรรคภาษาไทยอัตโนมัติ"
56
  )
57
 
58
  if __name__ == "__main__":