hantech commited on
Commit
a434c81
β€’
1 Parent(s): 0226999

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import pandas as pd
 
 
2
  import PIL
3
  from PIL import Image
4
  from PIL import ImageDraw
@@ -21,7 +23,16 @@ config['predictor']['beamsearch'] = True
21
  config['device'] = 'cpu' # mps
22
 
23
  recognitor = Predictor(config)
24
-
 
 
 
 
 
 
 
 
 
25
  def draw_boxes(image, bounds, color='yellow', width=2):
26
  draw = ImageDraw.Draw(image)
27
  for bound in bounds:
@@ -29,7 +40,7 @@ def draw_boxes(image, bounds, color='yellow', width=2):
29
  draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
30
  return image
31
 
32
- def inference(filepath, lang):
33
  img = cv2.imread(filepath)
34
  width, height, _ = img.shape
35
  reader = easyocr.Reader(lang)
@@ -56,6 +67,9 @@ def inference(filepath, lang):
56
  cropped_image = Image.fromarray(cropped_image)
57
  out = recognitor.predict(cropped_image)
58
  print(out)
 
 
 
59
  new_bounds.append((bbox,text, out, prob))
60
  im = PIL.Image.open(filepath)
61
  draw_boxes(im, bounds)
@@ -72,7 +86,7 @@ choices = [
72
  ]
73
  gr.Interface(
74
  inference,
75
- [gr.inputs.Image(type='filepath', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['vi'], label='language')],
76
  [gr.outputs.Image(type='pil', label='Output'), gr.outputs.Dataframe(type='pandas', headers=['easyOCR','vietOCR', 'confidence'])],
77
  title=title,
78
  description=description,
 
1
  import pandas as pd
2
+ from transformers import pipeline
3
+
4
  import PIL
5
  from PIL import Image
6
  from PIL import ImageDraw
 
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)
38
  for bound in bounds:
 
40
  draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
41
  return image
42
 
43
+ def inference(filepath, lang, labels):
44
  img = cv2.imread(filepath)
45
  width, height, _ = img.shape
46
  reader = easyocr.Reader(lang)
 
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)
 
86
  ]
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,