派生自 Algorithm/baseDetector

Scheaven
2021-09-08 9b1532d86c2cf48a63017f3460897d8d14b98b60
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "detector.h"
 
 
Detector::Detector()
{
 
}
 
Detector::~Detector()
{
 
}
 
void Detector::init(const Config &config)
{
    this->_config = config;
    this->set_gpu_id(_config.gpu_id);
    this->parse_config();
    this->build_net();
}
 
void Detector::detect(const std::vector<cv::Mat> &vec_image, std::vector<BatchResult> &vec_batch_result)
{
    Timer timer;
    std::vector<DsImage> vec_ds_images;
    vec_batch_result.clear();
    vec_batch_result.resize(vec_image.size());
    // std::cout << _p_net->getInputW()<< ":"<<  _p_net->getInputH()<< std::endl;
    for (const auto &img:vec_image)
    {
        vec_ds_images.emplace_back(img, _vec_net_type[_config.net_type], _p_net->getInputH(), _p_net->getInputW());
    }
    cv::Mat trtInput = blobFromDsImages(vec_ds_images, _p_net->getInputH(),_p_net->getInputW());
    timer.out("eve pre detect ");
 
    timer.reset();
    _p_net->doInference(trtInput.data, vec_ds_images.size());
    for (uint32_t i = 0; i < vec_ds_images.size(); ++i)
    {
        auto curImage = vec_ds_images.at(i);
        auto binfo = _p_net->decodeDetections(i, curImage.getImageHeight(), curImage.getImageWidth());
        // exit(0);
        std::cout << binfo.size()<<":binfo"<<std::endl;
        auto remaining = nmsAllClasses(_p_net->getNMSThresh(),
            binfo,
            _p_net->getNumClasses(),
            _vec_net_type[_config.net_type]);
        if (remaining.empty())
        {
            continue;
        }
        std::vector<Result> vec_result(0);
        for (const auto &b : remaining)
        {
            Result res;
            res.id = b.label;
            res.prob = b.prob;
            // std::cout << "b.prob: " << b.prob << std::endl;
            const int x = b.box.x1;
            const int y = b.box.y1;
            const int w = b.box.x2 - b.box.x1;
            const int h = b.box.y2 - b.box.y1;
            res.rect = cv::Rect(x, y, w, h);
            vec_result.push_back(res);
        }
        vec_batch_result[i] = vec_result;
    }
    timer.out("eve pre detect post");
    DEBUG("--detect over--" );
}
 
void Detector::set_gpu_id(const int id)
{
    cudaError_t status = cudaSetDevice(id);
    if (status != cudaSuccess)
    {
        /* code */
        DEBUG((boost::format("gpu id: %d not exist !" )%id).str());
        assert(0);
    }
}
 
void Detector::parse_config()
{
    _info.precision = _vec_precision[_config.inference_precison];
    _info.deviceType = "kGPU";
    _info.inputBlobName = "data";
    _infer_param.printPerfInfo = false;
    _infer_param.printPredictionInfo = false;
    _infer_param.calibImagesPath = "";
    _infer_param.probThresh = _config.detect_thresh;
    _infer_param.nmsThresh = 0.5;
}
 
void Detector::build_net()
{
    if(_config.net_type == COMMON)
        _p_net = std::unique_ptr<Detecter>{new Detecter(_info,_infer_param,1)};
    else{
        _p_net = std::unique_ptr<Detecter>{new Detecter(_info,_infer_param,2)};
    }
}