Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import PIL
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from PIL import ImageDraw
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
import easyocr
|
| 8 |
+
|
| 9 |
+
def draw_boxes(image, bounds, color='red', width=2):
|
| 10 |
+
draw = ImageDraw.Draw(image)
|
| 11 |
+
for bound in bounds:
|
| 12 |
+
p0, p1, p2, p3 = bound[0]
|
| 13 |
+
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
|
| 14 |
+
return image
|
| 15 |
+
|
| 16 |
+
def inference(img, lang):
|
| 17 |
+
reader = easyocr.Reader(lang)
|
| 18 |
+
bounds = reader.readtext(img.name)
|
| 19 |
+
im = PIL.Image.open(img.name)
|
| 20 |
+
draw_boxes(im, bounds)
|
| 21 |
+
im.save('result.jpg')
|
| 22 |
+
return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]
|
| 23 |
+
|
| 24 |
+
gr.Interface(
|
| 25 |
+
inference,
|
| 26 |
+
[gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en', 'vi'], label='language')],
|
| 27 |
+
[gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
|
| 28 |
+
title=title,
|
| 29 |
+
description=description,
|
| 30 |
+
article=article,
|
| 31 |
+
examples=examples,
|
| 32 |
+
css=css,
|
| 33 |
+
enable_queue=True
|
| 34 |
+
).launch(debug=True)
|