diff --git a/deploy/pphuman/ppvehicle/vehicle_plate.py b/deploy/pphuman/ppvehicle/vehicle_plate.py index 94415a830e169b803113dda828a90edb8ff60b8b..8d26b4fb19a496d4d933bdbbb5e90908cb6b93da 100644 --- a/deploy/pphuman/ppvehicle/vehicle_plate.py +++ b/deploy/pphuman/ppvehicle/vehicle_plate.py @@ -24,15 +24,14 @@ import math import paddle import sys -# add deploy path of PadleDetection to sys.path -# add deploy path of PadleDetection to sys.path parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 3))) sys.path.insert(0, parent_path) -from python.infer import get_test_images, print_arguments -from pphuman.ppvehicle.vehicle_plateutils import create_predictor, get_infer_gpuid, get_rotate_crop_image, draw_boxes, argsparser -from pphuman.ppvehicle.vecplatepostprocess import build_post_process +from python.infer import get_test_images from python.preprocess import preprocess, NormalizeImage, Permute, Resize_Mult32 +from pphuman.ppvehicle.vehicle_plateutils import create_predictor, get_infer_gpuid, get_rotate_crop_image, draw_boxes +from pphuman.ppvehicle.vehicleplate_postprocess import build_post_process +from pphuman.pipe_utils import merge_cfg, print_arguments, argsparser class PlateDetector(object): @@ -155,36 +154,36 @@ class TextRecognizer(object): self.rec_batch_num = cfg['rec_batch_num'] self.rec_algorithm = cfg['rec_algorithm'] word_dict_path = cfg['word_dict_path'] - isuse_space_char = True + use_space_char = True postprocess_params = { 'name': 'CTCLabelDecode', "character_dict_path": word_dict_path, - "use_space_char": isuse_space_char + "use_space_char": use_space_char } if self.rec_algorithm == "SRN": postprocess_params = { 'name': 'SRNLabelDecode', "character_dict_path": word_dict_path, - "use_space_char": isuse_space_char + "use_space_char": use_space_char } elif self.rec_algorithm == "RARE": postprocess_params = { 'name': 'AttnLabelDecode', "character_dict_path": word_dict_path, - "use_space_char": isuse_space_char + "use_space_char": use_space_char } elif self.rec_algorithm == 'NRTR': postprocess_params = { 'name': 'NRTRLabelDecode', "character_dict_path": word_dict_path, - "use_space_char": isuse_space_char + "use_space_char": use_space_char } elif self.rec_algorithm == "SAR": postprocess_params = { 'name': 'SARLabelDecode', "character_dict_path": word_dict_path, - "use_space_char": isuse_space_char + "use_space_char": use_space_char } self.postprocess_op = build_post_process(postprocess_params) self.predictor, self.input_tensor, self.output_tensors, self.config = \ @@ -520,22 +519,24 @@ class PlateRecognizer(object): def main(): - detector = PlateRecognizer(FLAGS) + cfg = merge_cfg(FLAGS) + print_arguments(cfg) + vehicleplate_cfg = cfg['VEHICLE_PLATE'] + detector = PlateRecognizer(FLAGS, vehicleplate_cfg) # predict from image img_list = get_test_images(FLAGS.image_dir, FLAGS.image_file) for img in img_list: image = cv2.imread(img) results = detector.get_platelicense([image]) + print(results) if __name__ == '__main__': paddle.enable_static() parser = argsparser() FLAGS = parser.parse_args() - print_arguments(FLAGS) FLAGS.device = FLAGS.device.upper() assert FLAGS.device in ['CPU', 'GPU', 'XPU' ], "device should be CPU, GPU or XPU" - # assert not FLAGS.use_gpu, "use_gpu has been deprecated, please use --device" main() diff --git a/deploy/pphuman/ppvehicle/vehicle_plateutils.py b/deploy/pphuman/ppvehicle/vehicle_plateutils.py index 95185102b632dcd9a975af3264e92a5cc3303ba4..c4c7cf2b26fba7388806fe25b91420d47e8a4682 100644 --- a/deploy/pphuman/ppvehicle/vehicle_plateutils.py +++ b/deploy/pphuman/ppvehicle/vehicle_plateutils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -26,106 +26,6 @@ import time import ast -def argsparser(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - "--config", type=str, default=None, help=("Path of configure")) - parser.add_argument("--det_algorithm", type=str, default='DB') - parser.add_argument("--det_model_dir", type=str) - parser.add_argument("--det_limit_side_len", type=float, default=960) - parser.add_argument("--det_limit_type", type=str, default='max') - parser.add_argument("--rec_algorithm", type=str, default='SVTR_LCNet') - parser.add_argument("--rec_model_dir", type=str) - parser.add_argument("--rec_image_shape", type=str, default="3, 48, 320") - parser.add_argument("--rec_batch_num", type=int, default=6) - parser.add_argument( - "--word_dict_path", - type=str, - default="deploy/pphuman/ppvehicle/rec_word_dict.txt") - parser.add_argument( - "--image_file", type=str, default=None, help="Path of image file.") - parser.add_argument( - "--image_dir", - type=str, - default=None, - help="Dir of image file, `image_file` has a higher priority.") - parser.add_argument( - "--video_file", - type=str, - default=None, - help="Path of video file, `video_file` or `camera_id` has a highest priority." - ) - parser.add_argument( - "--video_dir", - type=str, - default=None, - help="Dir of video file, `video_file` has a higher priority.") - parser.add_argument( - "--model_dir", nargs='*', help="set model dir in pipeline") - parser.add_argument( - "--camera_id", - type=int, - default=-1, - help="device id of camera to predict.") - parser.add_argument( - "--output_dir", - type=str, - default="output", - help="Directory of output visualization files.") - parser.add_argument( - "--run_mode", - type=str, - default='paddle', - help="mode of running(paddle/trt_fp32/trt_fp16/trt_int8)") - parser.add_argument( - "--device", - type=str, - default='cpu', - help="Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU." - ) - parser.add_argument( - "--enable_mkldnn", - type=ast.literal_eval, - default=False, - help="Whether use mkldnn with CPU.") - parser.add_argument( - "--cpu_threads", type=int, default=1, help="Num of threads with CPU.") - parser.add_argument( - "--trt_min_shape", type=int, default=1, help="min_shape for TensorRT.") - parser.add_argument( - "--trt_max_shape", - type=int, - default=1280, - help="max_shape for TensorRT.") - parser.add_argument( - "--trt_opt_shape", - type=int, - default=640, - help="opt_shape for TensorRT.") - parser.add_argument( - "--trt_calib_mode", - type=bool, - default=False, - help="If the model is produced by TRT offline quantitative " - "calibration, trt_calib_mode need to set True.") - parser.add_argument( - "--do_entrance_counting", - action='store_true', - help="Whether counting the numbers of identifiers entering " - "or getting out from the entrance. Note that only support one-class" - "counting, multi-class counting is coming soon.") - parser.add_argument( - "--secs_interval", - type=int, - default=2, - help="The seconds interval to count after tracking") - parser.add_argument( - "--draw_center_traj", - action='store_true', - help="Whether drawing the trajectory of center") - return parser - - def create_predictor(args, cfg, mode): if mode == "det": model_dir = cfg['det_model_dir'] @@ -169,87 +69,86 @@ def create_predictor(args, cfg, mode): precision_mode=precision_map[args.run_mode], use_static=False, use_calib_mode=trt_calib_mode) - - # skip the minmum trt subgraph - use_dynamic_shape = True - if mode == "det": - min_input_shape = { - "x": [1, 3, 50, 50], - "conv2d_92.tmp_0": [1, 120, 20, 20], - "conv2d_91.tmp_0": [1, 24, 10, 10], - "conv2d_59.tmp_0": [1, 96, 20, 20], - "nearest_interp_v2_1.tmp_0": [1, 256, 10, 10], - "nearest_interp_v2_2.tmp_0": [1, 256, 20, 20], - "conv2d_124.tmp_0": [1, 256, 20, 20], - "nearest_interp_v2_3.tmp_0": [1, 64, 20, 20], - "nearest_interp_v2_4.tmp_0": [1, 64, 20, 20], - "nearest_interp_v2_5.tmp_0": [1, 64, 20, 20], - "elementwise_add_7": [1, 56, 2, 2], - "nearest_interp_v2_0.tmp_0": [1, 256, 2, 2] - } - max_input_shape = { - "x": [1, 3, 1536, 1536], - "conv2d_92.tmp_0": [1, 120, 400, 400], - "conv2d_91.tmp_0": [1, 24, 200, 200], - "conv2d_59.tmp_0": [1, 96, 400, 400], - "nearest_interp_v2_1.tmp_0": [1, 256, 200, 200], - "conv2d_124.tmp_0": [1, 256, 400, 400], - "nearest_interp_v2_2.tmp_0": [1, 256, 400, 400], - "nearest_interp_v2_3.tmp_0": [1, 64, 400, 400], - "nearest_interp_v2_4.tmp_0": [1, 64, 400, 400], - "nearest_interp_v2_5.tmp_0": [1, 64, 400, 400], - "elementwise_add_7": [1, 56, 400, 400], - "nearest_interp_v2_0.tmp_0": [1, 256, 400, 400] - } - opt_input_shape = { - "x": [1, 3, 640, 640], - "conv2d_92.tmp_0": [1, 120, 160, 160], - "conv2d_91.tmp_0": [1, 24, 80, 80], - "conv2d_59.tmp_0": [1, 96, 160, 160], - "nearest_interp_v2_1.tmp_0": [1, 256, 80, 80], - "nearest_interp_v2_2.tmp_0": [1, 256, 160, 160], - "conv2d_124.tmp_0": [1, 256, 160, 160], - "nearest_interp_v2_3.tmp_0": [1, 64, 160, 160], - "nearest_interp_v2_4.tmp_0": [1, 64, 160, 160], - "nearest_interp_v2_5.tmp_0": [1, 64, 160, 160], - "elementwise_add_7": [1, 56, 40, 40], - "nearest_interp_v2_0.tmp_0": [1, 256, 40, 40] - } - min_pact_shape = { - "nearest_interp_v2_26.tmp_0": [1, 256, 20, 20], - "nearest_interp_v2_27.tmp_0": [1, 64, 20, 20], - "nearest_interp_v2_28.tmp_0": [1, 64, 20, 20], - "nearest_interp_v2_29.tmp_0": [1, 64, 20, 20] - } - max_pact_shape = { - "nearest_interp_v2_26.tmp_0": [1, 256, 400, 400], - "nearest_interp_v2_27.tmp_0": [1, 64, 400, 400], - "nearest_interp_v2_28.tmp_0": [1, 64, 400, 400], - "nearest_interp_v2_29.tmp_0": [1, 64, 400, 400] - } - opt_pact_shape = { - "nearest_interp_v2_26.tmp_0": [1, 256, 160, 160], - "nearest_interp_v2_27.tmp_0": [1, 64, 160, 160], - "nearest_interp_v2_28.tmp_0": [1, 64, 160, 160], - "nearest_interp_v2_29.tmp_0": [1, 64, 160, 160] - } - min_input_shape.update(min_pact_shape) - max_input_shape.update(max_pact_shape) - opt_input_shape.update(opt_pact_shape) - elif mode == "rec": - imgH = int(cfg['rec_image_shape'][-2]) - min_input_shape = {"x": [1, 3, imgH, 10]} - max_input_shape = {"x": [batch_size, 3, imgH, 2304]} - opt_input_shape = {"x": [batch_size, 3, imgH, 320]} - elif mode == "cls": - min_input_shape = {"x": [1, 3, 48, 10]} - max_input_shape = {"x": [batch_size, 3, 48, 1024]} - opt_input_shape = {"x": [batch_size, 3, 48, 320]} - else: - use_dynamic_shape = False - if use_dynamic_shape: - config.set_trt_dynamic_shape_info(min_input_shape, max_input_shape, - opt_input_shape) + use_dynamic_shape = True + + if mode == "det": + min_input_shape = { + "x": [1, 3, 50, 50], + "conv2d_92.tmp_0": [1, 120, 20, 20], + "conv2d_91.tmp_0": [1, 24, 10, 10], + "conv2d_59.tmp_0": [1, 96, 20, 20], + "nearest_interp_v2_1.tmp_0": [1, 256, 10, 10], + "nearest_interp_v2_2.tmp_0": [1, 256, 20, 20], + "conv2d_124.tmp_0": [1, 256, 20, 20], + "nearest_interp_v2_3.tmp_0": [1, 64, 20, 20], + "nearest_interp_v2_4.tmp_0": [1, 64, 20, 20], + "nearest_interp_v2_5.tmp_0": [1, 64, 20, 20], + "elementwise_add_7": [1, 56, 2, 2], + "nearest_interp_v2_0.tmp_0": [1, 256, 2, 2] + } + max_input_shape = { + "x": [1, 3, 1536, 1536], + "conv2d_92.tmp_0": [1, 120, 400, 400], + "conv2d_91.tmp_0": [1, 24, 200, 200], + "conv2d_59.tmp_0": [1, 96, 400, 400], + "nearest_interp_v2_1.tmp_0": [1, 256, 200, 200], + "conv2d_124.tmp_0": [1, 256, 400, 400], + "nearest_interp_v2_2.tmp_0": [1, 256, 400, 400], + "nearest_interp_v2_3.tmp_0": [1, 64, 400, 400], + "nearest_interp_v2_4.tmp_0": [1, 64, 400, 400], + "nearest_interp_v2_5.tmp_0": [1, 64, 400, 400], + "elementwise_add_7": [1, 56, 400, 400], + "nearest_interp_v2_0.tmp_0": [1, 256, 400, 400] + } + opt_input_shape = { + "x": [1, 3, 640, 640], + "conv2d_92.tmp_0": [1, 120, 160, 160], + "conv2d_91.tmp_0": [1, 24, 80, 80], + "conv2d_59.tmp_0": [1, 96, 160, 160], + "nearest_interp_v2_1.tmp_0": [1, 256, 80, 80], + "nearest_interp_v2_2.tmp_0": [1, 256, 160, 160], + "conv2d_124.tmp_0": [1, 256, 160, 160], + "nearest_interp_v2_3.tmp_0": [1, 64, 160, 160], + "nearest_interp_v2_4.tmp_0": [1, 64, 160, 160], + "nearest_interp_v2_5.tmp_0": [1, 64, 160, 160], + "elementwise_add_7": [1, 56, 40, 40], + "nearest_interp_v2_0.tmp_0": [1, 256, 40, 40] + } + min_pact_shape = { + "nearest_interp_v2_26.tmp_0": [1, 256, 20, 20], + "nearest_interp_v2_27.tmp_0": [1, 64, 20, 20], + "nearest_interp_v2_28.tmp_0": [1, 64, 20, 20], + "nearest_interp_v2_29.tmp_0": [1, 64, 20, 20] + } + max_pact_shape = { + "nearest_interp_v2_26.tmp_0": [1, 256, 400, 400], + "nearest_interp_v2_27.tmp_0": [1, 64, 400, 400], + "nearest_interp_v2_28.tmp_0": [1, 64, 400, 400], + "nearest_interp_v2_29.tmp_0": [1, 64, 400, 400] + } + opt_pact_shape = { + "nearest_interp_v2_26.tmp_0": [1, 256, 160, 160], + "nearest_interp_v2_27.tmp_0": [1, 64, 160, 160], + "nearest_interp_v2_28.tmp_0": [1, 64, 160, 160], + "nearest_interp_v2_29.tmp_0": [1, 64, 160, 160] + } + min_input_shape.update(min_pact_shape) + max_input_shape.update(max_pact_shape) + opt_input_shape.update(opt_pact_shape) + elif mode == "rec": + imgH = int(cfg['rec_image_shape'][-2]) + min_input_shape = {"x": [1, 3, imgH, 10]} + max_input_shape = {"x": [batch_size, 3, imgH, 2304]} + opt_input_shape = {"x": [batch_size, 3, imgH, 320]} + elif mode == "cls": + min_input_shape = {"x": [1, 3, 48, 10]} + max_input_shape = {"x": [batch_size, 3, 48, 1024]} + opt_input_shape = {"x": [batch_size, 3, 48, 320]} + else: + use_dynamic_shape = False + if use_dynamic_shape: + config.set_trt_dynamic_shape_info( + min_input_shape, max_input_shape, opt_input_shape) else: config.disable_gpu() diff --git a/deploy/pphuman/ppvehicle/vecplatepostprocess.py b/deploy/pphuman/ppvehicle/vehicleplate_postprocess.py similarity index 100% rename from deploy/pphuman/ppvehicle/vecplatepostprocess.py rename to deploy/pphuman/ppvehicle/vehicleplate_postprocess.py