21
21
from tempfile import TemporaryDirectory
22
22
from engineering_notation import EngNumber # only from pip
23
23
from tqdm import tqdm
24
+
24
25
# may only apply to windows
25
26
try :
26
27
from scripts .regsetup import description
29
30
pass
30
31
31
32
import v2e .desktop as desktop
32
- from v2e .v2e_utils import all_images , read_image , \
33
+ from v2e .v2e_utils import all_images , read_image , \
33
34
check_lowpass , v2e_quit
34
35
from v2e .v2e_args import v2e_args , write_args_info , v2e_check_dvs_exposure_args
35
36
from v2e .v2e_args import NO_SLOWDOWN
@@ -79,11 +80,10 @@ def makeOutputFolder(output_folder_base, suffix_counter,
79
80
logger .error ("specify either --overwrite or --unique_output_folder" )
80
81
v2e_quit ()
81
82
if suffix_counter > 0 :
82
- output_folder = output_folder_base + '-' + str (suffix_counter )
83
+ output_folder = output_folder_base + '-' + str (suffix_counter )
83
84
else :
84
- output_folder = output_folder_base
85
- nonEmptyFolderExists = not overwrite and os .path .exists (output_folder ) \
86
- and os .listdir (output_folder )
85
+ output_folder = output_folder_base
86
+ nonEmptyFolderExists = not overwrite and os .path .exists (output_folder ) and os .listdir (output_folder )
87
87
if nonEmptyFolderExists and not overwrite and not unique_output_folder :
88
88
logger .error (
89
89
'non-empty output folder {} already exists \n '
@@ -93,7 +93,7 @@ def makeOutputFolder(output_folder_base, suffix_counter,
93
93
94
94
if nonEmptyFolderExists and unique_output_folder :
95
95
return makeOutputFolder (
96
- output_folder_base , suffix_counter + 1 , overwrite , unique_output_folder )
96
+ output_folder_base , suffix_counter + 1 , overwrite , unique_output_folder )
97
97
else :
98
98
logger .info ('using output folder {}' .format (output_folder ))
99
99
if not os .path .exists (output_folder ):
@@ -102,13 +102,12 @@ def makeOutputFolder(output_folder_base, suffix_counter,
102
102
103
103
104
104
def main ():
105
-
106
105
args = get_args ()
107
- overwrite = args .overwrite
108
- output_folder = args .output_folder
109
- unique_output_folder = args .unique_output_folder
106
+ overwrite : bool = args .overwrite
107
+ output_folder : str = args .output_folder
108
+ unique_output_folder : bool = args .unique_output_folder
110
109
111
- output_folder = makeOutputFolder (output_folder , 0 , overwrite , unique_output_folder )
110
+ output_folder = makeOutputFolder (output_folder , 0 , overwrite , unique_output_folder )
112
111
113
112
if (args .output_width is not None ) ^ (args .output_width is not None ):
114
113
logger .error (
@@ -191,21 +190,21 @@ def main():
191
190
logger .error ('source {} fps is 0' .format (input_file ))
192
191
v2e_quit ()
193
192
194
- srcFrameIntervalS = (1. / srcFps )/ input_slowmotion_factor
193
+ srcFrameIntervalS = (1. / srcFps ) / input_slowmotion_factor
195
194
196
- slowdown_factor = int (np .ceil (srcFrameIntervalS / timestamp_resolution ))
195
+ slowdown_factor = int (np .ceil (srcFrameIntervalS / timestamp_resolution ))
197
196
if slowdown_factor < 1 :
198
197
slowdown_factor = 1
199
198
logger .warning (
200
199
'timestamp resolution={}s is greater than source '
201
200
'frame interval={}s, will not use upsampling'
202
- .format (timestamp_resolution , srcFrameIntervalS ))
201
+ .format (timestamp_resolution , srcFrameIntervalS ))
203
202
204
203
logger .info (
205
204
'src video frame rate={:.2f} Hz with slowmotion_factor={:.2f}, '
206
205
'timestamp resolution={:.3f} ms, computed slomo upsampling factor={}'
207
- .format (
208
- srcFps , input_slowmotion_factor , timestamp_resolution * 1000 ,
206
+ .format (
207
+ srcFps , input_slowmotion_factor , timestamp_resolution * 1000 ,
209
208
slowdown_factor ))
210
209
211
210
slomoTimestampResolutionS = srcFrameIntervalS / slowdown_factor
@@ -220,9 +219,9 @@ def main():
220
219
logger .warning (
221
220
'upsampled src frame intervals of {}s is larger than '
222
221
'the desired DVS timestamp resolution of {}s'
223
- .format (slomoTimestampResolutionS , timestamp_resolution ))
222
+ .format (slomoTimestampResolutionS , timestamp_resolution ))
224
223
225
- check_lowpass (cutoff_hz , 1 / slomoTimestampResolutionS , logger )
224
+ check_lowpass (cutoff_hz , 1 / slomoTimestampResolutionS , logger )
226
225
227
226
# the SloMo model, set no SloMo model if no slowdown
228
227
if slowdown_factor != NO_SLOWDOWN :
@@ -238,15 +237,15 @@ def main():
238
237
if start_time else 0
239
238
stop_frame = int (srcNumFrames * (stop_time / srcTotalDuration )) \
240
239
if stop_time else srcNumFrames
241
- srcNumFramesToBeProccessed = stop_frame - start_frame + 1
242
- srcDurationToBeProcessed = srcNumFramesToBeProccessed / srcFps
243
- start_time = start_frame / srcFps
244
- stop_time = stop_frame / srcFps
240
+ srcNumFramesToBeProccessed = stop_frame - start_frame + 1
241
+ srcDurationToBeProcessed = srcNumFramesToBeProccessed / srcFps
242
+ start_time = start_frame / srcFps
243
+ stop_time = stop_frame / srcFps
245
244
246
245
if exposure_mode == ExposureMode .DURATION :
247
- dvsFps = 1. / exposure_val
246
+ dvsFps = 1. / exposure_val
248
247
dvsNumFrames = np .math .floor (
249
- dvsFps * srcDurationToBeProcessed / input_slowmotion_factor )
248
+ dvsFps * srcDurationToBeProcessed / input_slowmotion_factor )
250
249
dvsDuration = dvsNumFrames / dvsFps
251
250
dvsPlaybackDuration = dvsNumFrames / avi_frame_rate
252
251
logger .info (
@@ -258,20 +257,20 @@ def main():
258
257
'\n v2e DVS video will have {}fps (accumulation time {}s), '
259
258
'\n DVS video will have {} frames with duration {}s '
260
259
'and playback duration {}s\n '
261
- .format (input_file , srcNumFrames , EngNumber (srcTotalDuration ),
262
- EngNumber (srcFps ), EngNumber (input_slowmotion_factor ),
263
- EngNumber (srcFrameIntervalS ),
264
- EngNumber (srcFps * slowdown_factor ),
265
- EngNumber (slomoTimestampResolutionS ),
266
- EngNumber (dvsFps ), EngNumber (1 / dvsFps ),
267
- dvsNumFrames , EngNumber (dvsDuration ),
268
- EngNumber (dvsPlaybackDuration )))
269
- if dvsFps > (1 / slomoTimestampResolutionS ):
260
+ .format (input_file , srcNumFrames , EngNumber (srcTotalDuration ),
261
+ EngNumber (srcFps ), EngNumber (input_slowmotion_factor ),
262
+ EngNumber (srcFrameIntervalS ),
263
+ EngNumber (srcFps * slowdown_factor ),
264
+ EngNumber (slomoTimestampResolutionS ),
265
+ EngNumber (dvsFps ), EngNumber (1 / dvsFps ),
266
+ dvsNumFrames , EngNumber (dvsDuration ),
267
+ EngNumber (dvsPlaybackDuration )))
268
+ if dvsFps > (1 / slomoTimestampResolutionS ):
270
269
logger .warning (
271
270
'DVS video frame rate={}Hz is larger than '
272
271
'the effective DVS frame rate of {}Hz; '
273
272
'DVS video will have blank frames' .format (
274
- dvsFps , (1 / slomoTimestampResolutionS )))
273
+ dvsFps , (1 / slomoTimestampResolutionS )))
275
274
else :
276
275
logger .info (
277
276
'\n \n {} has {} frames with duration {}s, '
@@ -280,11 +279,11 @@ def main():
280
279
'\n events will have timestamp resolution {}s,'
281
280
'\n v2e DVS video will have constant count '
282
281
'frames with {} events), '
283
- .format (input_file , srcNumFrames , EngNumber (srcTotalDuration ),
284
- EngNumber (srcFps ), EngNumber (srcFrameIntervalS ),
285
- EngNumber (srcFps * slowdown_factor ),
286
- EngNumber (slomoTimestampResolutionS ),
287
- exposure_val ))
282
+ .format (input_file , srcNumFrames , EngNumber (srcTotalDuration ),
283
+ EngNumber (srcFps ), EngNumber (srcFrameIntervalS ),
284
+ EngNumber (srcFps * slowdown_factor ),
285
+ EngNumber (slomoTimestampResolutionS ),
286
+ exposure_val ))
288
287
289
288
emulator = EventEmulator (
290
289
pos_thres = pos_thres , neg_thres = neg_thres ,
@@ -305,7 +304,7 @@ def main():
305
304
306
305
# timestamps of DVS start at zero and end with span of video we processed
307
306
ts0 = 0
308
- ts1 = (stop_time - start_time )/ input_slowmotion_factor
307
+ ts1 = (stop_time - start_time ) / input_slowmotion_factor
309
308
num_frames = srcNumFramesToBeProccessed
310
309
inputHeight = None
311
310
inputWidth = None
@@ -336,25 +335,25 @@ def main():
336
335
'input video size\n Are you sure you want this? '
337
336
'It might be slow.\n Consider using '
338
337
'--output_width and --output_height'
339
- .format (output_width , output_height ))
338
+ .format (output_width , output_height ))
340
339
341
340
logger .info ('Resizing input frames to output size '
342
341
'(with possible RGG to luma conversion)' )
343
342
for inputFrameIndex in tqdm (
344
343
range (srcNumFramesToBeProccessed ),
345
344
desc = 'rgb2luma' , unit = 'fr' ):
346
- # read frame
345
+ # read frame
347
346
ret , inputVideoFrame = cap .read ()
348
347
349
- if not ret or inputFrameIndex + start_frame > stop_frame :
348
+ if not ret or inputFrameIndex + start_frame > stop_frame :
350
349
break
351
350
352
351
if output_height and output_width and \
353
352
(inputHeight != output_height or
354
353
inputWidth != output_width ):
355
354
dim = (output_width , output_height )
356
- (fx , fy ) = (float (output_width )/ inputWidth ,
357
- float (output_height )/ inputHeight )
355
+ (fx , fy ) = (float (output_width ) / inputWidth ,
356
+ float (output_height ) / inputHeight )
358
357
inputVideoFrame = cv2 .resize (
359
358
src = inputVideoFrame , dsize = dim , fx = fx , fy = fy ,
360
359
interpolation = cv2 .INTER_AREA )
@@ -369,7 +368,7 @@ def main():
369
368
370
369
# save frame into numpy records
371
370
save_path = os .path .join (
372
- source_frames_dir , str (inputFrameIndex ).zfill (8 )+ ".npy" )
371
+ source_frames_dir , str (inputFrameIndex ).zfill (8 ) + ".npy" )
373
372
np .save (save_path , inputVideoFrame )
374
373
# print("Writing source frame {}".format(save_path), end="\r")
375
374
cap .release ()
@@ -389,12 +388,12 @@ def main():
389
388
.format (source_frames_dir ))
390
389
interpFramesFilenames = []
391
390
src_files = sorted (
392
- glob .glob ("{}" .format (source_frames_dir )+ "/*.npy" ))
391
+ glob .glob ("{}" .format (source_frames_dir ) + "/*.npy" ))
393
392
for frame_idx , src_file_path in tqdm (
394
393
enumerate (src_files ), desc = 'npy2png' , unit = 'fr' ):
395
394
src_frame = np .load (src_file_path )
396
395
tgt_file_path = os .path .join (
397
- interpFramesFolder , str (frame_idx )+ ".png" )
396
+ interpFramesFolder , str (frame_idx ) + ".png" )
398
397
interpFramesFilenames .append (tgt_file_path )
399
398
cv2 .imwrite (tgt_file_path , src_frame )
400
399
@@ -408,18 +407,18 @@ def main():
408
407
# interpolate events
409
408
# get some progress bar
410
409
# events = np.zeros((0, 4), dtype=np.float32)
411
- num_batches = (n // (slowdown_factor * batch_size ))+ 1
410
+ num_batches = (n // (slowdown_factor * batch_size )) + 1
412
411
413
412
with tqdm (
414
- total = num_batches * slowdown_factor * batch_size ,
413
+ total = num_batches * slowdown_factor * batch_size ,
415
414
desc = 'dvs' , unit = 'fr' ) as pbar :
416
415
for batch_idx in (range (num_batches )):
417
416
events = np .zeros ((0 , 4 ), dtype = np .float32 )
418
- for sub_img_idx in range (slowdown_factor * batch_size ):
419
- image_idx = batch_idx * (slowdown_factor * batch_size ) + \
420
- sub_img_idx
417
+ for sub_img_idx in range (slowdown_factor * batch_size ):
418
+ image_idx = batch_idx * (slowdown_factor * batch_size ) + \
419
+ sub_img_idx
421
420
# at the end of the file
422
- if image_idx > n - 1 :
421
+ if image_idx > n - 1 :
423
422
break
424
423
fr = read_image (interpFramesFilenames [image_idx ])
425
424
newEvents = emulator .generate_events (
@@ -444,26 +443,26 @@ def main():
444
443
logger .error ('no frames read from file' )
445
444
v2e_quit ()
446
445
totalTime = (time .time () - time_run_started )
447
- framePerS = num_frames / totalTime
448
- sPerFrame = 1 / framePerS
449
- throughputStr = (str (EngNumber (framePerS ))+ 'fr/s' ) \
450
- if framePerS > 1 else (str (EngNumber (sPerFrame ))+ 's/fr' )
446
+ framePerS = num_frames / totalTime
447
+ sPerFrame = 1 / framePerS
448
+ throughputStr = (str (EngNumber (framePerS )) + 'fr/s' ) \
449
+ if framePerS > 1 else (str (EngNumber (sPerFrame )) + 's/fr' )
451
450
logger .info (
452
451
'done processing {} frames in {}s ({})\n see output folder {}'
453
- .format (num_frames ,
454
- EngNumber (totalTime ),
455
- throughputStr ,
456
- output_folder ))
452
+ .format (num_frames ,
453
+ EngNumber (totalTime ),
454
+ throughputStr ,
455
+ output_folder ))
457
456
logger .info ('generated total {} events ({} on, {} off)'
458
457
.format (EngNumber (emulator .num_events_total ),
459
458
EngNumber (emulator .num_events_on ),
460
459
EngNumber (emulator .num_events_off )))
461
460
logger .info (
462
461
'avg event rate {}Hz ({}Hz on, {}Hz off)'
463
- .format (
464
- EngNumber (emulator .num_events_total / srcDurationToBeProcessed ),
465
- EngNumber (emulator .num_events_on / srcDurationToBeProcessed ),
466
- EngNumber (emulator .num_events_off / srcDurationToBeProcessed )))
462
+ .format (
463
+ EngNumber (emulator .num_events_total / srcDurationToBeProcessed ),
464
+ EngNumber (emulator .num_events_on / srcDurationToBeProcessed ),
465
+ EngNumber (emulator .num_events_off / srcDurationToBeProcessed )))
467
466
try :
468
467
desktop .open (os .path .abspath (output_folder ))
469
468
except Exception as e :
0 commit comments