eoeooe commited on
Commit
c8e96af
·
verified ·
1 Parent(s): 108b630

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -27
app.py CHANGED
@@ -7,38 +7,26 @@ from PIL import Image
7
  from collections import defaultdict
8
  import re
9
 
10
- def ocr_with_boxes(image):
11
- # แปลงจาก PIL → numpy array แบบไม่ copy memory
12
  img = np.array(image, copy=False)
13
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
14
 
15
- # ---- Resize เพื่อให้ Tesseract อ่านตัวอักษรเล็ก ๆ ได้ดีขึ้น ----
16
- height, width = img.shape[:2]
17
- scale = 2 # ขยาย 2 เท่า
18
- img = cv2.resize(img, (width*scale, height*scale), interpolation=cv2.INTER_CUBIC)
19
-
20
- # ---- Preprocessing สำหรับ OCR ----
21
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
22
- gray = cv2.GaussianBlur(gray, (3,3), 0)
23
- th = cv2.adaptiveThreshold(
24
- gray, 255,
25
- cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
26
- cv2.THRESH_BINARY, 31, 9
27
- )
28
- # morphology ช่วยให้ตัวอักษรชัดขึ้น
29
- kernel = np.ones((1,1), np.uint8)
30
- proc = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
31
 
32
- # ---- OCR ภาษาไทย + อังกฤษ ----
33
  custom_config = r'-l tha+eng --oem 3 --psm 6'
34
- data = pytesseract.image_to_data(proc, config=custom_config, output_type=Output.DICT)
35
 
36
  totalBox = len(data['text'])
37
  lines = defaultdict(list)
38
 
39
  for i in range(totalBox):
40
- # ใช้ confidence > 30 เพื่อกรองตัวอักษรผิดพลาด
41
- if int(data['conf'][i]) > 30:
42
  text = data['text'][i].strip()
43
  if text != "":
44
  line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
@@ -47,14 +35,12 @@ def ocr_with_boxes(image):
47
  # วาดกรอบบน original image
48
  x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
49
  cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
50
- cv2.putText(img, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
51
- 0.7, (0, 0, 255), 2, cv2.LINE_AA)
52
 
53
  # รวมข้อความเป็นบรรทัด
54
  extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
55
  final_text = "\n".join(extracted_texts)
56
 
57
- # 🔧 แก้เว้นวรรคภาษาไทย
58
  final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
59
 
60
  # แปลงกลับเป็น PIL
@@ -65,11 +51,11 @@ def ocr_with_boxes(image):
65
 
66
  # ---- Gradio UI ----
67
  demo = gr.Interface(
68
- fn=ocr_with_boxes,
69
  inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
70
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
71
- title="OCR ไทย + อังกฤษ (Tesseract + Preprocess + Fix Spacing)",
72
- description="อัปโหลดภาพ ระบบจะ OCR ไทย+อังกฤษ วาดกรอบ และแก้เว้นวรรคภาษาไทยอัตโนมัติ พร้อมปรับ preprocessing ให้ตัวเลขและอังกฤษแม่นขึ้น"
73
  )
74
 
75
  if __name__ == "__main__":
 
7
  from collections import defaultdict
8
  import re
9
 
10
+ def fast_ocr_with_boxes(image):
11
+ # แปลงจาก PIL → numpy array แบบไม่ copy
12
  img = np.array(image, copy=False)
13
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
14
 
15
+ # ---- Preprocessing minimal สำหรับความเร็ว ----
 
 
 
 
 
16
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
17
+ # ใช้ simple threshold แทน adaptive
18
+ _, th = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
 
 
 
 
 
 
 
19
 
20
+ # ---- OCR ไทย + อังกฤษ + ตัวเลข ----
21
  custom_config = r'-l tha+eng --oem 3 --psm 6'
22
+ data = pytesseract.image_to_data(th, config=custom_config, output_type=Output.DICT)
23
 
24
  totalBox = len(data['text'])
25
  lines = defaultdict(list)
26
 
27
  for i in range(totalBox):
28
+ # ใช้ confidence > 0
29
+ if int(data['conf'][i]) > 0:
30
  text = data['text'][i].strip()
31
  if text != "":
32
  line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
 
35
  # วาดกรอบบน original image
36
  x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
37
  cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
 
 
38
 
39
  # รวมข้อความเป็นบรรทัด
40
  extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
41
  final_text = "\n".join(extracted_texts)
42
 
43
+ # แก้เว้นวรรคภาษาไทย
44
  final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
45
 
46
  # แปลงกลับเป็น PIL
 
51
 
52
  # ---- Gradio UI ----
53
  demo = gr.Interface(
54
+ fn=fast_ocr_with_boxes,
55
  inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
56
  outputs=[gr.Image(label="ผลลัพธ์พร้อมกรอบข้อความ"), gr.Textbox(label="ข้อความ OCR")],
57
+ title="Fast OCR ไทย + อังกฤษ (Tesseract + Minimal Preprocess)",
58
+ description="อัปโหลดภาพ ระบบจะ OCR ไทย+อังกฤษ วาดกรอบ และแก้เว้นวรรคภาษาไทยแบบเร็วสุด ด้วย preprocessing minimal"
59
  )
60
 
61
  if __name__ == "__main__":