Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import pytesseract
|
| 4 |
+
|
| 5 |
+
def crop_and_ocr(image, box):
|
| 6 |
+
if box is None:
|
| 7 |
+
return "กรุณาเลือกบริเวณที่ต้องการ OCR"
|
| 8 |
+
|
| 9 |
+
x, y, w, h = box
|
| 10 |
+
x2, y2 = x + w, y + h
|
| 11 |
+
|
| 12 |
+
# ตัดภาพเฉพาะส่วน
|
| 13 |
+
cropped = image.crop((x, y, x2, y2))
|
| 14 |
+
|
| 15 |
+
# แปลงเป็นขาวดำ
|
| 16 |
+
cropped = cropped.convert('L').point(lambda p: 0 if p < 160 else 255)
|
| 17 |
+
|
| 18 |
+
# OCR
|
| 19 |
+
text = pytesseract.image_to_string(cropped, lang='eng+tha', config='--psm 6')
|
| 20 |
+
return text.strip()
|
| 21 |
+
|
| 22 |
+
gr.Interface(
|
| 23 |
+
fn=crop_and_ocr,
|
| 24 |
+
inputs=[
|
| 25 |
+
gr.Image(type="pil", tool="select", label="อัปโหลดแล้วลากคลุมบริเวณที่ต้องการ OCR"),
|
| 26 |
+
gr.State()
|
| 27 |
+
],
|
| 28 |
+
outputs="textbox",
|
| 29 |
+
title="OCR บนภาพที่เลือก crop",
|
| 30 |
+
allow_flagging="never"
|
| 31 |
+
).launch()
|