Update app.py
Browse files1 unmodified line234567895678910111213131415161617171819192021221 unmodified linefrom PIL import Imageimport pytesseract
def ocr_image(image: Image.Image): # Perform OCR using pytesseract if image is None: return "No image uploaded." text = pytesseract.image_to_string(image, lang='tha+eng') # Thai + English OCRdef ocr_on_cropped(cropped_image: Image.Image): if cropped_image is None: return "กรุณาอัปโหลดและครอบรูปภาพก่อน" # ทำ OCR บนภาพที่ถูกครอบ text = pytesseract.image_to_string(cropped_image, lang="tha+eng") return text
with gr.Blocks() as demo: gr.Markdown("## 🖼️ OCR จากภาพ - อัปโหลดและครอบบริเวณที่ต้องการ") gr.Markdown("## 📷 OCR จากภาพ - อัปโหลดและครอบเฉพาะบริเวณที่ต้องการ")
with gr.Row(): image_input = gr.Image(type="pil", label="อัปโหลดแล้วครอบบริเวณที่ต้องการ OCR", interactive=True) text_output = gr.Textbox(label="ข้อความที่ตรวจพบจาก OCR") image_input = gr.Image(type="pil", interactive=True, label="🖼️ อัปโหลดแล้วครอบบริเวณที่ต้องการ") ocr_result = gr.Textbox(label="📄 ข้อความที่ตรวจพบ")
image_input.change(fn=ocr_image, inputs=image_input, outputs=text_output) image_input.change(fn=ocr_on_cropped, inputs=image_input, outputs=ocr_result)
demo.launch()app.py+10-9