Opencv3.3版本以上使用SSD模型实现实时摄像头对象检测

时间:2024-03-31 11:22:11

环境:

1、安装opencv3.3以上,因为3.3以上才有dnn模块。目前opencv已经更新到opencv4.0.0-alpha,下面网站对更新日志一目了然:

https://github.com/opencv/opencv/wiki/ChangeLog#version400

2、如果是window系统最好安装vs2015或者vs2017,因为vs2013已经不支持vc14,具体怎么搭建环境就不说了,网上一大把。当然在Ubuntu上也可以,并且实现起来更流畅。

模型:

模型用的是caffe的SSD,还需要一个prototxt文件,该文件记录分类标签以及模型的结构。路径就在安装的opencv路径里,我的是:

SSD路径:E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.caffemodel

prototxt路径:E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.prototxt

分类:

一共有二十类,推断只训练了20类,可能是为了跑的更快。若要自己训练数据需要去caffe或者TF平台。

const char* classNames[] = { "background","aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair","cow", "diningtable", "dog", "horse","motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor" };

摄像头:

自己随便找一个摄像头就行。修改摄像头位置在:capture.open(0);0不行就改为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 meanVal = 127.5;
const float scaleFactor = 0.007843f;
const char* classNames[] = { "background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor" };

String modelFile = "E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.caffemodel";
String model_text_file = "E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.prototxt";

int main(int argc, char** argv) {
	VideoCapture capture;
	capture.open(0);
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	int w = capture.get(CAP_PROP_FRAME_WIDTH);
	int h = capture.get(CAP_PROP_FRAME_HEIGHT);
	printf("frame width : %d, frame height : %d", w, h);

	// set up net
	Net net = readNetFromCaffe(model_text_file, modelFile);

	Mat frame;
	while (capture.read(frame)) {
		imshow("input", frame);

		//预测
		Mat inputblob = blobFromImage(frame, scaleFactor, Size(width, height), meanVal, false);
		net.setInput(inputblob, "data");
		Mat detection = net.forward("detection_out");

		//检测
		Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
		float confidence_threshold = 0.25;
		for (int i = 0; i < detectionMat.rows; i++) {
			float confidence = detectionMat.at<float>(i, 2);
			if (confidence > confidence_threshold) {
				size_t objIndex = (size_t)(detectionMat.at<float>(i, 1));
				float tl_x = detectionMat.at<float>(i, 3) * frame.cols;
				float tl_y = detectionMat.at<float>(i, 4) * frame.rows;
				float br_x = detectionMat.at<float>(i, 5) * frame.cols;
				float br_y = detectionMat.at<float>(i, 6) * frame.rows;

				Rect object_box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
				rectangle(frame, object_box, Scalar(0, 0, 255), 2, 8, 0);
				putText(frame, format("%s", classNames[objIndex]), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);
			}
		}
		imshow("ssd-video-demo", frame);
		char c = waitKey(5);
		if (c == 27) { // ESC退出
			break;
		}
	}
	capture.release();
	waitKey(0);
	return 0;
}

结果:

原始图像:

Opencv3.3版本以上使用SSD模型实现实时摄像头对象检测

检测之后:

Opencv3.3版本以上使用SSD模型实现实时摄像头对象检测

总结:

我是在window上跑的,配置I5 6402p,用的CPU,不知道GPU怎么跑,有知道的大神欢迎交流。速度基本实时,准确度还可以。