Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# PIL -> OpenCV
|
| 12 |
img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 13 |
-
|
| 14 |
-
# Preprocessing
|
| 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
|
| 19 |
processed_pil = Image.fromarray(cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB))
|
| 20 |
-
|
| 21 |
-
# ✅
|
| 22 |
-
doc =
|
|
|
|
|
|
|
| 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=
|
| 31 |
inputs=gr.Image(type="pil", label="อัปโหลดรูปภาพ"),
|
| 32 |
outputs=gr.Textbox(label="ข้อความที่ OCR ได้"),
|
| 33 |
-
title="OCR ภาษาไทย (Doctr
|
| 34 |
-
description="
|
| 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__":
|