wangrong
2025-03-19 291afb9c81aae1d15d9e3edcea618d0a2e1fc5a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifdef VP_WITH_TRT
#include "vp_trt_vehicle_plate_detector.h"
 
namespace vp_nodes {
    
    vp_trt_vehicle_plate_detector::vp_trt_vehicle_plate_detector(std::string node_name, 
                                                                std::string plate_det_model_path, 
                                                                std::string char_rec_model_path):
                                                                vp_secondary_infer_node(node_name, "") {
        plate_detector = std::make_shared<trt_vehicle::VehiclePlateDetector>(plate_det_model_path, char_rec_model_path);
        this->initialized();
    }
    
    vp_trt_vehicle_plate_detector::~vp_trt_vehicle_plate_detector() {
        deinitialized();
    }
 
    void vp_trt_vehicle_plate_detector::postprocess(const std::vector<cv::Mat>& raw_outputs, const std::vector<std::shared_ptr<vp_objects::vp_frame_meta>>& frame_meta_with_batch) {
 
    }
 
    // please refer to vp_infer_node::run_infer_combinations
    void vp_trt_vehicle_plate_detector::run_infer_combinations(const std::vector<std::shared_ptr<vp_objects::vp_frame_meta>>& frame_meta_with_batch) {
        assert(frame_meta_with_batch.size() == 1);
        std::vector<cv::Mat> mats_to_infer;
 
        // start
        auto start_time = std::chrono::system_clock::now();
 
        // prepare data, as same as base class
        vp_secondary_infer_node::prepare(frame_meta_with_batch, mats_to_infer);
        auto prepare_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start_time);
 
        start_time = std::chrono::system_clock::now();
        // here we detect vehicle plate for all target(like car/bus) in frame
        std::vector<std::vector<trt_vehicle::Plate>> plates;
        plate_detector->detect(mats_to_infer, plates, true);  // detect at most 1 plate for each image
 
        auto& frame_meta = frame_meta_with_batch[0];
        for (int i = 0; i < plates.size(); i++) {
            for (int j = 0; j < plates[i].size(); j++) {
                auto& plate = plates[i][j];
 
                // only plate detection but no recognition result
                if (plate.text.empty()) {
                    continue;
                }
                
                // check value range
                auto x = std::max(0, plate.x + frame_meta->targets[i]->x - 10);  // offset
                auto y = std::max(0, plate.y + frame_meta->targets[i]->y - 8);   // offset
                auto w = std::min(plate.width, frame_meta->frame.cols - x);
                auto h = std::min(plate.height, frame_meta->frame.rows - y);
                if (w <= 0 || h <=0) {
                    continue;
                }
                
                // create sub target and update back into frame meta
                // we treat vehicle plate as sub target of those in vp_frame_meta.targets
                auto sub_target = std::make_shared<vp_objects::vp_sub_target>(x, y, w, h, 
                                                    -1, 0, plate.color + "_" + plate.text, frame_meta->frame_index, frame_meta->channel_index);
                frame_meta->targets[i]->sub_targets.push_back(sub_target);
            }
        }
        auto infer_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start_time);
 
        // can not calculate preprocess time and postprocess time, set 0 by default.
        vp_infer_node::infer_combinations_time_cost(mats_to_infer.size(), prepare_time.count(), 0, infer_time.count(), 0);
    }
}
#endif