-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_marker.py
133 lines (112 loc) · 4.42 KB
/
video_marker.py
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
# Identify each markers
import numpy as np
import cv2 as cv
import hash
import marker
cap = cv.VideoCapture('data/slow.mp4')
# if not cap.isOpened():
# print("Cannot open camera")
# exit()
section_num = 1
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
width_section = width / section_num
height_section = height / section_num
fps = cap.get(cv.CAP_PROP_FPS)
print(f"width: {width}, height: {height}, fps: {fps}")
fourcc = cv.VideoWriter_fourcc(*'mp4v')
out = cv.VideoWriter('output/video_marker_240.mp4', fourcc, fps, (int(width), int(height)))
save_video = False
visual_video = True
def visualize_marker(markers):
dot_size = 4
circle_type = cv.FILLED
for node in markers['1']:
red = (0, 0, 255)
cv.circle(frame_copy, node, dot_size, red, circle_type)
for subgraph in markers['2']:
green = (0, 255, 0)
cv.circle(frame_copy, subgraph['c'], dot_size, green, circle_type)
for subgraph in markers['3']:
blue = (255, 0, 0)
cv.circle(frame_copy, subgraph['c'], dot_size, blue, circle_type)
for subgraph in markers['4']:
yellow = (0, 255, 255)
cv.circle(frame_copy, subgraph['c'], dot_size, yellow, circle_type)
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
frame_copy = frame.copy()
# Display the resulting frame
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 110, 255, cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(thresh, cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE)
# with_contours = cv.drawContours(frame, contours, -1, (0, 255, 0), 1)
hashMap = hash.HashMap(width_section, height_section)
# Draw 10 * 10 Section
for s in range(1, section_num):
cv.line(frame, (int(width_section * s), 0), (int(width_section * s), height), (0, 255, 255), 1, 1)
cv.line(frame, (0, int(height_section * s)), (width, int(height_section * s)), (0, 255, 255), 1, 1)
for i in contours:
M = cv.moments(i)
if M['m00'] != 0 and cv.contourArea(i) < 100:
cX = int(M['m10'] / M['m00'])
cY = int(M['m01'] / M['m00'])
hashMap.insert((cX, cY))
# cv.circle(frame, (cX, cY), 2, (0, 0, 255), -1)
# cv.drawContours(frame, [i], 0, (0, 0, 255), 1)
# Visualize hashed points
visual_grid = False
if visual_grid:
for sec in hashMap.grid:
for point in hashMap.getPointsFromKey(sec):
color = (0, int(800 * (sec[0] % 2) / section_num), int(800 * (sec[1] % 2) / section_num))
cv.circle(frame_copy, point, 4, color, -1)
cv.imshow("grid", frame_copy)
# Find dot cluster section by section
visual_marker_edge = True
markers = {'1': [], '2': [], '3': [], '4': []}
for key, points in hashMap.grid.items():
edges = []
nodes = []
n = len(points)
for i in range(n-1):
for j in range(i+1, n):
dst = (points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2
if dst < 300:
nodes.append(points[i])
nodes.append(points[j])
edge = [points[i], points[j]]
if visual_marker_edge:
cv.line(frame, edge[0], edge[1], (255, 0, 0), 2, 1)
edges.append(edge)
# Identify markers
node_sets = set(nodes) # Remove duplicates
nodes = list(node_sets)
markers['1'] = list(set(points) - node_sets) # Saving single points which are not constructing edges
subtrees = marker.findSubgraphsInBFS(nodes, edges)
for i, subtree in enumerate(subtrees):
if len(subtree['n']) == 2:
markers['2'].append(subtree)
elif len(subtree['n']) == 3:
markers['3'].append(subtree)
elif len(subtree['n']) == 4:
markers['4'].append(subtree)
else:
continue
visualize_marker(markers)
# Show keypoints
if save_video:
out.write(frame_copy)
if visual_video:
cv.imshow("frame", frame_copy)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
out.release()
cv.destroyAllWindows()