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 cv2 as cv | |
import math | |
import numpy as np | |
torch.hub.download_url_to_file('https://i.pinimg.com/originals/45/d0/30/45d03054e15f4be731781eecba7458a4.jpg', 'korean.jpg') | |
def midpoint(x1, y1, x2, y2): | |
x_mid = int((x1 + x2)/2) | |
y_mid = int((y1 + y2)/2) | |
return (x_mid, y_mid) | |
def draw_mask(img, bounds): | |
draw = ImageDraw.Draw(img) | |
for bound in bounds: | |
p0, p1, p2, p3 = bound[0] | |
draw.polygon((*p0, *p1, *p2, *p3, *p0), fill=255) | |
return img | |
def masking(img, mask): | |
img = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA) | |
return img | |
def inference(img, lang): | |
if lang == "english": | |
lang = ['en'] | |
elif lang == "chinese": | |
lang = ['ch_sim'] | |
elif lang == "korean": | |
lang = ['ko'] | |
else: | |
lang = ['ja'] | |
reader = easyocr.Reader(lang) | |
bounds = reader.readtext(img.name) | |
im = PIL.Image.open(img.name) | |
#mask = PIL.Image.open(img.name) | |
mask = Image.new("L", im.size, 0) | |
draw_mask(mask, bounds) | |
# | |
#masking(im, mask) | |
#img_inpainted = cv2.inpaint(im, mask, 7, cv2.INPAINT_NS) | |
#remove_text(im, mask, bounds) | |
lang = "" | |
cv2.imshow('dst', dst) | |
im.save('result.jpg') | |
mask.save('mask.png') | |
img = cv.imread('result.jpg') | |
mask = cv.imread('mask2.png',0) | |
dst = cv.inpaint(img,mask,3,cv.INPAINT_TELEA) | |
#final.save('final.png') | |
#img_inpainted.save('inpaint.jpg') | |
#dst = cv2.inpaint(np.float32(img),np.float32(mask),3,cv2.INPAINT_TELEA) | |
#img_inpainted = cv2.inpaint(im, mask, 7, cv2.INPAINT_TELEA) | |
#cv2.imwrite('dst.png', dst) | |
dst.save('result.jpg') | |
return ['result.jpg', 'mask.png', pd.DataFrame(bounds). iloc[: , 1:2]] | |
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>" | |
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}" | |
examples = [['korean.jpg',"korean"]] | |
choices = [ | |
"chinese", | |
"english", | |
"japanese", | |
"korean" | |
] | |
gr.Interface( | |
inference, | |
[gr.inputs.Image(type='file', label='Input'),gr.inputs.Dropdown(choices, type="value", default="korean", label='language')], | |
[gr.outputs.Image(type='file', label='Output'), | |
gr.outputs.Image(type='file', label='Output'), | |
#gr.outputs.Image(type='file', label='Output'), | |
gr.outputs.Dataframe()], | |
title=title, | |
description=description, | |
article=article, | |
examples=examples, | |
css=css, | |
enable_queue=True | |
).launch(debug=True) |