eoeooe commited on
Commit
259b92e
·
verified ·
1 Parent(s): d44b6d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -33
app.py CHANGED
@@ -1,45 +1,59 @@
1
  import cv2
2
  import pytesseract
3
  from pytesseract import Output
4
- import gradio as gr
5
  import numpy as np
 
6
  from PIL import Image
 
 
7
 
8
- def preprocess_image(img):
9
- """Preprocessing เพื่อให้ OCR ไทย+อังกฤษ แม่นขึ้น"""
10
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
11
-
12
- # ลด noise
13
- gray = cv2.medianBlur(gray, 3)
14
 
15
- # Thresholding (adaptive) เพื่อให้ text ชัด
16
- thresh = cv2.adaptiveThreshold(
17
- gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
18
- cv2.THRESH_BINARY, 11, 2
19
- )
20
- return thresh
21
-
22
- def ocr_image(img_pil):
23
- # แปลง PIL -> OpenCV
24
- img = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)
25
 
26
- # Preprocessing
27
- processed_img = preprocess_image(img)
28
 
29
- # Config สำหรับไทย+อังกฤษ
30
- custom_config = r'-l tha+eng --oem 3 --psm 6 -c language_model_ngram_space_delimited_language=1'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # OCR
33
- text = pytesseract.image_to_string(processed_img, config=custom_config)
34
- return text
35
-
36
- # Gradio interface
37
- iface = gr.Interface(
38
- fn=ocr_image,
39
- inputs=gr.Image(type="pil"),
40
- outputs="text",
41
- title="OCR ไทย + อังกฤษ (Preprocessed)",
42
- description="อัปโหลดรูปเพื่ออ่านข้อความภาษาไทยและอังกฤษ พร้อมปรับภาพให้แม่นยำขึ้น"
43
  )
44
 
45
- iface.launch()
 
 
1
  import cv2
2
  import pytesseract
3
  from pytesseract import Output
 
4
  import numpy as np
5
+ import gradio as gr
6
  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 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
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__":
59
+ demo.launch()