wangrong
2025-01-03 2c4b8838f89bebca97615c5f0bac2c58e0349771
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
#ifdef VP_WITH_TRT
#include "vp_trt_vehicle_feature_encoder.h"
 
namespace vp_nodes {
    vp_trt_vehicle_feature_encoder::vp_trt_vehicle_feature_encoder(std::string node_name, 
                                                                    std::string vehicle_feature_model_path, 
                                                                    std::vector<int> p_class_ids_applied_to,
                                                                    int min_width_applied_to, int min_height_applied_to):
                                                                    vp_secondary_infer_node(node_name, "", "", "", 1, 1, 1, p_class_ids_applied_to, min_width_applied_to, min_height_applied_to) {
        vehicle_feature_encoder = std::make_shared<trt_vehicle::VehicleFeatureEncoder>(vehicle_feature_model_path);
        this->initialized();
    }
    
    vp_trt_vehicle_feature_encoder::~vp_trt_vehicle_feature_encoder() {
        deinitialized();
    }
 
    void vp_trt_vehicle_feature_encoder::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);
 
        // infer using trt_vehicle library
        start_time = std::chrono::system_clock::now();
        std::vector<std::vector<float>> vehicles_feature;
        vehicle_feature_encoder->encode(mats_to_infer, vehicles_feature);
 
        auto& frame_meta = frame_meta_with_batch[0];
        auto index = 0;
        for (int i = 0; i < vehicles_feature.size(); i++) {
            for (int j = index; j < frame_meta->targets.size(); j++) {
                // need apply or not?
                if (!need_apply(frame_meta->targets[j]->primary_class_id, frame_meta->targets[j]->width, frame_meta->targets[j]->height)) {
                    // continue as its primary_class_id is not in p_class_ids_applied_to
                    continue;
                }
 
                // update back to frame meta
                auto& features = vehicles_feature[i];
                for(int k = 0; k < features.size(); k++) {
                    frame_meta->targets[j]->embeddings.push_back(features[k]);
                }
 
                // break as we found the right target!
                index = j + 1;
                break;
            }
        }
        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);
    }
 
    void vp_trt_vehicle_feature_encoder::postprocess(const std::vector<cv::Mat>& raw_outputs, const std::vector<std::shared_ptr<vp_objects::vp_frame_meta>>& frame_meta_with_batch) {
 
    }
}
#endif