1.class Detector数据成员
private:
std::shared_ptr<void> detector_gpu_ptr;//智能指针
std::deque<std::vector<bbox_t>> prev_bbox_vec_deque;//deque(双端队列)是由一段一段的定量连续空间构成,可以向两端发展,因此不论在尾部或头部安插元素都十分迅速。 在中间部分安插元素则比较费时,因为必须移动其它元素。
public:
const int cur_gpu_id; //当前GPUID
float nms = .4;
bool wait_stream;
2.分析detect的参数和用到的对象的数据成员
3.在新project中首先引进darknet自身的库文件,然后直接创建main()函数,在main()外部拷贝过来Detector的detect函数,传参以使其可以执行。
4.调用函数3 的源代码定义在---------》---------》---------》---------》---------》---------》 yolo_cpp_dll.sln
下
调用函数0
LIB_API std::vector<bbox_t> Detector::detect(std::string image_filename, float thresh, bool use_mean)
{
std::shared_ptr<image_t> image_ptr(new image_t, [](image_t *img) { if (img->data) free(img->data); delete img; });
*image_ptr = load_image(image_filename);
return detect(*image_ptr, thresh, use_mean);
}
调用函数1
std::vector<bbox_t> detect(cv::Mat mat, float thresh = 0.2, bool use_mean = false)
{
if(mat.data == NULL)
throw std::runtime_error("图像是空的");
auto image_ptr = mat_to_image_resize(mat);
return detect_resized(*image_ptr, mat.cols, mat.rows, thresh, use_mean);
}
调用函数2
std::vector<bbox_t> detect_resized(image_t img, int init_w, int init_h, float thresh = 0.2, bool use_mean = false)
{
if (img.data == NULL)
throw std::runtime_error("Image is empty");
auto detection_boxes = detect(img, thresh, use_mean);
float wk = (float)init_w / img.w, hk = (float)init_h / img.h;
for (auto &i : detection_boxes) i.x *= wk, i.w *= wk, i.y *= hk, i.h *= hk;
return detection_boxes;
}
调用函数3
LIB_API std::vector<bbox_t> Detector::detect(image_t img, float thresh, bool use_mean)
{
detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
network &net = detector_gpu.net;
#ifdef GPU
int old_gpu_index;
cudaGetDevice(&old_gpu_index);
if(cur_gpu_id != old_gpu_index)
cudaSetDevice(net.gpu_index);
net.wait_stream = wait_stream; // 1 - wait CUDA-stream, 0 - not to wait
#endif
//std::cout << "net.gpu_index = " << net.gpu_index << std::endl;
image im;
im.c = img.c;
im.data = img.data;
im.h = img.h;
im.w = img.w;
image sized;
if (net.w == im.w && net.h == im.h) {
sized = make_image(im.w, im.h, im.c);
memcpy(sized.data, im.data, im.w*im.h*im.c * sizeof(float));//内存间数据的复制
}
else
sized = resize_image(im, net.w, net.h);
layer l = net.layers[net.n - 1];
float *X = sized.data;//图像数据的获取
float *prediction = network_predict(net, X);//网络信息的正向传递
if (use_mean) {
memcpy(detector_gpu.predictions[detector_gpu.demo_index], prediction, l.outputs * sizeof(float));
mean_arrays(detector_gpu.predictions, NFRAMES, l.outputs, detector_gpu.avg);
l.output = detector_gpu.avg;
detector_gpu.demo_index = (detector_gpu.demo_index + 1) % NFRAMES;
}
//get_region_boxes(l, 1, 1, thresh, detector_gpu.probs, detector_gpu.boxes, 0, 0);
//if (nms) do_nms_sort(detector_gpu.boxes, detector_gpu.probs, l.w*l.h*l.n, l.classes, nms);
int nboxes = 0;
int letterbox = 0;
float hier_thresh = 0.5;
detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox);//获得检测框
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);//进行NMS排序
std::vector<bbox_t> bbox_vec;
for (int i = 0; i < nboxes; ++i) {
box b = dets[i].bbox;
int const obj_id = max_index(dets[i].prob, l.classes);
float const prob = dets[i].prob[obj_id];
if (prob > thresh)//将大于阈值的保存进新数组中
{
bbox_t bbox;
bbox.x = std::max((double)0, (b.x - b.w / 2.)*im.w);
bbox.y = std::max((double)0, (b.y - b.h / 2.)*im.h);
bbox.w = b.w*im.w;
bbox.h = b.h*im.h;
bbox.obj_id = obj_id;
bbox.prob = prob;
bbox.track_id = 0;
bbox.frames_counter = 0;
bbox.x_3d = NAN;
bbox.y_3d = NAN;
bbox.z_3d = NAN;
bbox_vec.push_back(bbox);
}
}
free_detections(dets, nboxes);//释放掉结构体数组中包含的指针
if(sized.data)//释放掉图像结构体中的指针
free(sized.data);
#ifdef GPU
if (cur_gpu_id != old_gpu_index)
cudaSetDevice(old_gpu_index);
#endif
return bbox_vec;
}