Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
8 |
-
|
9 |
-
|
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,
|
29 |
|
30 |
for cnt in contours:
|
31 |
-
|
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
|
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
|
48 |
-
inputs
|
49 |
-
sources
|
50 |
-
type
|
51 |
),
|
52 |
-
outputs
|
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()
|