eoeooe commited on
Commit
2636f1e
·
verified ·
1 Parent(s): aeec1ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -21
app.py CHANGED
@@ -7,52 +7,73 @@ from PIL import Image
7
  from collections import defaultdict
8
  import re
9
 
10
- def ocr_with_boxes(image):
11
  # แปลงจาก PIL → OpenCV
12
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
 
14
- # config ไทย +ังฤษ
15
- custom_config = r'-l tha+eng --oem 1 --psm 8'
 
 
 
 
 
 
 
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
50
  demo = gr.Interface(
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__":
 
7
  from collections import defaultdict
8
  import re
9
 
10
+ def ocr_scb_slip(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)
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 สำหรับโลโก้ SCB ---
25
+ custom_config = r'-l eng --oem 3 --psm 7 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ'
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)
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
  # แปลงกลับเป็น PIL
65
  img_out = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
66
  img_pil = Image.fromarray(img_out)
67
+
68
  return img_pil, final_text
69
 
70
  # Gradio UI
71
  demo = gr.Interface(
72
+ fn=ocr_scb_slip,
73
+ inputs=gr.Image(type="pil", label="อัปโหลดสลิป SCB"),
74
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
75
+ title="OCR สลิป SCB (Tesseract + Logo Fix)",
76
+ description="OCR โลโก้ SCB แลข้อความนสลิป วาดกรอบมบรรทัด + margin รอบลโก้"
77
  )
78
 
79
  if __name__ == "__main__":