Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,8 @@ from PIL import Image
|
|
| 6 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
| 7 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
def
|
| 11 |
# ตรวจสอบว่าเป็น RGB
|
| 12 |
if image.mode != "RGB":
|
| 13 |
image = image.convert("RGB")
|
|
@@ -15,36 +15,18 @@ def detect_bank(image):
|
|
| 15 |
# ทำ OCR ด้วย TrOCR
|
| 16 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 17 |
generated_ids = model.generate(pixel_values)
|
| 18 |
-
text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
"SCB": "ธนาคารไทยพาณิชย์ (SCB)",
|
| 23 |
-
"กสิกร": "ธนาคารกสิกรไทย",
|
| 24 |
-
"กรุงเทพ": "ธนาคารกรุงเทพ",
|
| 25 |
-
"กรุงไทย": "ธนาคารกรุงไทย",
|
| 26 |
-
"ออมสิน": "ธนาคารออมสิน",
|
| 27 |
-
"กรุงศรี": "ธนาคารกรุงศรีอยุธยา",
|
| 28 |
-
"tmb": "ธนาคารทหารไทยธนชาต (ttb)",
|
| 29 |
-
"ttb": "ธนาคารทหารไทยธนชาต (ttb)",
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
# ตรวจจับชื่อธนาคาร
|
| 33 |
-
detected = [name for key, name in banks.items() if key.lower() in text.lower()]
|
| 34 |
-
|
| 35 |
-
# ตอบกลับตามผลลัพธ์
|
| 36 |
-
if detected:
|
| 37 |
-
return f"✅ ตรวจพบธนาคาร: {detected[0]}"
|
| 38 |
-
else:
|
| 39 |
-
return f"⚠️ ไม่พบชื่อธนาคารในภาพ\n\n📄 ข้อความที่พบในภาพ:\n{text}"
|
| 40 |
|
| 41 |
# สร้าง Gradio Interface
|
| 42 |
iface = gr.Interface(
|
| 43 |
-
fn=
|
| 44 |
-
inputs=gr.Image(type="pil", label="
|
| 45 |
-
outputs=gr.Textbox(lines=10, label="
|
| 46 |
-
title="🧾
|
| 47 |
-
description="
|
| 48 |
)
|
| 49 |
|
| 50 |
# เปิดแอป
|
|
|
|
| 6 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
| 7 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
| 8 |
|
| 9 |
+
# ฟังก์ชันหลักสำหรับแสดงข้อความที่ตรวจจับได้จากภาพ
|
| 10 |
+
def ocr_text(image):
|
| 11 |
# ตรวจสอบว่าเป็น RGB
|
| 12 |
if image.mode != "RGB":
|
| 13 |
image = image.convert("RGB")
|
|
|
|
| 15 |
# ทำ OCR ด้วย TrOCR
|
| 16 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 17 |
generated_ids = model.generate(pixel_values)
|
| 18 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 19 |
|
| 20 |
+
# แสดงข้อความที่ตรวจจับได้
|
| 21 |
+
return f"📄 ข้อความที่พบในภาพ:\n{text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# สร้าง Gradio Interface
|
| 24 |
iface = gr.Interface(
|
| 25 |
+
fn=ocr_text,
|
| 26 |
+
inputs=gr.Image(type="pil", label="อัปโหลดภาพที่มีข้อความ"),
|
| 27 |
+
outputs=gr.Textbox(lines=10, label="ข้อความที่ตรวจจับได้"),
|
| 28 |
+
title="🧾 อ่านข้อความจากภาพ (OCR)",
|
| 29 |
+
description="ระบบจะอ่านข้อความจากภาพและแสดงผลลัพธ์ทั้งหมดโดยไม่กรอง"
|
| 30 |
)
|
| 31 |
|
| 32 |
# เปิดแอป
|