Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import torch
|
|
9 |
import easyocr
|
10 |
import omegaconf
|
11 |
import cv2
|
|
|
12 |
|
13 |
from vietocr.vietocr.tool.predictor import Predictor
|
14 |
from vietocr.vietocr.tool.config import Cfg
|
@@ -23,15 +24,21 @@ config['predictor']['beamsearch'] = True
|
|
23 |
config['device'] = 'cpu' # mps
|
24 |
|
25 |
recognitor = Predictor(config)
|
26 |
-
|
27 |
-
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
def draw_boxes(image, bounds, color='yellow', width=2):
|
37 |
draw = ImageDraw.Draw(image)
|
@@ -46,6 +53,7 @@ def inference(filepath, lang, labels):
|
|
46 |
reader = easyocr.Reader(lang)
|
47 |
bounds = reader.readtext(filepath)
|
48 |
new_bounds=[]
|
|
|
49 |
for (bbox, text, prob) in bounds:
|
50 |
(tl, tr, br, bl) = bbox
|
51 |
tl = (int(tl[0]), int(tl[1]))
|
@@ -66,15 +74,13 @@ def inference(filepath, lang, labels):
|
|
66 |
cropped_image = img[min_y:max_y,min_x:max_x] # crop the image
|
67 |
cropped_image = Image.fromarray(cropped_image)
|
68 |
out = recognitor.predict(cropped_image)
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
print(max_score)
|
73 |
-
new_bounds.append((bbox,text, out, prob))
|
74 |
im = PIL.Image.open(filepath)
|
75 |
draw_boxes(im, bounds)
|
76 |
im.save('result.jpg')
|
77 |
-
return ['result.jpg',
|
78 |
|
79 |
title = 'EasyOCR'
|
80 |
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.'
|
@@ -87,7 +93,7 @@ choices = [
|
|
87 |
gr.Interface(
|
88 |
inference,
|
89 |
[gr.inputs.Image(type='filepath', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['vi'], label='language'), gr.inputs.Textbox(label='Labels')],
|
90 |
-
[gr.outputs.Image(type='pil', label='Output'), gr.outputs.
|
91 |
title=title,
|
92 |
description=description,
|
93 |
article=article,
|
|
|
9 |
import easyocr
|
10 |
import omegaconf
|
11 |
import cv2
|
12 |
+
import json
|
13 |
|
14 |
from vietocr.vietocr.tool.predictor import Predictor
|
15 |
from vietocr.vietocr.tool.config import Cfg
|
|
|
24 |
config['device'] = 'cpu' # mps
|
25 |
|
26 |
recognitor = Predictor(config)
|
27 |
+
model_name = "microsoft/xdoc-base-squad2.0"
|
28 |
+
nlp = pipeline('question-answering', model=model_name)
|
29 |
+
def query(doc, candidates):
|
30 |
+
questions = candidates.split(", ")
|
31 |
+
result={}
|
32 |
+
for question in questions:
|
33 |
+
QA_input = {
|
34 |
+
'question': question,
|
35 |
+
'context': doc
|
36 |
+
}
|
37 |
+
|
38 |
+
res= nlp(QA_input)
|
39 |
+
value = res['answer']
|
40 |
+
result[question]=value
|
41 |
+
return result
|
42 |
|
43 |
def draw_boxes(image, bounds, color='yellow', width=2):
|
44 |
draw = ImageDraw.Draw(image)
|
|
|
53 |
reader = easyocr.Reader(lang)
|
54 |
bounds = reader.readtext(filepath)
|
55 |
new_bounds=[]
|
56 |
+
text=''
|
57 |
for (bbox, text, prob) in bounds:
|
58 |
(tl, tr, br, bl) = bbox
|
59 |
tl = (int(tl[0]), int(tl[1]))
|
|
|
74 |
cropped_image = img[min_y:max_y,min_x:max_x] # crop the image
|
75 |
cropped_image = Image.fromarray(cropped_image)
|
76 |
out = recognitor.predict(cropped_image)
|
77 |
+
text = text + '\t' + out
|
78 |
+
result = query(text, labels)
|
79 |
+
jsonText = json.dumps(result)
|
|
|
|
|
80 |
im = PIL.Image.open(filepath)
|
81 |
draw_boxes(im, bounds)
|
82 |
im.save('result.jpg')
|
83 |
+
return ['result.jpg', jsonText]
|
84 |
|
85 |
title = 'EasyOCR'
|
86 |
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.'
|
|
|
93 |
gr.Interface(
|
94 |
inference,
|
95 |
[gr.inputs.Image(type='filepath', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['vi'], label='language'), gr.inputs.Textbox(label='Labels')],
|
96 |
+
[gr.outputs.Image(type='pil', label='Output'), gr.outputs.Textbox(label='Json')],
|
97 |
title=title,
|
98 |
description=description,
|
99 |
article=article,
|