eoeooe commited on
Commit
c0cb8ff
·
verified ·
1 Parent(s): 8da2d7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -5,33 +5,46 @@ from PIL import Image
5
  from doctr.models import ocr_predictor
6
  from doctr.io import DocumentFile
7
 
 
8
  predictor = ocr_predictor(pretrained=True)
9
 
10
- def ocr_image_doctr_cv2(image):
 
 
 
 
 
 
 
 
 
 
 
11
  # PIL -> OpenCV
12
  img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
-
14
- # Preprocessing: grayscale + threshold
15
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
16
  _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
17
-
18
- # แปลงกลับเป็น PIL.Image
19
  processed_pil = Image.fromarray(cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB))
20
-
21
- # ✅ ส่งเป็น list ของ PIL.Image เสมอ
22
- doc = DocumentFile.from_images([processed_pil])
 
 
23
  result = predictor(doc)
24
-
25
- # ดึงข้อความทั้งหมด
26
  text = "\n".join([block.content for page in result.pages for block in page.blocks])
27
  return text.strip()
28
 
 
29
  demo = gr.Interface(
30
- fn=ocr_image_doctr_cv2,
31
  inputs=gr.Image(type="pil", label="อัปโหลดรูปภาพ"),
32
  outputs=gr.Textbox(label="ข้อความที่ OCR ได้"),
33
- title="OCR ภาษาไทย (Doctr + OpenCV)",
34
- description="อัปโหลดภาพแล้วระบบจะอ่านข้อความภาษาไทยออกมาโดยใช้ Doctr OCR พร้อม preprocessing ด้วย OpenCV"
35
  )
36
 
37
  if __name__ == "__main__":
 
5
  from doctr.models import ocr_predictor
6
  from doctr.io import DocumentFile
7
 
8
+ # โหลดโมเดล
9
  predictor = ocr_predictor(pretrained=True)
10
 
11
+ def to_docfile(img):
12
+ # กันพัง: รองรับทั้ง PIL, numpy, list
13
+ if isinstance(img, np.ndarray):
14
+ return DocumentFile.from_images([img])
15
+ elif isinstance(img, Image.Image):
16
+ return DocumentFile.from_images([img])
17
+ elif isinstance(img, list):
18
+ return DocumentFile.from_images(img)
19
+ else:
20
+ raise ValueError(f"Unsupported input type: {type(img)}")
21
+
22
+ def ocr_image_doctr(image):
23
  # PIL -> OpenCV
24
  img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
25
+
26
+ # Preprocessing
27
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
28
  _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
29
+
30
+ # แปลงกลับเป็น PIL
31
  processed_pil = Image.fromarray(cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB))
32
+
33
+ # ✅ ใช้ wrapper ป้องกัน error
34
+ doc = to_docfile(processed_pil)
35
+
36
+ # OCR
37
  result = predictor(doc)
 
 
38
  text = "\n".join([block.content for page in result.pages for block in page.blocks])
39
  return text.strip()
40
 
41
+ # Gradio
42
  demo = gr.Interface(
43
+ fn=ocr_image_doctr,
44
  inputs=gr.Image(type="pil", label="อัปโหลดรูปภาพ"),
45
  outputs=gr.Textbox(label="ข้อความที่ OCR ได้"),
46
+ title="OCR ภาษาไทย (Doctr)",
47
+ description="OCR ด้วย Doctr + Preprocessing"
48
  )
49
 
50
  if __name__ == "__main__":