WrittenRec / app.py
JustKiddo's picture
Update app.py
478de70 verified
raw
history blame
933 Bytes
import pandas as pd
import PIL
from PIL import Image
from PIL import ImageDraw
import gradio as gr
import torch
import easyocr
def draw_boxes(image, bounds, color='red', 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:]]
gr.Interface(
inference,
inputs=[gr.Image(type='pil', label='Input'),
gr.CheckboxGroup(choices, type="value", default=['vi'], label='language')],
outputs=[gr.Image(type='pil', label='Output'),
gr.Dataframe(headers=['text', 'confidence'])],
enable_queue=True
).launch(debug=True)