Spaces:
Runtime error
Runtime error
import pandas as pd | |
import PIL | |
from PIL import Image | |
from PIL import ImageDraw | |
import gradio as gr | |
import torch | |
import easyocr | |
import omegaconf | |
from vietocr.model.transformerocr import VietOCR | |
from vietocr.model.vocab import Vocab | |
from vietocr.translate import translate, process_input | |
config = omegaconf.OmegaConf.load("vgg-seq2seq.yaml") | |
config = omegaconf.OmegaConf.to_container(config, resolve=True) | |
vocab = Vocab(config['vocab']) | |
model = VietOCR(len(vocab), | |
config['backbone'], | |
config['cnn'], | |
config['transformer'], | |
config['seq_modeling']) | |
model.load_state_dict(torch.load('train_old.pth', map_location=torch.device('cpu'))) | |
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/thai.jpg', 'thai.jpg') | |
torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/french.jpg', 'french.jpg') | |
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://github.com/JaidedAI/EasyOCR/raw/master/examples/korean.png', 'korean.png') | |
torch.hub.download_url_to_file('https://i.imgur.com/mwQFd7G.jpeg', 'Hindi.jpeg') | |
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(filepath, lang): | |
reader = easyocr.Reader(lang) | |
bounds = reader.readtext(filepath) | |
new_bounds=[] | |
for (bbox, text, prob) in bounds: | |
x1,y1 = bbox[0] | |
x2,y2 = bbox[1] | |
x3,y3 = bbox[2] | |
x4,y4 = bbox[3] | |
# crop the region of interest (ROI) | |
img = Image.open(filepath) | |
#img = img[y0:y1, x0:x1] | |
img = img.crop((x1, y1, x3, y3)) # crop the image | |
img = process_input(img, config['dataset']['image_height'], | |
config['dataset']['image_min_width'], config['dataset']['image_max_width']) | |
out = translate(img, model)[0].tolist() | |
out = vocab.decode(out) | |
new_bounds.append((bbox, out, prob)) | |
im = PIL.Image.open(filepath) | |
draw_boxes(im, bounds) | |
im.save('result.jpg') | |
return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]] | |
title = 'EasyOCR' | |
description = 'Gradio demo for EasyOCR. EasyOCR demo supports 80+ languages.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.' | |
article = "<p style='text-align: center'><a href='https://www.jaided.ai/easyocr/'>Ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc.</a> | <a href='https://github.com/JaidedAI/EasyOCR'>Github Repo</a></p>" | |
examples = [['english.png',['en']],['thai.jpg',['th']],['french.jpg',['fr', 'en']],['chinese.jpg',['ch_sim', 'en']],['japanese.jpg',['ja', 'en']],['korean.png',['ko', 'en']],['Hindi.jpeg',['hi', 'en']]] | |
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}" | |
choices = [ | |
"abq", | |
"ady", | |
"af", | |
"ang", | |
"ar", | |
"as", | |
"ava", | |
"az", | |
"be", | |
"bg", | |
"bh", | |
"bho", | |
"bn", | |
"bs", | |
"ch_sim", | |
"ch_tra", | |
"che", | |
"cs", | |
"cy", | |
"da", | |
"dar", | |
"de", | |
"en", | |
"es", | |
"et", | |
"fa", | |
"fr", | |
"ga", | |
"gom", | |
"hi", | |
"hr", | |
"hu", | |
"id", | |
"inh", | |
"is", | |
"it", | |
"ja", | |
"kbd", | |
"kn", | |
"ko", | |
"ku", | |
"la", | |
"lbe", | |
"lez", | |
"lt", | |
"lv", | |
"mah", | |
"mai", | |
"mi", | |
"mn", | |
"mr", | |
"ms", | |
"mt", | |
"ne", | |
"new", | |
"nl", | |
"no", | |
"oc", | |
"pi", | |
"pl", | |
"pt", | |
"ro", | |
"ru", | |
"rs_cyrillic", | |
"rs_latin", | |
"sck", | |
"sk", | |
"sl", | |
"sq", | |
"sv", | |
"sw", | |
"ta", | |
"tab", | |
"te", | |
"th", | |
"tjk", | |
"tl", | |
"tr", | |
"ug", | |
"uk", | |
"ur", | |
"uz", | |
"vi" | |
] | |
gr.Interface( | |
inference, | |
[gr.inputs.Image(type='filepath', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')], | |
[gr.outputs.Image(type='pil', label='Output'), gr.outputs.Dataframe(type='pandas', headers=['text', 'confidence'])], | |
title=title, | |
description=description, | |
article=article, | |
examples=examples, | |
css=css, | |
enable_queue=True | |
).launch(debug=True) |