File size: 2,013 Bytes
dbdbddf
 
1abadf7
dbdbddf
 
1abadf7
4a58ad9
 
 
 
1abadf7
dbdbddf
1abadf7
7667d72
 
8b21536
4a58ad9
dbdbddf
 
4a58ad9
1abadf7
dbdbddf
1abadf7
 
 
7667d72
c5380a5
1a4398d
fe10d73
1abadf7
fe10d73
 
 
 
dbdbddf
1abadf7
4a58ad9
 
 
 
1a4398d
4bd60c6
1a4398d
 
fe10d73
3d45b3a
4a58ad9
be03105
4a58ad9
9326455
4a58ad9
fe10d73
dbdbddf
1abadf7
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, from_hub
import gradio as gr

os.environ['USE_TORCH'] = '1'
reco_model_zgh = from_hub('ayymen/crnn_mobilenet_v3_large_zgh')
predictor_zgh = ocr_predictor(reco_arch=reco_model_zgh, pretrained=True)

reco_model = from_hub('ayymen/crnn_mobilenet_v3_large_tifinagh')
predictor = ocr_predictor(reco_arch=reco_model, pretrained=True)

title = "Tifinagh OCR"
description = """Upload an image to get the OCR results!
Thanks to @iseddik for the data!"""

def ocr(img, script):
    img.save("out.jpg")
    doc = DocumentFile.from_images("out.jpg")
    output = predictor_zgh(doc) if script == "Tifinagh-IRCAM" else 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"
    _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"),
                        gr.Dropdown(choices=['Tifinagh-IRCAM', 'Tifinagh'], label="Script", value="Tifinagh-IRCAM")
                    ],
                    outputs=[
                        gr.Textbox(lines=20, label="Full Text"),
                        gr.File(label="Download OCR Results")
                    ],
                    title=title,
                    description=description,
                    examples=[
                        ["Examples/3.jpg", "Tifinagh-IRCAM"],
                        ["Examples/2.jpg", "Tifinagh-IRCAM"],
                        ["Examples/1.jpg", "Tifinagh-IRCAM"]
                    ]
                    )

demo.launch(debug=True)