Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
import easyocr
|
| 6 |
+
|
| 7 |
+
# 📥 Let's download some sample images to play with!
|
| 8 |
+
def download_sample_images():
|
| 9 |
+
image_urls = {
|
| 10 |
+
'20-Books.jpg': 'https://raw.githubusercontent.com/AaronCWacker/Yggdrasil/master/images/20-Books.jpg',
|
| 11 |
+
'COVID.png': 'https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png',
|
| 12 |
+
'chinese.jpg': 'https://github.com/JaidedAI/EasyOCR/raw/master/examples/chinese.jpg',
|
| 13 |
+
'japanese.jpg': 'https://github.com/JaidedAI/EasyOCR/raw/master/examples/japanese.jpg',
|
| 14 |
+
'Hindi.jpeg': 'https://i.imgur.com/mwQFd7G.jpeg'
|
| 15 |
+
}
|
| 16 |
+
for filename, url in image_urls.items():
|
| 17 |
+
# 🛸 Beaming down image: {filename}
|
| 18 |
+
torch.hub.download_url_to_file(url, filename)
|
| 19 |
+
|
| 20 |
+
# 🖌️ Function to draw boxes around detected text (because we all love boxes)
|
| 21 |
+
def draw_boxes(image, bounds, color='yellow', width=2):
|
| 22 |
+
draw = ImageDraw.Draw(image)
|
| 23 |
+
for bound in bounds:
|
| 24 |
+
# 🧙♂️ Drawing magic rectangles
|
| 25 |
+
p0, p1, p2, p3 = bound[0]
|
| 26 |
+
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
|
| 27 |
+
return image
|
| 28 |
+
|
| 29 |
+
# 🔮 The core function that does the OCR wizardry
|
| 30 |
+
def inference(img, lang):
|
| 31 |
+
# 🕵️♂️ Reading the image, please hold...
|
| 32 |
+
reader = easyocr.Reader(lang)
|
| 33 |
+
bounds = reader.readtext(img.name)
|
| 34 |
+
im = Image.open(img.name)
|
| 35 |
+
draw_boxes(im, bounds)
|
| 36 |
+
im.save('result.jpg')
|
| 37 |
+
return ['result.jpg', pd.DataFrame(bounds).iloc[:, 1:]]
|
| 38 |
+
|
| 39 |
+
# 🚀 Time to set up the Gradio app!
|
| 40 |
+
def main():
|
| 41 |
+
title = '🖼️ Image to Multilingual OCR 👁️ with Gradio'
|
| 42 |
+
description = 'Multilingual OCR that works conveniently on all devices in multiple languages. 🌐'
|
| 43 |
+
|
| 44 |
+
examples = [
|
| 45 |
+
['20-Books.jpg', ['en']],
|
| 46 |
+
['COVID.png', ['en']],
|
| 47 |
+
['chinese.jpg', ['ch_sim', 'en']],
|
| 48 |
+
['japanese.jpg', ['ja', 'en']],
|
| 49 |
+
['Hindi.jpeg', ['hi', 'en']]
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
|
| 53 |
+
choices = ["ch_sim", "ch_tra", "de", "en", "es", "ja", "hi", "ru"]
|
| 54 |
+
|
| 55 |
+
with gr.Blocks(css=css) as demo:
|
| 56 |
+
gr.Markdown(f"# {title}\n\n{description}")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
img_input = gr.Image(type='file', label='📥 Input Image')
|
| 61 |
+
lang_input = gr.CheckboxGroup(choices, value=['en'], label='🗣️ Language(s)')
|
| 62 |
+
submit_btn = gr.Button("Start OCR 🕵️♂️")
|
| 63 |
+
with gr.Column():
|
| 64 |
+
img_output = gr.Image(type='file', label='📤 Output Image')
|
| 65 |
+
df_output = gr.Dataframe(headers=['Text', 'Confidence'])
|
| 66 |
+
|
| 67 |
+
gr.Examples(
|
| 68 |
+
examples=examples,
|
| 69 |
+
inputs=[img_input, lang_input],
|
| 70 |
+
outputs=[img_output, df_output],
|
| 71 |
+
examples_per_page=5,
|
| 72 |
+
cache_examples=False
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
submit_btn.click(fn=inference, inputs=[img_input, lang_input], outputs=[img_output, df_output])
|
| 76 |
+
|
| 77 |
+
demo.launch(debug=True)
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
download_sample_images()
|
| 81 |
+
main()
|