-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfootage_parser.py
More file actions
279 lines (250 loc) · 11.8 KB
/
footage_parser.py
File metadata and controls
279 lines (250 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from time import time
from typing import List
import cv2
import numpy as np
import pandas as pd
class FootageParser:
def __init__(self):
pass
def detect_corners(self, image):
# Convert the image to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define the range of blue color in HSV
lower_green = np.array([35, 50, 50])
upper_green = np.array([85, 255, 255])
# Create a mask for blue color
mask = cv2.inRange(hsv, lower_green, upper_green)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Store detected blue circles
green_circles = []
for contour in contours:
# Approximate the contour to a circle
approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)
# Check if the contour is circular (with some tolerance on the aspect ratio)
if len(approx) > 3:
(x, y), radius = cv2.minEnclosingCircle(contour)
if radius > 5: # Minimum radius for detection
green_circle = (int(x), int(y), int(radius))
green_circles.append(green_circle)
return green_circles # Return the rectangle as 4 points
def detect_wire(self, image):
# Convert the image to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define the range of red color in HSV
lower_yellow = np.array([0, 100, 100])
upper_yellow = np.array([30, 255, 255])
# Create a mask for red color
mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Loop through the contours and check for the largest circle
wire_end = None
for contour in contours:
# Approximate the contour to a circle
approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)
# Check if the contour is circular (with some tolerance on the aspect ratio)
if len(approx) > 3:
(x, y), radius = cv2.minEnclosingCircle(contour)
if radius > 5: # Minimum radius for detection
wire_end = (int(x), int(y), int(radius))
break
return wire_end
def map_point_to_normalized_space(self, rect_corners, point):
# Unpack rectangle corners
p1, p2, p3, p4 = rect_corners
x1, y1 = p1[0]
x2, y2 = p2[0]
x3, y3 = p3[0]
x4, y4 = p4[0]
px, py = point
# Define source and destination points
src = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
dst = np.array([[0, 1], [1, 1], [1, 0], [0, 0]])
# Corrected: Solve the transformation matrix
A = []
B = []
for (sx, sy), (dx, dy) in zip(src, dst):
A.append([sx, sy, 1, 0, 0, 0, -dx * sx, -dx * sy])
B.append(dx)
A.append([0, 0, 0, sx, sy, 1, -dy * sx, -dy * sy])
B.append(dy)
A = np.array(A)
B = np.array(B)
# Solve for h (8 parameters)
h = np.linalg.lstsq(A, B, rcond=None)[0]
# Append 1 to h to make it a 9-element vector and reshape to 3x3
H = np.append(h, 1).reshape(3, 3)
# Map the point using the homography matrix
mapped = np.dot(H, np.array([px, py, 1]))
u, v = mapped[0] / mapped[2], mapped[1] / mapped[2]
return u, v
def wire_screenspace_to_gridspace(self, corners, wire_end):
if len(corners) == 4 and wire_end:
points = np.array([[x,y] for x, y, _ in corners])
centroid = np.mean(points, axis=0)
angles = np.arctan2(points[:, 1] - centroid[1], points[:, 0] - centroid[0])
sorted_points = points[np.argsort(angles)]
sorted_points = sorted_points.reshape((-1, 1, 2))
wire_end_pos = wire_end[0], wire_end[1]
norm_pred_x, norm_pred_y = self.map_point_to_normalized_space(sorted_points, wire_end_pos)
pred_x = norm_pred_x
pred_y = 1 - norm_pred_y
return [pred_x, pred_y]
return None
def map_normalized_to_screen_space(self, rect_corners, point):
p1, p2, p3, p4 = rect_corners
x1, y1 = p1[0]
x2, y2 = p2[0]
x3, y3 = p3[0]
x4, y4 = p4[0]
src = np.array([[x1, y1],
[x2, y2],
[x3, y3],
[x4, y4]])
dst = np.array([[0, 1],
[1, 1],
[1, 0],
[0, 0]])
A = []
B = []
for (sx, sy), (dx, dy) in zip(src, dst):
A.append([sx, sy, 1, 0, 0, 0, -dx * sx, -dx * sy])
B.append(dx)
A.append([0, 0, 0, sx, sy, 1, -dy * sx, -dy * sy])
B.append(dy)
A = np.array(A)
B = np.array(B)
h = np.linalg.lstsq(A, B, rcond=None)[0]
H = np.append(h, 1).reshape(3, 3)
H_inv = np.linalg.inv(H)
u, v = point # point in normalized space
mapped = np.dot(H_inv, np.array([u, v, 1]))
x, y = mapped[0] / mapped[2], mapped[1] / mapped[2]
return x, y
def grid_space_to_screenspace(self, corners, grid_coord):
if len(corners) == 4 and grid_coord:
points = np.array([[x, y] for x, y, _ in corners])
centroid = np.mean(points, axis=0)
angles = np.arctan2(points[:, 1] - centroid[1], points[:, 0] - centroid[0])
sorted_points = points[np.argsort(angles)]
sorted_points = sorted_points.reshape((-1, 1, 2))
norm_coord = (grid_coord[0], 1 - grid_coord[1])
x, y = self.map_normalized_to_screen_space(sorted_points, norm_coord)
return [x, y]
return None
def get_capture_device(self):
for i in range(10):
try:
cap = cv2.VideoCapture(i)
if not cap.isOpened():
cap.release()
continue
print(f"Found capture device {i}")
return cap
except:
continue
return None
def parse_video(self, video_path, show = False, is_live = False, save_output = False, target = None):
if is_live:
cap = self.get_capture_device()
if cap is None:
raise Exception("Cap notfound")
else:
cap = cv2.VideoCapture(video_path)
frame_rate = cap.get(cv2.CAP_PROP_FPS)
total_time = 0
while True:
ret, frame = cap.read()
if not ret:
continue
if save_output:
cv2.imwrite(f"{video_path}/{time()}.png", frame)
wire_end = self.detect_wire(frame)
corners = self.detect_corners(frame)
pos = self.wire_screenspace_to_gridspace(corners, wire_end)
if pos:
yield pos, total_time
total_time += 1 / frame_rate
if show:
timer_text = f"{int(total_time // 60):02}:{int(total_time % 60):02}"
for corner in corners:
center_x, center_y, radius = corner
top_left = (center_x - radius, center_y - radius)
bottom_right = (center_x + radius, center_y + radius)
cv2.rectangle(frame, top_left, bottom_right, (255, 0, 255), 2)
if wire_end:
center_x, center_y, radius = wire_end
top_left = (center_x - radius, center_y - radius)
bottom_right = (center_x + radius, center_y + radius)
cv2.rectangle(frame, top_left, bottom_right, (0, 255, 0), 2) # Green square
if len(corners) == 4:
points = np.array([[x,y] for x, y, _ in corners])
centroid = np.mean(points, axis=0)
angles = np.arctan2(points[:, 1] - centroid[1], points[:, 0] - centroid[0])
sorted_points = points[np.argsort(angles)]
sorted_points = sorted_points.reshape((-1, 1, 2))
cv2.polylines(frame, [sorted_points], isClosed=True, color=(255, 255, 0), thickness=2)
cv2.putText(
frame, # Frame
f"TIME:{timer_text}", # Text to display
(10, 50), # Position (x, y)
cv2.FONT_HERSHEY_SIMPLEX, # Font
1, # Font scale
(0, 0, 255), # Color (BGR - green)
2, # Thickness
cv2.LINE_AA # Line type
)
if pos:
cv2.putText(
frame, # Frame
f"POS:{float(round(pos[0], 2)), float(round(pos[1], 2))}",
(10, 100), # Position (x, y)
cv2.FONT_HERSHEY_SIMPLEX, # Font
1, # Font scale
(0, 0, 255), # Color (BGR - green)
2, # Thickness
cv2.LINE_AA # Line type
)
if target:
target_screenspace = self.grid_space_to_screenspace(corners, target)
if target_screenspace:
target_screenspace = [int(v) for v in target_screenspace]
radius = 10
color = (0, 0, 255)
thickness = -1
cv2.circle(frame, target_screenspace, radius, color, thickness)
cv2.putText(
frame, # Frame
f"TARGET:{float(round(target[0], 2)), float(round(target[1], 2))}",
(10, 150), # Position (x, y)
cv2.FONT_HERSHEY_SIMPLEX, # Font
1, # Font scale
(0, 0, 255), # Color (BGR - green)
2, # Thickness
cv2.LINE_AA # Line type
)
if pos:
distance = np.linalg.norm(np.array(target)-np.array(pos))
cv2.putText(
frame, # Frame
f"DELTA:{float(round(distance, 4))}",
(10, 200), # Position (x, y)
cv2.FONT_HERSHEY_SIMPLEX, # Font
1, # Font scale
(0, 0, 255), # Color (BGR - green)
2, # Thickness
cv2.LINE_AA # Line type
)
cv2.imshow('Footage', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
fp = FootageParser()
target = [0, 0]
for pos, t in fp.parse_video("frames", True, True, False, target):
print(t, pos)
target[0] = np.sin(t)
target[1] = np.cos(t)