eoeooe commited on
Commit
2f1d88a
·
verified ·
1 Parent(s): 928739a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -28
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 detect_bank(image):
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
- banks = {
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=detect_bank,
44
- inputs=gr.Image(type="pil", label="อัปโหลดภาพสลิปหรือใบเสร็จ"),
45
- outputs=gr.Textbox(lines=10, label="ผลลัพธ์"),
46
- title="🧾 ตรวจจับชื่อธนาคารจากภาพ OCR",
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
  # เปิดแอป