fffiloni commited on
Commit
931bb21
1 Parent(s): af04d71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -4,11 +4,9 @@ import numpy as np
4
 
5
  def preprocess(img):
6
  img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
7
- img_blur = cv2.GaussianBlur(img_gray, (5, 5), 1)
8
- img_canny = cv2.Canny(img_blur, 50, 50)
9
- kernel = np.ones((3, 3))
10
- img_dilate = cv2.dilate(img_canny, kernel, iterations=2)
11
- img_erode = cv2.erode(img_dilate, kernel, iterations=1)
12
  return img_erode
13
 
14
  def find_tip(points, convex_hull):
@@ -25,16 +23,15 @@ def find_tip(points, convex_hull):
25
  def infer(image_in):
26
  img = cv2.imread(image_in)
27
 
28
- contours, hierarchy = cv2.findContours(preprocess(img), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
29
 
30
  for cnt in contours:
31
- peri = cv2.arcLength(cnt, True)
32
- approx = cv2.approxPolyDP(cnt, 0.025 * peri, True)
33
  hull = cv2.convexHull(approx, returnPoints=False)
34
  sides = len(hull)
35
 
36
  if 6 > sides > 3 and sides + 2 == len(approx):
37
- arrow_tip = find_tip(approx[:,0,:], hull.squeeze())
38
  if arrow_tip:
39
  cv2.drawContours(img, [cnt], -1, (0, 255, 0), 3)
40
  cv2.circle(img, arrow_tip, 3, (0, 0, 255), cv2.FILLED)
@@ -44,10 +41,10 @@ def infer(image_in):
44
  return "Image_result.png"
45
 
46
  gr.Interface(
47
- fn = infer,
48
- inputs = gr.Image(
49
- sources = ["upload"],
50
- type = "filepath"
51
  ),
52
- outputs = gr.Image()
53
- ).launch()
 
4
 
5
  def preprocess(img):
6
  img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
7
+ _, img_thresh = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
8
+ img_dilate = cv2.dilate(img_thresh, np.ones((3, 3)), iterations=2)
9
+ img_erode = cv2.erode(img_dilate, np.ones((3, 3)), iterations=1)
 
 
10
  return img_erode
11
 
12
  def find_tip(points, convex_hull):
 
23
  def infer(image_in):
24
  img = cv2.imread(image_in)
25
 
26
+ contours, _ = cv2.findContours(preprocess(img), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
27
 
28
  for cnt in contours:
29
+ approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)
 
30
  hull = cv2.convexHull(approx, returnPoints=False)
31
  sides = len(hull)
32
 
33
  if 6 > sides > 3 and sides + 2 == len(approx):
34
+ arrow_tip = find_tip(approx[:, 0, :], hull.squeeze())
35
  if arrow_tip:
36
  cv2.drawContours(img, [cnt], -1, (0, 255, 0), 3)
37
  cv2.circle(img, arrow_tip, 3, (0, 0, 255), cv2.FILLED)
 
41
  return "Image_result.png"
42
 
43
  gr.Interface(
44
+ fn=infer,
45
+ inputs=gr.Image(
46
+ sources=["upload"],
47
+ type="filepath"
48
  ),
49
+ outputs=gr.Image()
50
+ ).launch()