Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -42,6 +42,9 @@ def get_length(p1, p2):
|
|
42 |
line_length = ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5
|
43 |
return line_length
|
44 |
|
|
|
|
|
|
|
45 |
def get_max_distance_point(cnt, tip):
|
46 |
max_distance = 0
|
47 |
max_point = None
|
@@ -68,9 +71,13 @@ def find_tail(points, tip):
|
|
68 |
|
69 |
return None
|
70 |
|
71 |
-
def draw_arrow(img_result, tip, tail):
|
72 |
-
# Draw arrow on the blank image
|
73 |
-
cv2.arrowedLine(img_result, tuple(tail), tuple(tip), (0, 255, 0),
|
|
|
|
|
|
|
|
|
74 |
|
75 |
def infer(image_in):
|
76 |
img = cv2.imread(image_in)
|
@@ -86,7 +93,7 @@ def infer(image_in):
|
|
86 |
hull = cv2.convexHull(approx, returnPoints=False)
|
87 |
sides = len(hull)
|
88 |
|
89 |
-
if
|
90 |
arrow_tip = find_tip(approx[:,0,:], hull.squeeze())
|
91 |
|
92 |
|
@@ -96,9 +103,11 @@ def infer(image_in):
|
|
96 |
arrow_tail = find_tail(approx[:,0,:], arrow_tip)
|
97 |
if arrow_tail :
|
98 |
cv2.circle(img, arrow_tail, 3, (255, 0, 0), cv2.FILLED)
|
99 |
-
|
|
|
|
|
100 |
# Draw arrow on the same blank image
|
101 |
-
draw_arrow(img_result, arrow_tip, arrow_tail)
|
102 |
|
103 |
|
104 |
|
|
|
42 |
line_length = ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5
|
43 |
return line_length
|
44 |
|
45 |
+
def get_angle(tip, tail):
|
46 |
+
return np.degrees(np.arctan2(tail[1] - tip[1], tail[0] - tip[0]))
|
47 |
+
|
48 |
def get_max_distance_point(cnt, tip):
|
49 |
max_distance = 0
|
50 |
max_point = None
|
|
|
71 |
|
72 |
return None
|
73 |
|
74 |
+
def draw_arrow(img_result, tip, tail, length, angle):
|
75 |
+
# Draw arrow on the blank image with inverted tip and tail
|
76 |
+
cv2.arrowedLine(img_result, tuple(tail), tuple(tip), (0, 255, 0), 3)
|
77 |
+
|
78 |
+
# Add length and angle as text next to the tip point
|
79 |
+
text = f"Length: {length:.2f}, Angle: {angle:.2f}"
|
80 |
+
cv2.putText(img_result, text, (tip[0] + 10, tip[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
|
81 |
|
82 |
def infer(image_in):
|
83 |
img = cv2.imread(image_in)
|
|
|
93 |
hull = cv2.convexHull(approx, returnPoints=False)
|
94 |
sides = len(hull)
|
95 |
|
96 |
+
if 8 >= sides > 3 and sides + 2 == len(approx):
|
97 |
arrow_tip = find_tip(approx[:,0,:], hull.squeeze())
|
98 |
|
99 |
|
|
|
103 |
arrow_tail = find_tail(approx[:,0,:], arrow_tip)
|
104 |
if arrow_tail :
|
105 |
cv2.circle(img, arrow_tail, 3, (255, 0, 0), cv2.FILLED)
|
106 |
+
# Calculate length and angle
|
107 |
+
arrow_length = get_length(arrow_tip, arrow_tail)
|
108 |
+
arrow_angle = get_angle(arrow_tip, arrow_tail)
|
109 |
# Draw arrow on the same blank image
|
110 |
+
draw_arrow(img_result, arrow_tip, arrow_tail, arrow_length, arrow_angle)
|
111 |
|
112 |
|
113 |
|