Spaces:
Build error
Build error
import pandas as pd | |
import PIL | |
from PIL import Image | |
from PIL import ImageDraw | |
import gradio as gr | |
import torch | |
import easyocr | |
torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/20-Books.jpg', '20-Books.jpg') | |
torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/21-Books.jpg', '21-Books.jpg') | |
torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/22-Magazines.jpg', '22-Magazines.jpg') | |
torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/23-Magazines.jpg', '23-Magazines.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/24-Card-Games.jpg', '24-Card-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/25-Games.jpg', '25-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/26-Books.jpg', '26-Books.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/27-Books.jpg', '27-Books.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/28-Books.jpg', '28-Books.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/27-Games.jpg', '27-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/28-Games.jpg', '28-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/29-Books-Science-Fiction.jpg', '29-Books-Science-Fiction.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/30-Manga-Books.jpg', '30-Manga-Books.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/31-Books.jpg', '31-Books.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/32-Books.jpg', '32-Books.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/33-Game-Night.jpg', '33-Game-Night.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/34-Games.jpg', '34-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/35-Favorite-Games.jpg', '35-Favorite-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/36-Game-Night.jpg', '36-Game-Night.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/38-Strategy-Games.jpg', '38-Strategy-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/39-Games.jpg', '39-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/40-Games.jpg', '40-Games.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/41-Game-Rules.jpg', '41-Game-Rules.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/42-Game-Rules.jpg', '42-Game-Rules.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/43-Games-Pieces.jpg', '43-Games-Pieces.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/44-Game-Pieces.jpg', '44-Game-Pieces.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/45-Choose-Your-Own-Adventure.jpg', '45-Choose-Your-Own-Adventure.jpg') | |
#torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/46-Choose-Your-Own-Adventure.jpg', '46-Choose-Your-Own-Adventure.jpg') | |
#torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png', 'english.png') | |
#torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/chinese.jpg', 'chinese.jpg') | |
#torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/japanese.jpg', 'japanese.jpg') | |
#torch.hub.download_url_to_file('https://i.imgur.com/mwQFd7G.jpeg', 'Hindi.jpeg') | |
#, | |
#['24-Card-Games.jpg',['en']], | |
#['25-Games.jpg',['en']], | |
#['26-Books.jpg',['en']], | |
#['27-Books.jpg',['en']], | |
#['28-Books.jpg',['en']], | |
#['27-Games.jpg',['en']], | |
#['28-Games.jpg',['en']]] | |
#['29-Books-Science-Fiction.jpg',['en']], | |
#['30-Manga-Books.jpg',['en']], | |
#['31-Books.jpg',['en']], | |
#['32-Books.jpg',['en']], | |
#['33-Game-Night.jpg',['en']], | |
#['34-Games.jpg',['en']] | |
#['35-Favorite-Games.jpg',['en']], | |
#['36-Game-Night.jpg',['en']], | |
#['38-Strategy-Games.jpg',['en']], | |
#['39-Games.jpg',['en']], | |
#['40-Games.jpg',['en']], | |
#['41-Game-Rules.jpg',['en']], | |
#['42-Game-Rules.jpg',['en']], | |
#['43-Games-Pieces.jpg',['en']], | |
#['44-Game-Pieces.jpg',['en']], | |
#['45-Choose-Your-Own-Adventure.jpg',['en']], | |
#['46-Choose-Your-Own-Adventure.jpg',['en']] | |
#] | |
# Comment | |
#['english.png',['en']], | |
#['chinese.jpg',['ch_sim', 'en']], | |
#['japanese.jpg',['ja', 'en']], | |
#['Hindi.jpeg',['hi', 'en']] | |
def draw_boxes(image, bounds, color='yellow', width=2): | |
draw = ImageDraw.Draw(image) | |
for bound in bounds: | |
p0, p1, p2, p3 = bound[0] | |
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width) | |
return image | |
def inference(img, lang): | |
reader = easyocr.Reader(lang) | |
bounds = reader.readtext(img.name) | |
im = PIL.Image.open(img.name) | |
draw_boxes(im, bounds) | |
im.save('result.jpg') | |
return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]] | |
title = 'Image To Optical Character Recognition' | |
description = 'Multilingual OCR which works conveniently on all devices in multiple languages.' | |
article = "<p style='text-align: center'></p>" | |
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}" | |
examples = [['20-Books.jpg',['en']],['21-Books.jpg',['en']],['22-Magazines.jpg',['en']],['23-Magazines.jpg',['en']]] | |
choices = [ | |
"ch_sim", | |
"ch_tra", | |
"de", | |
"en", | |
"es", | |
"ja", | |
"hi", | |
"ru" | |
] | |
gr.Interface( | |
inference, | |
[gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')], | |
[gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])], | |
title=title, | |
description=description, | |
article=article, | |
examples=examples, | |
css=css, | |
enable_queue=True | |
).launch(debug=True) |