-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_data_f1.py
138 lines (101 loc) · 4.43 KB
/
gen_data_f1.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
134
135
136
137
138
import numpy as np
import sys
sys.path.append('..')
import cv2
import cv2.aruco as aruco
import camera_data
import argparse
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--video', required=True, action='store', default='.', help="Video to analyze")
parser.add_argument('-o', '--out_video', required=True, action='store', default='.', help="output video")
parser.add_argument('-cd', '--camera_data', required=True, action='store', default='.', help="yaml file with camera calibration")
parser.add_argument('-l', '--log', required=True, action='store', default='log.txt', help="Visibility log")
parser.add_argument('-s', '--show', required=False, action='store', default='True', help="Visibility log")
return parser.parse_args()
def drawcenter(img, imgpts):
corner = tuple(map(int, imgpts[0].ravel()))
img = cv2.line(img, corner, tuple(map(int, imgpts[1].ravel())), (255,0,0), 5)
img = cv2.line(img, corner, tuple(map(int, imgpts[2].ravel())), (0,255,0), 5)
img = cv2.line(img, corner, tuple(map(int, imgpts[3].ravel())), (0,0,255), 5)
return img
def far_off(p):
for i in range(3):
if abs(p[i]) > 1000:
return True
return False
def process(file, camera_data_file, out_video, log_file, show):
markerlength = 10
camera_matrix, dist_coeffs = camera_data.get_data(camera_data_file)
aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_50)#Aruco marker size 4x4 (+ border)
parameters = aruco.DetectorParameters_create()
axis = np.float32([[0, 0, 0],[3,0,0], [0,3,0], [0,0,3]]).reshape(-1,3)
# Define the codec and create VideoWriter object
cap = cv2.VideoCapture(file)
# Define the codec and create VideoWriter object
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)
print(f'W = {width} and H = {height}')
fps = cap.get(cv2.CAP_PROP_FPS)
print(f'FPS {fps}')
codec = 'mp4v'
frametot = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)+ 0.5)
out = cv2.VideoWriter(out_video, cv2.VideoWriter_fourcc(*codec), fps, (width, height))
size = (width, height)
last_frameno = 0
font = cv2.FONT_HERSHEY_SIMPLEX
#start = 257300 - 10
#cap.set(cv2.CAP_PROP_POS_FRAMES, start)
seen = 0
rvec = np.array([0., 0., 0.])
tvec = np.array([0., 0., 0.])
touse= True
log = open(log_file, 'w+')
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
marked = frame.copy()#aruco.drawDetectedMarkers(color_image.copy(), corners)#, ids)#Fails without the '.copy()', the 'drawDetectedMarkers' draw contours of the tags in the image
marked = aruco.drawDetectedMarkers(marked, corners, ids, (0,255,0))
corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
points3d = []
crns = []
frameno = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
cv2.putText(marked,f'{frameno}/{frametot}',(8,25), font, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
out.write(marked)
if frameno % 100 == 0:
print(f'{frameno}/{frametot}')
#out.write(marked)
if show:
cv2.imshow('frame',marked)
key = cv2.waitKey(1)
if key == ord('q'):
break
if ids is None:
continue
n = 0
for index, id in enumerate(ids):
if id[0] in model_corners:
n += 1
if n > 0:
seen += 1
log.write(str(frameno) + '\n')
print('Seen {} of total {} frames'.format(seen, frametot)) #Seen 76589 of total 270944 frames
#Seen 23331 of total 206454 frames
# Release everything if job is finished
log.close()
cap.release()
cv2.destroyAllWindows()
def main():
args = get_args()
FILE = args.video
OUT_FILE = args.out_video
LOG_FILE = args.log
SHOW = False if args.show.lower() == 'false' else True
CAMERA_DATA = args.camera_data
process(FILE, CAMERA_DATA, OUT_FILE, LOG_FILE, SHOW)
if __name__ == '__main__':
main()