27
27
from reid import ReID
28
28
from datacollector import DataCollector , Result
29
29
from mtmct import mtmct_process
30
- from ppvehicle .vehicle_plate import PlateRecognizer
31
30
32
31
# add deploy path of PadleDetection to sys.path
33
32
parent_path = os .path .abspath (os .path .join (__file__ , * (['..' ] * 2 )))
51
50
from pptracking .python .mot .visualize import plot_tracking_dict
52
51
from pptracking .python .mot .utils import flow_statistic
53
52
53
+ from ppvehicle .vehicle_plate import PlateRecognizer
54
+ from ppvehicle .vehicle_attr import VehicleAttr
55
+
54
56
55
57
class Pipeline (object ):
56
58
"""
@@ -224,12 +226,12 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
224
226
# general module for pphuman and ppvehicle
225
227
self .with_mot = cfg .get ('MOT' , False )['enable' ] if cfg .get (
226
228
'MOT' , False ) else False
227
- self .with_attr = cfg .get ('ATTR' , False )['enable' ] if cfg .get (
229
+ self .with_human_attr = cfg .get ('ATTR' , False )['enable' ] if cfg .get (
228
230
'ATTR' , False ) else False
229
231
if self .with_mot :
230
232
print ('Multi-Object Tracking enabled' )
231
- if self .with_attr :
232
- print ('Attribute Recognition enabled' )
233
+ if self .with_human_attr :
234
+ print ('Human Attribute Recognition enabled' )
233
235
234
236
# only for pphuman
235
237
self .with_skeleton_action = cfg .get (
@@ -265,6 +267,12 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
265
267
if self .with_vehicleplate :
266
268
print ('Vehicle Plate Recognition enabled' )
267
269
270
+ self .with_vehicle_attr = cfg .get (
271
+ 'VEHICLE_ATTR' , False )['enable' ] if cfg .get ('VEHICLE_ATTR' ,
272
+ False ) else False
273
+ if self .with_vehicle_attr :
274
+ print ('Vehicle Attribute Recognition enabled' )
275
+
268
276
self .modebase = {
269
277
"framebased" : False ,
270
278
"videobased" : False ,
@@ -294,7 +302,7 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
294
302
model_dir , device , run_mode , batch_size , trt_min_shape ,
295
303
trt_max_shape , trt_opt_shape , trt_calib_mode , cpu_threads ,
296
304
enable_mkldnn )
297
- if self .with_attr :
305
+ if self .with_human_attr :
298
306
attr_cfg = self .cfg ['ATTR' ]
299
307
model_dir = attr_cfg ['model_dir' ]
300
308
batch_size = attr_cfg ['batch_size' ]
@@ -305,8 +313,21 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
305
313
trt_max_shape , trt_opt_shape , trt_calib_mode , cpu_threads ,
306
314
enable_mkldnn )
307
315
316
+ if self .with_vehicle_attr :
317
+ vehicleattr_cfg = self .cfg ['VEHICLE_ATTR' ]
318
+ model_dir = vehicleattr_cfg ['model_dir' ]
319
+ batch_size = vehicleattr_cfg ['batch_size' ]
320
+ color_threshold = vehicleattr_cfg ['color_threshold' ]
321
+ type_threshold = vehicleattr_cfg ['type_threshold' ]
322
+ basemode = vehicleattr_cfg ['basemode' ]
323
+ self .modebase [basemode ] = True
324
+ self .vehicle_attr_predictor = VehicleAttr (
325
+ model_dir , device , run_mode , batch_size , trt_min_shape ,
326
+ trt_max_shape , trt_opt_shape , trt_calib_mode , cpu_threads ,
327
+ enable_mkldnn , color_threshold , type_threshold )
328
+
308
329
else :
309
- if self .with_attr :
330
+ if self .with_human_attr :
310
331
attr_cfg = self .cfg ['ATTR' ]
311
332
model_dir = attr_cfg ['model_dir' ]
312
333
batch_size = attr_cfg ['batch_size' ]
@@ -412,6 +433,19 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
412
433
basemode = vehicleplate_cfg ['basemode' ]
413
434
self .modebase [basemode ] = True
414
435
436
+ if self .with_vehicle_attr :
437
+ vehicleattr_cfg = self .cfg ['VEHICLE_ATTR' ]
438
+ model_dir = vehicleattr_cfg ['model_dir' ]
439
+ batch_size = vehicleattr_cfg ['batch_size' ]
440
+ color_threshold = vehicleattr_cfg ['color_threshold' ]
441
+ type_threshold = vehicleattr_cfg ['type_threshold' ]
442
+ basemode = vehicleattr_cfg ['basemode' ]
443
+ self .modebase [basemode ] = True
444
+ self .vehicle_attr_predictor = VehicleAttr (
445
+ model_dir , device , run_mode , batch_size , trt_min_shape ,
446
+ trt_max_shape , trt_opt_shape , trt_calib_mode , cpu_threads ,
447
+ enable_mkldnn , color_threshold , type_threshold )
448
+
415
449
if self .with_mot or self .modebase ["idbased" ] or self .modebase [
416
450
"skeletonbased" ]:
417
451
mot_cfg = self .cfg ['MOT' ]
@@ -510,7 +544,7 @@ def predict_image(self, input):
510
544
self .pipe_timer .module_time ['det' ].end ()
511
545
self .pipeline_res .update (det_res , 'det' )
512
546
513
- if self .with_attr :
547
+ if self .with_human_attr :
514
548
crop_inputs = crop_image_with_det (batch_input , det_res )
515
549
attr_res_list = []
516
550
@@ -528,6 +562,24 @@ def predict_image(self, input):
528
562
attr_res = {'output' : attr_res_list }
529
563
self .pipeline_res .update (attr_res , 'attr' )
530
564
565
+ if self .with_vehicle_attr :
566
+ crop_inputs = crop_image_with_det (batch_input , det_res )
567
+ vehicle_attr_res_list = []
568
+
569
+ if i > self .warmup_frame :
570
+ self .pipe_timer .module_time ['vehicle_attr' ].start ()
571
+
572
+ for crop_input in crop_inputs :
573
+ attr_res = self .vehicle_attr_predictor .predict_image (
574
+ crop_input , visual = False )
575
+ vehicle_attr_res_list .extend (attr_res ['output' ])
576
+
577
+ if i > self .warmup_frame :
578
+ self .pipe_timer .module_time ['vehicle_attr' ].end ()
579
+
580
+ attr_res = {'output' : vehicle_attr_res_list }
581
+ self .pipeline_res .update (attr_res , 'vehicle_attr' )
582
+
531
583
self .pipe_timer .img_num += len (batch_input )
532
584
if i > self .warmup_frame :
533
585
self .pipe_timer .total_time .end ()
@@ -581,13 +633,14 @@ def predict_video(self, video_file):
581
633
ret , frame = capture .read ()
582
634
if not ret :
583
635
break
636
+ frame_rgb = cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB )
584
637
585
638
if self .modebase ["idbased" ] or self .modebase ["skeletonbased" ]:
586
639
if frame_id > self .warmup_frame :
587
640
self .pipe_timer .total_time .start ()
588
641
self .pipe_timer .module_time ['mot' ].start ()
589
642
res = self .mot_predictor .predict_image (
590
- [copy .deepcopy (frame )], visual = False )
643
+ [copy .deepcopy (frame_rgb )], visual = False )
591
644
592
645
if frame_id > self .warmup_frame :
593
646
self .pipe_timer .module_time ['mot' ].end ()
@@ -625,14 +678,14 @@ def predict_video(self, video_file):
625
678
626
679
self .pipeline_res .update (mot_res , 'mot' )
627
680
crop_input , new_bboxes , ori_bboxes = crop_image_with_mot (
628
- frame , mot_res )
681
+ frame_rgb , mot_res )
629
682
630
683
if self .with_vehicleplate :
631
684
platelicense = self .vehicleplate_detector .get_platelicense (
632
685
crop_input )
633
686
self .pipeline_res .update (platelicense , 'vehicleplate' )
634
687
635
- if self .with_attr :
688
+ if self .with_human_attr :
636
689
if frame_id > self .warmup_frame :
637
690
self .pipe_timer .module_time ['attr' ].start ()
638
691
attr_res = self .attr_predictor .predict_image (
@@ -641,6 +694,15 @@ def predict_video(self, video_file):
641
694
self .pipe_timer .module_time ['attr' ].end ()
642
695
self .pipeline_res .update (attr_res , 'attr' )
643
696
697
+ if self .with_vehicle_attr :
698
+ if frame_id > self .warmup_frame :
699
+ self .pipe_timer .module_time ['vehicle_attr' ].start ()
700
+ attr_res = self .vehicle_attr_predictor .predict_image (
701
+ crop_input , visual = False )
702
+ if frame_id > self .warmup_frame :
703
+ self .pipe_timer .module_time ['vehicle_attr' ].end ()
704
+ self .pipeline_res .update (attr_res , 'vehicle_attr' )
705
+
644
706
if self .with_idbased_detaction :
645
707
if frame_id > self .warmup_frame :
646
708
self .pipe_timer .module_time ['det_action' ].start ()
@@ -708,7 +770,7 @@ def predict_video(self, video_file):
708
770
709
771
if self .with_mtmct and frame_id % 10 == 0 :
710
772
crop_input , img_qualities , rects = self .reid_predictor .crop_image_with_mot (
711
- frame , mot_res )
773
+ frame_rgb , mot_res )
712
774
if frame_id > self .warmup_frame :
713
775
self .pipe_timer .module_time ['reid' ].start ()
714
776
reid_res = self .reid_predictor .predict_batch (crop_input )
@@ -740,7 +802,7 @@ def predict_video(self, video_file):
740
802
# collect frames
741
803
if frame_id % sample_freq == 0 :
742
804
# Scale image
743
- scaled_img = scale (frame )
805
+ scaled_img = scale (frame_rgb )
744
806
video_action_imgs .append (scaled_img )
745
807
746
808
# the number of collected frames is enough to predict video action
@@ -820,11 +882,18 @@ def visualize_video(self,
820
882
records = records ,
821
883
center_traj = center_traj )
822
884
823
- attr_res = result .get ('attr' )
824
- if attr_res is not None :
885
+ human_attr_res = result .get ('attr' )
886
+ if human_attr_res is not None :
887
+ boxes = mot_res ['boxes' ][:, 1 :]
888
+ human_attr_res = human_attr_res ['output' ]
889
+ image = visualize_attr (image , human_attr_res , boxes )
890
+ image = np .array (image )
891
+
892
+ vehicle_attr_res = result .get ('vehicle_attr' )
893
+ if vehicle_attr_res is not None :
825
894
boxes = mot_res ['boxes' ][:, 1 :]
826
- attr_res = attr_res ['output' ]
827
- image = visualize_attr (image , attr_res , boxes )
895
+ vehicle_attr_res = vehicle_attr_res ['output' ]
896
+ image = visualize_attr (image , vehicle_attr_res , boxes )
828
897
image = np .array (image )
829
898
830
899
vehicleplate_res = result .get ('vehicleplate' )
@@ -883,7 +952,9 @@ def visualize_video(self,
883
952
def visualize_image (self , im_files , images , result ):
884
953
start_idx , boxes_num_i = 0 , 0
885
954
det_res = result .get ('det' )
886
- attr_res = result .get ('attr' )
955
+ human_attr_res = result .get ('attr' )
956
+ vehicle_attr_res = result .get ('vehicle_attr' )
957
+
887
958
for i , (im_file , im ) in enumerate (zip (im_files , images )):
888
959
if det_res is not None :
889
960
det_res_i = {}
@@ -897,10 +968,15 @@ def visualize_image(self, im_files, images, result):
897
968
threshold = self .cfg ['crop_thresh' ])
898
969
im = np .ascontiguousarray (np .copy (im ))
899
970
im = cv2 .cvtColor (im , cv2 .COLOR_RGB2BGR )
900
- if attr_res is not None :
901
- attr_res_i = attr_res ['output' ][start_idx :start_idx +
902
- boxes_num_i ]
903
- im = visualize_attr (im , attr_res_i , det_res_i ['boxes' ])
971
+ if human_attr_res is not None :
972
+ human_attr_res_i = human_attr_res ['output' ][start_idx :start_idx
973
+ + boxes_num_i ]
974
+ im = visualize_attr (im , human_attr_res_i , det_res_i ['boxes' ])
975
+ if vehicle_attr_res is not None :
976
+ vehicle_attr_res_i = vehicle_attr_res ['output' ][
977
+ start_idx :start_idx + boxes_num_i ]
978
+ im = visualize_attr (im , vehicle_attr_res_i , det_res_i ['boxes' ])
979
+
904
980
img_name = os .path .split (im_file )[- 1 ]
905
981
if not os .path .exists (self .output_dir ):
906
982
os .makedirs (self .output_dir )
0 commit comments