环境:opencv3.3+VS2017+C++
我们讲述了如何实时的读取视频中每一帧的数据,以及基于mobilenet如何进行对象的检测,嘿嘿,如果两个流程组合起来,会怎么样呢??我们来看一下咯~
1、前期准备
前面的文章中已经很详细的说过依赖包引用以及模型位置定义,这里不再赘述!
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
using namespace cv;
using namespace cv::dnn;
using namespace std;
const size_t width = 300;
const size_t height = 300;
const float scaleFector = 0.007843f;
const float meanVal = 127.5;
//cv::String labelFile = "D:/new_cv/opencv/sources/samples/data/dnn/model_ssd_det/label.txt";
cv::String model_bin_file = "D:/new_cv/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.caffemodel";
cv::String model_txt_file = "D:/new_cv/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.prototxt";
const char* classNames[] = { "background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor" };
2、视频文件加载并读取
这部分内容和上一篇博客内容相近,可以简单简单看一下,如果不懂,随时一起讨论学习!
VideoCapture capture;
capture.open("D:/new_cv/opencv/sources/samples/data/vtest.avi");
if (!capture.isOpened())
{
cout << "can not open video test" << endl;
return -1;
}
Mat src;
while (capture.read(src))
{
imshow("src", src);
char stp = waitKey(100);
if (stp == 27)
{
break;
}
}
waitKey(0);
return 0;
3,实时检测部分
为了解耦,方便独立调用,我将检测部分封装成了一个独立的模块,
Mat detect_from_video(Mat &src)
{
if (src.empty())
{
cout << "can not open file" << endl;
exit(-1);
}
Net net;//初始化网络
net = readNetFromCaffe(model_video_txt_file, model_video_bin_file);
if (net.empty())
{
cout << "init the model net error";
exit(-1);
}
Mat blobimg = blobFromImage(src, scaleFector, Size(300, 300), meanVal);
net.setInput(blobimg, "data");
Mat detection = net.forward("detection_out");//接收输出层结果
//cout << detection.size << endl;
//cout << detection.size[2]<<" "<< detection.size[3] << endl;
// Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.at<float>());//注意是ptr不是at
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());//声明矩阵接收结果
const float confidence_threshold = 0.25;//定义置信度阈值
for (int i = 0; i < detectionMat.rows; i++)
{
float detect_confidence = detectionMat.at<float>(i, 2);
if (detect_confidence > confidence_threshold)//选择符合条件的结果,可能有多个圈
{
size_t det_index = (size_t)detectionMat.at<float>(i, 1);
float x1 = detectionMat.at<float>(i, 3)*src.cols;
float y1 = detectionMat.at<float>(i, 4)*src.rows;
float x2 = detectionMat.at<float>(i, 5)*src.cols;
float y2 = detectionMat.at<float>(i, 6)*src.rows;
Rect rec((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
rectangle(src,rec, Scalar(1, 1, 2), 2, 8, 0);
putText(src, format("%s", class_video_Names[det_index]), Point(x1, y1) ,FONT_HERSHEY_SIMPLEX,1.0, Scalar(1, 2, 3), 2, 8, 0);
imshow("src", src);
}
}
return src;
}
如果模型足够好,效果会有很多的!(动态实时的呦!!!)
左边有我微信,大家可以一起学习,一起进步!坚持一件事很难,但坚持下来一定很酷!^_^