-
Notifications
You must be signed in to change notification settings - Fork 0
/
guirecord.py
369 lines (279 loc) · 10.3 KB
/
guirecord.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import numpy as np
import cv2
import argparse
import os, sys, time
from modules.myutils import uniqueFn
from modules.vidwriter import VidWriter
import modules.GlobalsB as Globals
from modules.GuiB import GuiB
from modules.GraphicsCV import resize_img
from modules.filelog import TimeLog
from modules.filelog import MetaDataLog
'''
DATA EXCHANGE
Cam I/O <-> VideoRecord <-> GUI I/O <-> (meta-data DB) & FileSystem
admin gui cmds:
quit
record/stop
time_to_record
filename
show video
Cam settings
framesize
Transforms on frames
(different than transforms on compressed frames and vid)
Gain
ISO
what was set away from default?
indv. video data:
timing of each frame
avg FPS
numFrames
recordTime (ms)
text notes
'''
ap = argparse.ArgumentParser()
ap.add_argument("--funkyread", action="store_true", default=False)
ap.add_argument("--sleepschedule", action="store_true", default=False)
args = vars(ap.parse_args())
Globals.init()
Globals.gui_cmd_quit = False
Globals.gui_cmd_record = False
Globals.gui_cmd_reset = False
Globals.gui_unique_fn = "------N/A------"
Globals.gui_dir_path = "/"
Globals.sw_record_start = False
Globals.sw_record_stop = False
Globals.sw_gui_dir = False
Globals.gui_b_jumpcut = False
Globals.b_jumpcut_inprogress = False
Globals.sw_preview_frame = False
Globals.gui_b_preview_frame = True
Globals.sw_camera_reset = False
Globals.gui_frame_size_enum = 0
Globals.gui_cam_num = 0
Globals.gui_b_resize = True
Globals.gui_codec_enum = 0
Globals.gui_b_buffer = False
Globals.gui_log_enum = 0
try:
gui = GuiB(b_log=True)
except Exception as e:
print 'failed to start guiB'
print e
sys.exit()
while(not(Globals.gui_cmd_quit)):
Globals.sw_camera_reset = False
init_savedir = "data/usr/nov_calib_1/"
# init_savedir = "data/usr/nov_throw_2/"
ext = "avi"
time_to_record = 999
b_show = True
reset_cntr_exit = 10
list_frames = []
reset_cntr = 0
sw_outofmem_break = False
local_sw_record_start = False # only way to tell first frame being recorded
b_experimental_read = args["funkyread"]
b_sleep_schedule = args["sleepschedule"]
if Globals.gui_log_enum == 0:
#simple
b_timelog = True
b_log_vars = False
if Globals.gui_log_enum == 1:
#detailed
b_timelog = True
b_log_vars = True
if Globals.gui_log_enum == 2:
#none
b_timelog = False
b_log_vars = False
timelog = TimeLog(inert = not(b_timelog)
,b_log_vars = b_log_vars
,b_sleep_schedule = b_sleep_schedule
)
if Globals.gui_frame_size_enum == 0:
frame_size = (640,480)
if Globals.gui_frame_size_enum == 1:
frame_size = (1280,720)
if Globals.gui_frame_size_enum == 2:
frame_size = (1920,1080)
if Globals.gui_codec_enum == 0:
fourcc = cv2.VideoWriter_fourcc("X","2","6","4")
if Globals.gui_codec_enum == 1:
fourcc = -1
if Globals.gui_codec_enum == 2:
fourcc = 0
cam = cv2.VideoCapture(Globals.gui_cam_num)
try:
cam.set(3,frame_size[0])
cam.set(4,frame_size[1])
except:
print 'couldnt set cam with frame_size: ', str(frame_size)
metalog = MetaDataLog()
metalog.set(
cam_num = Globals.gui_cam_num
,frame_size = frame_size
,fourcc_enum = fourcc
,b_buffer = Globals.gui_b_buffer
)
gui.myGui.set_sv_dir(init_savedir) #set directory to gui ang global
gui.myGui.get_sv_dir()
fn = uniqueFn( fn_base = "output"
,fn_dir = Globals.gui_dir_path
,fn_ext = ext
)
gui.myGui.set_sv_fn(fn)
gui.myGui.get_sv_fn()
t0 = time.time()
reset_cntr += 1
while(cam.isOpened()):
try:
timelog.log_start()
if not(b_experimental_read):
ret,frame = cam.read()
else:
for _ in range(2):
ret = cam.grab()
ret, frame = cam.retrieve()
timelog.log_time(
Globals.gui_cmd_record
,Globals.gui_b_preview_frame
,Globals.gui_frame_size_enum
,local_sw_record_start
)
if not(ret):
print '~~~~~~~~~~~~~~~~~~no ret from camera grab \n'
assert False
else:
if Globals.sw_preview_frame:
Globals.sw_preview_frame = False
if b_show:
b_show = False
cv2.destroyAllWindows()
else:
b_show = True
if b_show:
if Globals.gui_b_resize:
img_display = frame.copy()
img_display = resize_img(frame, True)
cv2.imshow('img_display',img_display)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
cv2.imshow('unresized_frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if Globals.gui_cmd_record:
local_sw_record_start = False
if Globals.sw_record_start:
Globals.sw_record_start = False
local_sw_record_start = True
if Globals.gui_b_jumpcut:
if Globals.b_jumpcut_inprogres:
continue
if not(Globals.gui_b_jumpcut):
Globals.b_jumpcut_inprogres = False
try:
out.release()
except:
pass
i = 0
fn = uniqueFn( fn_base = "output"
,fn_dir = Globals.gui_dir_path
,fn_ext = ext
)
if Globals.gui_unique_fn != fn:
fn = Globals.gui_unique_fn
out = VidWriter(
savefn = Globals.gui_dir_path + fn
,fourcc = fourcc
,outshape = frame_size
)
timelog.set_output(Globals.gui_dir_path + fn)
metalog.set_output_path_fn(Globals.gui_dir_path + fn)
metalog.set_notes()
if Globals.gui_b_jumpcut:
Globals.b_jumpcut_inprogres = True
continue
if Globals.gui_b_buffer:
list_frames.append(frame.copy())
else:
out.write(frame)
if Globals.sw_record_stop:
Globals.sw_record_stop = False
if Globals.gui_b_jumpcut:
continue
timelog.output_log()
metalog.output_log()
if Globals.gui_b_buffer:
for _frame in list_frames:
out.write(_frame)
list_frames = []
if sw_outofmem_break:
#you broke from outofmem: continue writing another file
sw_outofmem_break = False
Globals.sw_record_start = True
else:
#you broke by hitting record button: pause on gui and dont save
gui.myGui.cmd_record_sw()
Globals.gui_cmd_record = False
out.release()
new_fn = uniqueFn( fn_base = "output"
,fn_dir = Globals.gui_dir_path
,fn_ext = ext
)
gui.myGui.set_sv_fn(new_fn)
gui.myGui.get_sv_fn()
if Globals.sw_gui_dir:
Globals.sw_gui_dir = False
new_fn = uniqueFn( fn_base = "output"
,fn_dir = Globals.gui_dir_path
,fn_ext = ext
)
gui.myGui.set_sv_fn(new_fn)
gui.myGui.get_sv_fn()
if Globals.gui_b_buffer:
if sys.getsizeof(frame)* len(list_frames) > 1.5 * (10**9):
Globals.sw_record_stop = True
sw_outofmem_break = True
if reset_cntr > reset_cntr_exit:
break
if (time.time() - t0) > time_to_record:
break
if Globals.gui_cmd_quit:
break
if Globals.sw_camera_reset:
break
except Exception as e:
print 'excepted during frame read.'
print e
break
timelog.delay_here()
#Exit one cam-setup loop; still in main gui-loop
try:
cam.release()
except:
print 'no cam to release'
try:
out.release()
except:
print 'no out to release'
#Clean up
try:
cam.release()
except:
print 'no cam to release'
try:
out.release()
except:
print 'no out to release'
try:
cv2.destroyAllWindows()
except:
print 'no cv2 windows to destroy'
try:
pfn = str(Globals.gui_dir_path) + fn
print pfn, ": ", str(os.path.getsize(pfn) / (1000)), " kb"
except:
print 'couldnt find most recent saved file.'