Tifinagh-OCR / app.py
ayymen's picture
Update app.py
1abadf7
raw
history blame
1.35 kB
import os
from doctr.io import DocumentFile
from doctr.models import ocr_predictor, from_hub
import gradio as gr
os.environ['USE_TORCH'] = '1'
reco_model = from_hub('ayymen/crnn_mobilenet_v3_large_gen_hw')
predictor = ocr_predictor(reco_arch=reco_model, pretrained=True)
title = "Tifinagh OCR"
description = "Upload an image to get the OCR results !"
def ocr(img):
img.save("out.jpg")
doc = DocumentFile.from_images("out.jpg")
output = predictor(doc)
res = ""
for obj in output.pages:
for obj1 in obj.blocks:
for obj2 in obj1.lines:
for obj3 in obj2.words:
res=res + " " + obj3.value
res=res + "\n"
res=res + "\n\n"
_output_name = "RESULT_OCR.txt"
open(_output_name, 'w', encoding="utf-8").close() # clear file
with open(_output_name, "w", encoding="utf-8", errors="ignore") as f:
f.write(res)
print("Writing into file")
return res, _output_name
demo = gr.Interface(fn=ocr,
inputs=gr.Image(type="pil"),
outputs=["text", "file"],
title=title,
description=description,
examples=[["Examples/Book.png"],["Examples/News.png"],["Examples/Manuscript.jpg"],["Examples/Files.jpg"]]
)
demo.launch(debug=True)