Spaces:
Sleeping
Sleeping
File size: 1,539 Bytes
74548a8 84a6eea 74548a8 19f752d 74548a8 19f752d 74548a8 19f752d 74548a8 19f752d 74548a8 19f752d 84a6eea 19f752d 84a6eea 19f752d 74548a8 19f752d 74548a8 19f752d 74548a8 84a6eea 19f752d 84a6eea 19f752d 84a6eea 2e6613e 84a6eea 19f752d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import os
from doctr.io import DocumentFile
from doctr.models import ocr_predictor
import gradio as gr
# เปิดใช้ PyTorch GPU/CPU
os.environ['USE_TORCH'] = '1'
# โหลดโมเดล OCR ภาษาอังกฤษ (pretrained)
predictor = ocr_predictor(pretrained=True)
# Title/Description สำหรับ Gradio
title = "English OCR"
description = """Upload an image to extract English text from it!"""
# ฟังก์ชัน OCR
def ocr(img):
# บันทึกไฟล์ชั่วคราว
img.save("out.jpg")
doc = DocumentFile.from_images("out.jpg")
# ทำ OCR
output = predictor(doc)
# รวมผลลัพธ์เป็น string
res = ""
for page in output.pages:
for block in page.blocks:
for line in block.lines:
for word in line.words:
res += word.value + " "
res += "\n"
res += "\n"
# บันทึกผลเป็นไฟล์ txt
output_name = "RESULT_OCR.txt"
with open(output_name, "w", encoding="utf-8", errors="ignore") as f:
f.write(res)
return res, output_name
# สร้าง Gradio Interface
demo = gr.Interface(
fn=ocr,
inputs=[gr.Image(type="pil")], # รับภาพจากผู้ใช้
outputs=[
gr.Textbox(lines=20, label="Full Text"),
gr.File(label="Download OCR Results")
],
title=title,
description=description
)
# Launch
demo.launch(debug=True) |