Skip to content

Commit 2f06ad8

Browse files
authored
add vehicle attr model into pipeline (PaddlePaddle#6274)
* add vehicle attr inti pipeline * fix in no-rgb in predict_video
1 parent 3953163 commit 2f06ad8

File tree

5 files changed

+238
-28
lines changed

5 files changed

+238
-28
lines changed

deploy/pphuman/config/infer_cfg_ppvehicle.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
crop_thresh: 0.5
2-
attr_thresh: 0.5
3-
kpt_thresh: 0.2
42
visual: True
53
warmup_frame: 50
64

@@ -24,12 +22,14 @@ VEHICLE_PLATE:
2422
rec_batch_num: 6
2523
word_dict_path: deploy/pphuman/ppvehicle/rec_word_dict.txt
2624
basemode: "idbased"
27-
enable: True
25+
enable: False
2826

29-
ATTR:
30-
model_dir: output_inference/strongbaseline_r50_30e/
27+
VEHICLE_ATTR:
28+
model_dir: output_inference/vehicle_attribute_infer/
3129
batch_size: 8
3230
basemode: "idbased"
31+
color_threshold: 0.5
32+
type_threshold: 0.5
3333
enable: False
3434

3535
REID:

deploy/pphuman/datacollector.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def __init__(self):
2828
'reid': dict(),
2929
'det_action': dict(),
3030
'cls_action': dict(),
31-
'vehicleplate': dict()
31+
'vehicleplate': dict(),
32+
'vehicle_attr': dict()
3233
}
3334

3435
def update(self, res, name):

deploy/pphuman/pipe_utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ def __init__(self):
156156
'skeleton_action': Times(),
157157
'reid': Times(),
158158
'det_action': Times(),
159-
'cls_action': Times()
159+
'cls_action': Times(),
160+
'vehicle_attr': Times()
160161
}
161162
self.img_num = 0
162163

deploy/pphuman/pipeline.py

+97-21
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from reid import ReID
2828
from datacollector import DataCollector, Result
2929
from mtmct import mtmct_process
30-
from ppvehicle.vehicle_plate import PlateRecognizer
3130

3231
# add deploy path of PadleDetection to sys.path
3332
parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 2)))
@@ -51,6 +50,9 @@
5150
from pptracking.python.mot.visualize import plot_tracking_dict
5251
from pptracking.python.mot.utils import flow_statistic
5352

53+
from ppvehicle.vehicle_plate import PlateRecognizer
54+
from ppvehicle.vehicle_attr import VehicleAttr
55+
5456

5557
class Pipeline(object):
5658
"""
@@ -224,12 +226,12 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
224226
# general module for pphuman and ppvehicle
225227
self.with_mot = cfg.get('MOT', False)['enable'] if cfg.get(
226228
'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(
228230
'ATTR', False) else False
229231
if self.with_mot:
230232
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')
233235

234236
# only for pphuman
235237
self.with_skeleton_action = cfg.get(
@@ -265,6 +267,12 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
265267
if self.with_vehicleplate:
266268
print('Vehicle Plate Recognition enabled')
267269

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+
268276
self.modebase = {
269277
"framebased": False,
270278
"videobased": False,
@@ -294,7 +302,7 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
294302
model_dir, device, run_mode, batch_size, trt_min_shape,
295303
trt_max_shape, trt_opt_shape, trt_calib_mode, cpu_threads,
296304
enable_mkldnn)
297-
if self.with_attr:
305+
if self.with_human_attr:
298306
attr_cfg = self.cfg['ATTR']
299307
model_dir = attr_cfg['model_dir']
300308
batch_size = attr_cfg['batch_size']
@@ -305,8 +313,21 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
305313
trt_max_shape, trt_opt_shape, trt_calib_mode, cpu_threads,
306314
enable_mkldnn)
307315

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+
308329
else:
309-
if self.with_attr:
330+
if self.with_human_attr:
310331
attr_cfg = self.cfg['ATTR']
311332
model_dir = attr_cfg['model_dir']
312333
batch_size = attr_cfg['batch_size']
@@ -412,6 +433,19 @@ def __init__(self, args, cfg, is_video=True, multi_camera=False):
412433
basemode = vehicleplate_cfg['basemode']
413434
self.modebase[basemode] = True
414435

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+
415449
if self.with_mot or self.modebase["idbased"] or self.modebase[
416450
"skeletonbased"]:
417451
mot_cfg = self.cfg['MOT']
@@ -510,7 +544,7 @@ def predict_image(self, input):
510544
self.pipe_timer.module_time['det'].end()
511545
self.pipeline_res.update(det_res, 'det')
512546

513-
if self.with_attr:
547+
if self.with_human_attr:
514548
crop_inputs = crop_image_with_det(batch_input, det_res)
515549
attr_res_list = []
516550

@@ -528,6 +562,24 @@ def predict_image(self, input):
528562
attr_res = {'output': attr_res_list}
529563
self.pipeline_res.update(attr_res, 'attr')
530564

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+
531583
self.pipe_timer.img_num += len(batch_input)
532584
if i > self.warmup_frame:
533585
self.pipe_timer.total_time.end()
@@ -581,13 +633,14 @@ def predict_video(self, video_file):
581633
ret, frame = capture.read()
582634
if not ret:
583635
break
636+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
584637

585638
if self.modebase["idbased"] or self.modebase["skeletonbased"]:
586639
if frame_id > self.warmup_frame:
587640
self.pipe_timer.total_time.start()
588641
self.pipe_timer.module_time['mot'].start()
589642
res = self.mot_predictor.predict_image(
590-
[copy.deepcopy(frame)], visual=False)
643+
[copy.deepcopy(frame_rgb)], visual=False)
591644

592645
if frame_id > self.warmup_frame:
593646
self.pipe_timer.module_time['mot'].end()
@@ -625,14 +678,14 @@ def predict_video(self, video_file):
625678

626679
self.pipeline_res.update(mot_res, 'mot')
627680
crop_input, new_bboxes, ori_bboxes = crop_image_with_mot(
628-
frame, mot_res)
681+
frame_rgb, mot_res)
629682

630683
if self.with_vehicleplate:
631684
platelicense = self.vehicleplate_detector.get_platelicense(
632685
crop_input)
633686
self.pipeline_res.update(platelicense, 'vehicleplate')
634687

635-
if self.with_attr:
688+
if self.with_human_attr:
636689
if frame_id > self.warmup_frame:
637690
self.pipe_timer.module_time['attr'].start()
638691
attr_res = self.attr_predictor.predict_image(
@@ -641,6 +694,15 @@ def predict_video(self, video_file):
641694
self.pipe_timer.module_time['attr'].end()
642695
self.pipeline_res.update(attr_res, 'attr')
643696

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+
644706
if self.with_idbased_detaction:
645707
if frame_id > self.warmup_frame:
646708
self.pipe_timer.module_time['det_action'].start()
@@ -708,7 +770,7 @@ def predict_video(self, video_file):
708770

709771
if self.with_mtmct and frame_id % 10 == 0:
710772
crop_input, img_qualities, rects = self.reid_predictor.crop_image_with_mot(
711-
frame, mot_res)
773+
frame_rgb, mot_res)
712774
if frame_id > self.warmup_frame:
713775
self.pipe_timer.module_time['reid'].start()
714776
reid_res = self.reid_predictor.predict_batch(crop_input)
@@ -740,7 +802,7 @@ def predict_video(self, video_file):
740802
# collect frames
741803
if frame_id % sample_freq == 0:
742804
# Scale image
743-
scaled_img = scale(frame)
805+
scaled_img = scale(frame_rgb)
744806
video_action_imgs.append(scaled_img)
745807

746808
# the number of collected frames is enough to predict video action
@@ -820,11 +882,18 @@ def visualize_video(self,
820882
records=records,
821883
center_traj=center_traj)
822884

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:
825894
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)
828897
image = np.array(image)
829898

830899
vehicleplate_res = result.get('vehicleplate')
@@ -883,7 +952,9 @@ def visualize_video(self,
883952
def visualize_image(self, im_files, images, result):
884953
start_idx, boxes_num_i = 0, 0
885954
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+
887958
for i, (im_file, im) in enumerate(zip(im_files, images)):
888959
if det_res is not None:
889960
det_res_i = {}
@@ -897,10 +968,15 @@ def visualize_image(self, im_files, images, result):
897968
threshold=self.cfg['crop_thresh'])
898969
im = np.ascontiguousarray(np.copy(im))
899970
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+
904980
img_name = os.path.split(im_file)[-1]
905981
if not os.path.exists(self.output_dir):
906982
os.makedirs(self.output_dir)

0 commit comments

Comments
 (0)