###代码
在访问速率优化上,采用指针形式
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::dnn;
String objNames[] = { "background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor" };
int main(void) {
string bin_model = "/work/opencv_dnn/ssd/MobileNetSSD_deploy.caffemodel";
string protxt = "/work/opencv_dnn/ssd/MobileNetSSD_deploy.prototxt";
// load network model
Net net = readNetFromCaffe(protxt, bin_model);
// 设置计算后台
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
namedWindow("检测画面",0);
resizeWindow("检测画面",800,600);
// 获取各层信息
vector<string> layer_names = net.getLayerNames();
for (int i = 0; i < layer_names.size(); i++) {
int id = net.getLayerId(layer_names[i]);
auto layer = net.getLayer(id);
printf("layer id : %d, type : %s, name : %s \n", id, layer->type.c_str(), layer->name.c_str());
}
VideoCapture capture;
capture.open("/work/opencv_video/carcar.mp4");
Mat frame;
while (true) {
bool ret = capture.read(frame);
if (!ret) break;
// 构建输入
Mat blob = blobFromImage(frame, 0.007843, Size(300, 300), Scalar(127.5, 127.5, 127.5), false, false);
net.setInput(blob, "data");
// 执行推理
Mat detection = net.forward("detection_out");
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
float confidence_threshold = 0.5;
// 解析输出数据
for (int i = 0; i < detectionMat.rows; i++) {
float* curr_row = detectionMat.ptr<float>(i);
int image_id = (int)(*curr_row++);
size_t objIndex = (size_t)(*curr_row++);
float score = *curr_row++;
if (score > confidence_threshold) {
float tl_x = (*curr_row++) * frame.cols;
float tl_y = (*curr_row++) * frame.rows;
float br_x = (*curr_row++) * frame.cols;
float br_y = (*curr_row++) * frame.rows;
Rect box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
putText(frame, objNames[objIndex].c_str(), box.tl(), FONT_HERSHEY_SIMPLEX, 1.5, Scalar(0, 255, 0), 3, 8);
}
}
// measure time consume
vector<double> layersTimings;
double freq = getTickFrequency() / 1000.0;
double time = net.getPerfProfile(layersTimings) / freq;
ostringstream ss;
ss << "FPS: " << 1000 / time << " ; time : " << time << " ms";
// show
putText(frame, ss.str(), Point(20, 20), FONT_HERSHEY_PLAIN, 1.0, Scalar(255, 0, 0), 2, 8);
imshow("检测画面", frame);
char c = waitKey(1);
if (c == 27) { // ESC
break;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200210200547175.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3o5NjE5Njg1NDk=,size_16,color_FFFFFF,t_70)
// 释放资源
capture.release();
waitKey(0);
destroyAllWindows();
return 0;
}