hantech commited on
Commit
4dfb513
β€’
1 Parent(s): e469ee5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
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
- classifier = pipeline("zero-shot-classification",
27
- model="NDugar/debertav3-mnli-snli-anli")
28
- def zero_shot(doc, candidates):
29
- given_labels = candidates.split(", ")
30
- dictionary = classifier(doc, given_labels)
31
- new_dict = dict (zip (dictionary['labels'], dictionary['scores']))
32
- max_label = max (new_dict, key=new_dict.get)
33
- max_score = max(dictionary['scores'])
34
- return max_label, max_score
 
 
 
 
 
 
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
- print(out)
70
- max_label, max_score = zero_shot(out, labels)
71
- print(max_label)
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', pd.DataFrame(new_bounds).iloc[: , 2:]]
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.Dataframe(type='pandas', headers=['easyOCR','vietOCR', 'confidence'])],
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,