#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("pre");
|
|
_p_net->doInference(trtInput.data, vec_ds_images.size());
|
timer.reset();
|
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("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 == SMALL)
|
_p_net = std::unique_ptr<Detecter>{new Detecter(_info,_infer_param,2)};
|
else{
|
_p_net = std::unique_ptr<Detecter>{new Detecter(_info,_infer_param,1)};
|
}
|
}
|