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