65
65
is_async_mode = True
66
66
67
67
# Event class to store the intruder details
68
+
69
+
68
70
class Event :
69
71
def __init__ (self , event_time = None , intruder = None , count = None , frame = None ):
70
72
self .time = event_time
@@ -176,6 +178,7 @@ def parse_args():
176
178
else :
177
179
is_async_mode = True
178
180
181
+
179
182
def check_args ():
180
183
"""
181
184
Validate the command line arguments
@@ -249,17 +252,20 @@ def get_input():
249
252
labels = []
250
253
streams = []
251
254
252
- assert os .path .isfile (CONFIG_FILE ), "{} file doesn't exist" .format (CONFIG_FILE )
255
+ assert os .path .isfile (
256
+ CONFIG_FILE ), "{} file doesn't exist" .format (CONFIG_FILE )
253
257
config = json .loads (open (CONFIG_FILE ).read ())
254
258
for id , item in enumerate (config ['inputs' ]):
255
259
for idx , video in enumerate (item ['video' ]):
256
260
cams = idx + 1
257
261
cam_name = "Cam {}" .format (idx )
258
262
if video .isdigit ():
259
- video_cap = VideoCap (cv2 .VideoCapture (int (video )), cam_name , cams , is_cam = True )
263
+ video_cap = VideoCap (cv2 .VideoCapture (
264
+ int (video )), cam_name , cams , is_cam = True )
260
265
else :
261
266
if os .path .isfile (video ):
262
- video_cap = VideoCap (cv2 .VideoCapture (video ), cam_name , cams , is_cam = False )
267
+ video_cap = VideoCap (cv2 .VideoCapture (
268
+ video ), cam_name , cams , is_cam = False )
263
269
else :
264
270
return [- 8 , [video ]]
265
271
video_caps .append (video_cap )
@@ -302,16 +308,22 @@ def save_json():
302
308
for i in range (events_size ):
303
309
event_json .write ("\t \t \" %d\" :{\n " % (i ))
304
310
event_json .write ("\t \t \t \" time\" :\" %s\" ,\n " % events [i ].time )
305
- event_json .write ("\t \t \t \" content\" :\" %s\" ,\n " % events [i ].intruder )
306
- event_json .write ("\t \t \t \" videoTime\" :\" %d\" \n " % float (events [i ].frame / fps ))
311
+ event_json .write ("\t \t \t \" content\" :\" %s\" ,\n " %
312
+ events [i ].intruder )
313
+ event_json .write ("\t \t \t \" videoTime\" :\" %d\" \n " %
314
+ float (events [i ].frame / fps ))
307
315
event_json .write ("\t \t },\n " )
308
- data_json .write ("\t \t \" %d\" : \" %d\" ,\n " % (float (events [i ].frame / fps ), events [i ].count ))
316
+ data_json .write ("\t \t \" %d\" : \" %d\" ,\n " %
317
+ (float (events [i ].frame / fps ), events [i ].count ))
309
318
event_json .write ("\t \t \" %d\" :{\n " % events_size )
310
319
event_json .write ("\t \t \t \" time\" :\" %s\" ,\n " % events [events_size ].time )
311
- event_json .write ("\t \t \t \" content\" :\" %s\" ,\n " % events [events_size ].intruder )
312
- event_json .write ("\t \t \t \" videoTime\" :\" %d\" \n " % float (events [events_size ].frame / fps ))
320
+ event_json .write ("\t \t \t \" content\" :\" %s\" ,\n " %
321
+ events [events_size ].intruder )
322
+ event_json .write ("\t \t \t \" videoTime\" :\" %d\" \n " %
323
+ float (events [events_size ].frame / fps ))
313
324
event_json .write ("\t \t }\n " )
314
- data_json .write ("\t \t \" %d\" : \" %d\" \n " % (float (events [events_size ].frame / fps ), events [events_size ].count ))
325
+ data_json .write ("\t \t \" %d\" : \" %d\" \n " % (
326
+ float (events [events_size ].frame / fps ), events [events_size ].count ))
315
327
total = events [events_size ].count
316
328
event_json .write ("\t }\n " )
317
329
event_json .write ("}" )
@@ -350,13 +362,17 @@ def arrange_windows():
350
362
rows += 1
351
363
cols = 1
352
364
cv2 .namedWindow (video_caps [idx ].cam_name , cv2 .WINDOW_NORMAL )
353
- cv2 .resizeWindow (video_caps [idx ].cam_name , window_width , window_height )
354
- cv2 .moveWindow (video_caps [idx ].cam_name , spacer * cols , row_spacer * rows )
365
+ cv2 .resizeWindow (video_caps [idx ].cam_name ,
366
+ window_width , window_height )
367
+ cv2 .moveWindow (video_caps [idx ].cam_name ,
368
+ spacer * cols , row_spacer * rows )
355
369
else :
356
370
cols += 1
357
371
cv2 .namedWindow (video_caps [idx ].cam_name , cv2 .WINDOW_NORMAL )
358
- cv2 .resizeWindow (video_caps [idx ].cam_name , window_width , window_height )
359
- cv2 .moveWindow (video_caps [idx ].cam_name , spacer * cols , row_spacer * rows )
372
+ cv2 .resizeWindow (video_caps [idx ].cam_name ,
373
+ window_width , window_height )
374
+ cv2 .moveWindow (video_caps [idx ].cam_name ,
375
+ spacer * cols , row_spacer * rows )
360
376
361
377
362
378
# Signal handler
@@ -446,14 +462,16 @@ def intruder_detector():
446
462
# Initializing VideoWriter for each source
447
463
if UI and not LOOP_VIDEO :
448
464
for video_cap in video_caps :
449
- ret , ret_value = video_cap .init_vw (int (video_cap .input_height ), int (video_cap .input_width ))
465
+ ret , ret_value = video_cap .init_vw (
466
+ int (video_cap .input_height ), int (video_cap .input_width ))
450
467
if ret != 0 :
451
468
return ret , ret_value
452
469
453
470
# Initialise the class
454
471
infer_network = Network ()
455
472
# Load the network to IE plugin to get shape of input layer
456
- n , c , h , w = infer_network .load_model (model_xml , TARGET_DEVICE , 1 , 1 , 2 , CPU_EXTENSION )[1 ]
473
+ n , c , h , w = infer_network .load_model (
474
+ model_xml , TARGET_DEVICE , 1 , 1 , 2 , CPU_EXTENSION )[1 ]
457
475
# Arrange windows so that they are not overlapping
458
476
arrange_windows ()
459
477
@@ -488,7 +506,8 @@ def intruder_detector():
488
506
if no_more_data [idx ]:
489
507
stream_end_frame = numpy .zeros ((int (video_cap .input_height ), int (video_cap .input_width ), 1 ),
490
508
dtype = 'uint8' )
491
- stream_end_message = "Stream from {} has ended." .format (video_cap .cam_name )
509
+ stream_end_message = "Stream from {} has ended." .format (
510
+ video_cap .cam_name )
492
511
cv2 .putText (stream_end_frame , stream_end_message , (int (video_cap .input_width / 2 ) - 30 ,
493
512
int (video_cap .input_height / 2 ) - 30 ),
494
513
cv2 .FONT_HERSHEY_COMPLEX , 0.5 , (255 , 255 , 255 ), 1 )
@@ -514,7 +533,8 @@ def intruder_detector():
514
533
# Async enabled and more than one video capture
515
534
else :
516
535
# Get previous index
517
- videoCapResult = video_caps [idx - 1 if idx - 1 >= 0 else len (video_caps ) - 1 ]
536
+ videoCapResult = video_caps [idx -
537
+ 1 if idx - 1 >= 0 else len (video_caps ) - 1 ]
518
538
519
539
else :
520
540
in_frame = cv2 .resize (video_cap .frame , (w , h ))
@@ -541,7 +561,8 @@ def intruder_detector():
541
561
xmax = int (obj [5 ] * videoCapResult .input_width )
542
562
ymax = int (obj [6 ] * videoCapResult .input_height )
543
563
# Draw bounding box around the intruder detected
544
- cv2 .rectangle (videoCapResult .frame , (xmin , ymin ), (xmax , ymax ), (0 , 255 , 0 ), 4 , 16 )
564
+ cv2 .rectangle (videoCapResult .frame , (xmin , ymin ),
565
+ (xmax , ymax ), (0 , 255 , 0 ), 4 , 16 )
545
566
546
567
for i in range (videoCapResult .no_of_labels ):
547
568
if videoCapResult .candidate_count [i ] == videoCapResult .current_count [i ]:
@@ -557,8 +578,10 @@ def intruder_detector():
557
578
continue
558
579
559
580
if videoCapResult .current_count [i ] > videoCapResult .last_correct_count [i ]:
560
- videoCapResult .total_count [i ] += videoCapResult .current_count [i ] - videoCapResult .last_correct_count [i ]
561
- det_objs = videoCapResult .current_count [i ] - videoCapResult .last_correct_count [i ]
581
+ videoCapResult .total_count [i ] += videoCapResult .current_count [i ] - \
582
+ videoCapResult .last_correct_count [i ]
583
+ det_objs = videoCapResult .current_count [i ] - \
584
+ videoCapResult .last_correct_count [i ]
562
585
total_count = sum (videoCapResult .total_count )
563
586
for det_obj in range (det_objs ):
564
587
current_time = time .strftime ("%H:%M:%S" )
@@ -570,14 +593,17 @@ def intruder_detector():
570
593
frame = videoCapResult .frame_count )
571
594
videoCapResult .events .append (event )
572
595
573
- snapshot_name = "output/intruder_{}.png" .format (total_count )
596
+ snapshot_name = "output/intruder_{}.png" .format (
597
+ total_count )
574
598
cv2 .imwrite (snapshot_name , videoCapResult .frame )
575
599
videoCapResult .last_correct_count [i ] = videoCapResult .current_count [i ]
576
600
577
601
# Create intruder log window, add logs to the frame and display it
578
- log_window = numpy .zeros ((LOG_WIN_HEIGHT , LOG_WIN_WIDTH , 1 ), dtype = 'uint8' )
602
+ log_window = numpy .zeros (
603
+ (LOG_WIN_HEIGHT , LOG_WIN_WIDTH , 1 ), dtype = 'uint8' )
579
604
for i , log in enumerate (log_list ):
580
- cv2 .putText (log_window , log , (10 , 20 * i + 15 ), cv2 .FONT_HERSHEY_SIMPLEX , 0.5 , (255 , 255 , 255 ), 1 )
605
+ cv2 .putText (log_window , log , (10 , 20 * i + 15 ),
606
+ cv2 .FONT_HERSHEY_SIMPLEX , 0.5 , (255 , 255 , 255 ), 1 )
581
607
cv2 .imshow ("Intruder Log" , log_window )
582
608
videoCapResult .frame_count += 1
583
609
@@ -620,7 +646,8 @@ def intruder_detector():
620
646
621
647
if cv2 .waitKey (1 ) == 9 :
622
648
is_async_mode = not is_async_mode
623
- print ("Switched to {} mode" .format ("async" if is_async_mode else "sync" ))
649
+ print ("Switched to {} mode" .format (
650
+ "async" if is_async_mode else "sync" ))
624
651
625
652
if False not in no_more_data :
626
653
break
@@ -670,7 +697,8 @@ def intruder_detector():
670
697
elif status == - 14 :
671
698
print ("No input source found in configuration file!" )
672
699
elif status == - 15 :
673
- print ("Error: No labels currently in use. Please edit " + CONFIG_FILE + " file!" )
700
+ print ("Error: No labels currently in use. Please edit " +
701
+ CONFIG_FILE + " file!" )
674
702
elif status == - 16 :
675
703
print ("Error in opening intruder log file!" )
676
704
elif status == - 17 :
0 commit comments