DerrylNessie commited on
Commit
0851882
1 Parent(s): 1b6ec5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -6,19 +6,38 @@ import gradio as gr
6
  import torch
7
  import easyocr
8
  import cv2
 
 
9
 
10
  torch.hub.download_url_to_file('https://i.pinimg.com/originals/45/d0/30/45d03054e15f4be731781eecba7458a4.jpg', 'korean.png')
 
 
 
 
11
 
12
- def draw_boxes(image, bounds, color='yellow', width=2):
13
- draw = ImageDraw.Draw(image)
14
- for bound in bounds:
15
- p0, p1, p2, p3 = bound[0]
16
- draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
17
- return image
18
- def remove_text(image, bounds):
19
  for bound in bounds:
20
  p0, p1, p2, p3 = bound[0]
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def inference(img, lang):
23
  if lang == "english":
24
  lang = ['en']
@@ -32,7 +51,7 @@ def inference(img, lang):
32
  bounds = reader.readtext(img.name)
33
  im = PIL.Image.open(img.name)
34
  draw_boxes(im, bounds)
35
- remove_text(im, bounds)
36
  lang = ""
37
  im.save('result.jpg')
38
  return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]
 
6
  import torch
7
  import easyocr
8
  import cv2
9
+ import math
10
+ import numpy as np
11
 
12
  torch.hub.download_url_to_file('https://i.pinimg.com/originals/45/d0/30/45d03054e15f4be731781eecba7458a4.jpg', 'korean.png')
13
+ def midpoint(x1, y1, x2, y2):
14
+ x_mid = int((x1 + x2)/2)
15
+ y_mid = int((y1 + y2)/2)
16
+ return (x_mid, y_mid)
17
 
18
+ def draw_boxes(img, bounds, color='yellow', width=2):
19
+ draw = ImageDraw.Draw(img)
 
 
 
 
 
20
  for bound in bounds:
21
  p0, p1, p2, p3 = bound[0]
22
 
23
+ #for masking the image
24
+ x0, y0 = p0[0][1]
25
+ x1, y1 = p1[0][1]
26
+ x2, y2 = p2[0][1]
27
+ x3, y3 = p3[0][1]
28
+ x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
29
+ x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
30
+ thickness = int(math.sqrt( (x2 - x1)**2 + (y2 - y1)**2))
31
+
32
+ mask = np.zeros(img.shape[:2], dtype="uint8")
33
+ cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255, thickness)
34
+ masked = cv2.bitwise_and(img, img, mask=mask)
35
+
36
+
37
+
38
+ #draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
39
+ return masked
40
+
41
  def inference(img, lang):
42
  if lang == "english":
43
  lang = ['en']
 
51
  bounds = reader.readtext(img.name)
52
  im = PIL.Image.open(img.name)
53
  draw_boxes(im, bounds)
54
+ #remove_text(im, bounds)
55
  lang = ""
56
  im.save('result.jpg')
57
  return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]