File size: 3,159 Bytes
5ce453d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff31c95
5ce453d
 
ff31c95
 
5ce453d
ff31c95
 
 
5ce453d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff31c95
5ce453d
 
 
ff31c95
5ce453d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pandas as pd
from PIL import Image, ImageDraw
import gradio as gr
import torch
import easyocr

# 📥 Let's download some sample images to play with!
def download_sample_images():
    image_urls = {
        '20-Books.jpg': 'https://raw.githubusercontent.com/AaronCWacker/Yggdrasil/master/images/20-Books.jpg',
        'COVID.png': 'https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png',
        'chinese.jpg': 'https://github.com/JaidedAI/EasyOCR/raw/master/examples/chinese.jpg',
        'japanese.jpg': 'https://github.com/JaidedAI/EasyOCR/raw/master/examples/japanese.jpg',
        'Hindi.jpeg': 'https://i.imgur.com/mwQFd7G.jpeg'
    }
    for filename, url in image_urls.items():
        # 🛸 Beaming down image: {filename}
        torch.hub.download_url_to_file(url, filename)

# 🖌️ Function to draw boxes around detected text (because we all love boxes)
def draw_boxes(image, bounds, color='yellow', width=2):
    draw = ImageDraw.Draw(image)
    for bound in bounds:
        # 🧙‍♂️ Drawing magic rectangles
        p0, p1, p2, p3 = bound[0]
        draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
    return image

# 🔮 The core function that does the OCR wizardry
def inference(img_path, lang):
    # 🕵️‍♂️ Reading the image, please hold...
    reader = easyocr.Reader(lang)
    bounds = reader.readtext(img_path)
    im = Image.open(img_path)
    draw_boxes(im, bounds)
    result_path = 'result.jpg'
    im.save(result_path)
    return [result_path, pd.DataFrame(bounds).iloc[:, 1:]]

# 🚀 Time to set up the Gradio app!
def main():
    title = '🖼️ Image to Multilingual OCR 👁️ with Gradio'
    description = 'Multilingual OCR that works conveniently on all devices in multiple languages. 🌐'

    examples = [
        ['20-Books.jpg', ['en']],
        ['COVID.png', ['en']],
        ['chinese.jpg', ['ch_sim', 'en']],
        ['japanese.jpg', ['ja', 'en']],
        ['Hindi.jpeg', ['hi', 'en']]
    ]

    css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
    choices = ["ch_sim", "ch_tra", "de", "en", "es", "ja", "hi", "ru"]

    with gr.Blocks(css=css) as demo:
        gr.Markdown(f"# {title}\n\n{description}")
        
        with gr.Row():
            with gr.Column():
                img_input = gr.Image(type='filepath', label='📥 Input Image')
                lang_input = gr.CheckboxGroup(choices, value=['en'], label='🗣️ Language(s)')
                submit_btn = gr.Button("Start OCR 🕵️‍♂️")
            with gr.Column():
                img_output = gr.Image(type='filepath', label='📤 Output Image')
                df_output = gr.Dataframe(headers=['Text', 'Confidence'])

        gr.Examples(
            examples=examples,
            inputs=[img_input, lang_input],
            outputs=[img_output, df_output],
            examples_per_page=5,
            cache_examples=False
        )

        submit_btn.click(fn=inference, inputs=[img_input, lang_input], outputs=[img_output, df_output])

    demo.launch(debug=True)

if __name__ == "__main__":
    download_sample_images()
    main()