DerrylNessie commited on
Commit
d289e37
1 Parent(s): 3348f92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -24,11 +24,23 @@ def midpoint(x1, y1, x2, y2):
24
  return (x_mid, y_mid)
25
 
26
  def draw_mask(img, bounds):
27
- draw = ImageDraw.Draw(img)
28
- for bound in bounds:
29
- p0, p1, p2, p3 = bound[0]
30
- draw.polygon((*p0, *p1, *p2, *p3, *p0), fill=1)
31
- return img
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def masking(img, mask):
34
  #img = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)
@@ -50,16 +62,16 @@ def inference(img, lang):
50
  img_array = np.array(im)
51
 
52
  #mask = PIL.Image.open(img.name)
53
- mask = Image.new("1", im.size, 0)
54
- draw_mask(mask, bounds)
55
  #
56
  #masking(img_array, mask)
57
- img_inpainted = cv.inpaint(img_array, mask, 7, cv.INPAINT_NS)
58
  #remove_text(im, mask, bounds)
59
  lang = ""
60
  #cv.imshow('dst', dst)
61
  im.save('result.jpg')
62
- mask.save('mask.png')
63
 
64
  #img = cv.imread('result.jpg')
65
  #mask = cv.imread('mask2.png',0)
@@ -71,7 +83,7 @@ def inference(img, lang):
71
  #img_inpainted = cv2.inpaint(im, mask, 7, cv2.INPAINT_TELEA)
72
  #cv2.imwrite('dst.png', dst)
73
  #cv.imwrite('result.jpg',dst)
74
- return ['result.jpg', 'mask.png', pd.DataFrame(bounds). iloc[: , 1:2]]
75
 
76
  title = 'EasyOCR'
77
  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.'
@@ -88,7 +100,7 @@ gr.Interface(
88
  inference,
89
  [gr.inputs.Image(type='file', label='Input'),gr.inputs.Dropdown(choices, type="value", default="korean", label='language')],
90
  [gr.outputs.Image(type='file', label='Output'),
91
- gr.outputs.Image(type='file', label='Output'),
92
  #gr.outputs.Image(type='file', label='Output'),
93
  gr.outputs.Dataframe()],
94
  title=title,
 
24
  return (x_mid, y_mid)
25
 
26
  def draw_mask(img, bounds):
27
+ mask = np.zeros(img.shape[:2], dtype="uint8")
28
+ for box in bounds:
29
+
30
+ x0, y0 = box[0]
31
+ x1, y1 = box[1]
32
+ x2, y2 = box[2]
33
+ x3, y3 = box[3]
34
+
35
+ x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
36
+ x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
37
+
38
+ thickness = int(math.sqrt((x2 - x1)**2 + (y2 - y1)**2))
39
+
40
+ cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255, thickness)
41
+ img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
42
+
43
+ return(img)
44
 
45
  def masking(img, mask):
46
  #img = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)
 
62
  img_array = np.array(im)
63
 
64
  #mask = PIL.Image.open(img.name)
65
+ #mask = Image.new("L", im.size, 0)
66
+ draw_mask(img_array, bounds)
67
  #
68
  #masking(img_array, mask)
69
+ #img_inpainted = cv.inpaint(img_array, mask, 7, cv.INPAINT_NS)
70
  #remove_text(im, mask, bounds)
71
  lang = ""
72
  #cv.imshow('dst', dst)
73
  im.save('result.jpg')
74
+ #mask.save('mask.png')
75
 
76
  #img = cv.imread('result.jpg')
77
  #mask = cv.imread('mask2.png',0)
 
83
  #img_inpainted = cv2.inpaint(im, mask, 7, cv2.INPAINT_TELEA)
84
  #cv2.imwrite('dst.png', dst)
85
  #cv.imwrite('result.jpg',dst)
86
+ return ['result.jpg', pd.DataFrame(bounds). iloc[: , 1:2]]
87
 
88
  title = 'EasyOCR'
89
  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.'
 
100
  inference,
101
  [gr.inputs.Image(type='file', label='Input'),gr.inputs.Dropdown(choices, type="value", default="korean", label='language')],
102
  [gr.outputs.Image(type='file', label='Output'),
103
+ #gr.outputs.Image(type='file', label='Output'),
104
  #gr.outputs.Image(type='file', label='Output'),
105
  gr.outputs.Dataframe()],
106
  title=title,