Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from doctr.io import DocumentFile
|
| 3 |
+
from doctr.models import ocr_predictor
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
os.environ['USE_TORCH'] = '1'
|
| 7 |
+
|
| 8 |
+
# ใช้โมเดล OCR ภาษาอังกฤษที่มีใน Doctr
|
| 9 |
+
predictor = ocr_predictor(pretrained=True)
|
| 10 |
+
|
| 11 |
+
title = "English OCR"
|
| 12 |
+
description = """Upload an image to extract English text!"""
|
| 13 |
+
|
| 14 |
+
def ocr(img):
|
| 15 |
+
img.save("out.jpg")
|
| 16 |
+
doc = DocumentFile.from_images("out.jpg")
|
| 17 |
+
output = predictor(doc)
|
| 18 |
+
res = ""
|
| 19 |
+
for page in output.pages:
|
| 20 |
+
for block in page.blocks:
|
| 21 |
+
for line in block.lines:
|
| 22 |
+
for word in line.words:
|
| 23 |
+
res += word.value + " "
|
| 24 |
+
res += "\n"
|
| 25 |
+
res += "\n"
|
| 26 |
+
_output_name = "RESULT_OCR.txt"
|
| 27 |
+
with open(_output_name, "w", encoding="utf-8", errors="ignore") as f:
|
| 28 |
+
f.write(res)
|
| 29 |
+
return res, _output_name
|
| 30 |
+
|
| 31 |
+
demo = gr.Interface(
|
| 32 |
+
fn=ocr,
|
| 33 |
+
inputs=[gr.Image(type="pil")],
|
| 34 |
+
outputs=[
|
| 35 |
+
gr.Textbox(lines=20, label="Full Text"),
|
| 36 |
+
gr.File(label="Download OCR Results")
|
| 37 |
+
],
|
| 38 |
+
title=title,
|
| 39 |
+
description=description,
|
| 40 |
+
examples=[["Examples/english_sample.jpg"]]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.launch(debug=True)
|