fffiloni commited on
Commit
68faeed
·
verified ·
1 Parent(s): 07938c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -42,7 +42,7 @@ 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_clockwise_angle(tip, tail):
46
  return (np.degrees(np.arctan2(tip[1] - tail[1], tip[0] - tail[0]))*-1)
47
 
48
  def get_max_distance_point(cnt, tip):
@@ -74,14 +74,14 @@ def find_tail(points, tip):
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_length = f"Length: {length:.2f}"
80
  text_angle = f"Angle: {angle:.2f}"
81
 
82
  cv2.putText(img_result, text_length, (tip[0] + 10, tip[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
83
  cv2.putText(img_result, text_angle, (tip[0] + 10, tip[1] - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
84
-
85
  def infer(image_in):
86
  img = cv2.imread(image_in)
87
 
@@ -90,13 +90,15 @@ def infer(image_in):
90
  # Create a blank image to accumulate arrows
91
  img_result = np.zeros_like(img)
92
 
 
 
93
  for cnt in contours:
94
  peri = cv2.arcLength(cnt, True)
95
  approx = cv2.approxPolyDP(cnt, 0.025 * peri, True)
96
  hull = cv2.convexHull(approx, returnPoints=False)
97
  sides = len(hull)
98
 
99
- if 8 >= sides > 3 and sides + 2 == len(approx):
100
  arrow_tip = find_tip(approx[:,0,:], hull.squeeze())
101
 
102
 
@@ -105,10 +107,11 @@ def infer(image_in):
105
  cv2.circle(img, arrow_tip, 3, (0, 0, 255), cv2.FILLED)
106
  arrow_tail = find_tail(approx[:,0,:], arrow_tip)
107
  if arrow_tail :
 
108
  cv2.circle(img, arrow_tail, 3, (255, 0, 0), cv2.FILLED)
109
  # Calculate length and angle
110
  arrow_length = get_length(arrow_tip, arrow_tail)
111
- arrow_angle = get_clockwise_angle(arrow_tip, arrow_tail)
112
  # Draw arrow on the same blank image
113
  draw_arrow(img_result, arrow_tip, arrow_tail, arrow_length, arrow_angle)
114
 
@@ -117,6 +120,8 @@ def infer(image_in):
117
  cv2.imwrite("Image_result.png", img)
118
  cv2.imwrite("Arrows_on_same_blank.png", img_result)
119
 
 
 
120
  return "Image_result.png", "Arrows_on_same_blank.png"
121
 
122
  gr.Interface(
 
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(tip[1] - tail[1], tip[0] - tail[0]))*-1)
47
 
48
  def get_max_distance_point(cnt, tip):
 
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_length = f"Length: {length:.2f}"
80
  text_angle = f"Angle: {angle:.2f}"
81
 
82
  cv2.putText(img_result, text_length, (tip[0] + 10, tip[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
83
  cv2.putText(img_result, text_angle, (tip[0] + 10, tip[1] - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
84
+ """
85
  def infer(image_in):
86
  img = cv2.imread(image_in)
87
 
 
90
  # Create a blank image to accumulate arrows
91
  img_result = np.zeros_like(img)
92
 
93
+ arrows_coordinates = []
94
+
95
  for cnt in contours:
96
  peri = cv2.arcLength(cnt, True)
97
  approx = cv2.approxPolyDP(cnt, 0.025 * peri, True)
98
  hull = cv2.convexHull(approx, returnPoints=False)
99
  sides = len(hull)
100
 
101
+ if 6 > sides > 3 and sides + 2 == len(approx):
102
  arrow_tip = find_tip(approx[:,0,:], hull.squeeze())
103
 
104
 
 
107
  cv2.circle(img, arrow_tip, 3, (0, 0, 255), cv2.FILLED)
108
  arrow_tail = find_tail(approx[:,0,:], arrow_tip)
109
  if arrow_tail :
110
+ arrows_coordinates.append([arrow_tail, arrow_tip])
111
  cv2.circle(img, arrow_tail, 3, (255, 0, 0), cv2.FILLED)
112
  # Calculate length and angle
113
  arrow_length = get_length(arrow_tip, arrow_tail)
114
+ arrow_angle = get_angle(arrow_tip, arrow_tail)
115
  # Draw arrow on the same blank image
116
  draw_arrow(img_result, arrow_tip, arrow_tail, arrow_length, arrow_angle)
117
 
 
120
  cv2.imwrite("Image_result.png", img)
121
  cv2.imwrite("Arrows_on_same_blank.png", img_result)
122
 
123
+ print(f"arrows coordinates: {arrows_coordinates}")
124
+
125
  return "Image_result.png", "Arrows_on_same_blank.png"
126
 
127
  gr.Interface(