【OpenCV进阶】如何基于SSD模型实现对物体的实时检测

📢:如果你也对机器人、人工智能感兴趣,看来我们志同道合✨
📢:不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852
📢:文章若有幸对你有帮助,可点赞 👍 收藏 ⭐不迷路🙉
📢:内容若有错误,敬请留言 📝指正!原创文,转载注明出处

在这里插入图片描述

引言

reference:利用OpenCV和深度学习实现人脸检测
2018年之后,opencv DNN模式下开始使用卷积神经网络SSD人脸检测器,目前商业应用非常成熟,可以做到实时运行,对各种角度人脸均能做到准确的检测,具有很强的抗干扰性。

注意:opencv自带的人脸检测模型

一、安装Opencv

Opencv学习-第1节:环境配置与搭建

二、下载模型文件

打开windows下的终端,点击左下角的徽标键,输入cmd即可。然后在终端输入cd /d D:\opencv-4.1.0\opencv\sources\samples\dnn\face_detector
然后继续输入:python download_weights.py,这样就会根据权重文件生成配置文件和模型文件。

三、查看参数

进入models.yml文件,查看需要设置的参数。文件路径:D:\opencv-4.1.0\opencv\sources\samples\dnn
在这里插入图片描述

全部代码

1.基于caffe

基于caffe框架下的SSD深度卷积神经网络模型,做人脸检测。
.prototxt和.caffemodel的作用如下:

The .prototxt file(s) which define the model architecture (i.e., the layers themselves)

The .caffemodel file which contains the weights for the actual layers

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>

using namespace cv;
using namespace cv::dnn;
using namespace std;

String model_text_file = "D:/opencv-4.1.0/models/face_detector/deploy.prototxt";
String modelFile = "D:/opencv-4.1.0/models/face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel";

int main(int argc, char** argv) {
	VideoCapture capture;
	capture.open(0);
	namedWindow("input", 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);
	net.setPreferableBackend(DNN_BACKEND_OPENCV);
	net.setPreferableTarget(DNN_TARGET_CPU);

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

		//预测
		Mat inputblob = blobFromImage(frame, 1.0, Size(300, 300), Scalar(104,177,123), 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 %.2f", "face", confidence), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);
			}
		}
		vector < double>layerstimings;
		double freq = getTickFrequency() / 1000;
		double time = net.getPerfProfile(layerstimings) / freq;
		ostringstream ss;
		ss << "FPS" << 1000 / time << ";time:" << time << "ms";
		putText(frame, ss.str(), Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255), 2, 8);
		imshow("face-detection-demo", frame);
		char c = waitKey(5);
		if (c == 27) { // ESC退出
			break;
		}
	}
	capture.release();//释放资源
	waitKey(0);
	return 0;
}

2.基于TensorFlow

基于TensorFlow的SSD模型,人脸检测。

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>

using namespace cv;
using namespace cv::dnn;
using namespace std;

String model_text_file = "D:/opencv-4.1.0/models/face_detector/opencv_face_detector.pbtxt";
String modelFile = "D:/opencv-4.1.0/models/face_detector/opencv_face_detector_uint8.pb";

int main(int argc, char** argv) {
	VideoCapture capture;
	capture.open(0);
	namedWindow("input", 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 = readNetFromTensorflow(modelFile, model_text_file);

	net.setPreferableBackend(DNN_BACKEND_OPENCV);
	net.setPreferableTarget(DNN_TARGET_CPU);

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

		//预测
		Mat inputblob = blobFromImage(frame, 1.0, Size(300, 300), Scalar(104,177,123), 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 %.2f", "face", confidence), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);
			}
		}
		vector < double>layerstimings;
		double freq = getTickFrequency() / 1000;
		double time = net.getPerfProfile(layerstimings) / freq;
		ostringstream ss;
		ss << "FPS" << 1000 / time << ";time:" << time << "ms";
		putText(frame, ss.str(), Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255), 2, 8);
		imshow("face-detection-demo", frame);
		char c = waitKey(5);
		if (c == 27) { // ESC退出
			break;
		}
	}
	capture.release();//释放资源
	waitKey(0);
	return 0;
}

3.效果展示

在笔记本上运行,还挺流程的。
在这里插入图片描述

SSD模型-物体检测

全部代码

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>

using namespace cv;
using namespace cv::dnn;
using namespace std;

String model_text_file = "D:/opencv-4.1.0/models/ssd/MobileNetSSD_deploy.prototxt";
String modelFile = "D:/opencv-4.1.0/models/ssd/MobileNetSSD_deploy.caffemodel";

String objNames[] = { "background","aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair","cow", "diningtable", "dog", "horse","motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor" };//只检测20种对象

int main() {
	Mat src = imread("D:/images/objects.jpg");
	if (src.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	imshow("input image", src);

	Net net = readNetFromCaffe(model_text_file, modelFile);

	//构建输入
	Mat blobImage = blobFromImage(src, 0.007843,Size(300, 300),Scalar(127.5, 127.5, 127.5), true, false);
	printf("blobImage width : %d, height: %d\n", blobImage.cols, blobImage.rows);
	net.setInput(blobImage, "data");
		
	//执行推理
	Mat detection = net.forward("detection_out");
	Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
	float confidence_threshold = 0.2;

	//解析输出数据
	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) * src.cols;
			float tl_y = detectionMat.at<float>(i, 4) * src.rows;
			float br_x = detectionMat.at<float>(i, 5) * src.cols;
			float br_y = detectionMat.at<float>(i, 6) *src.rows;

			Rect object_box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
			rectangle(src, object_box, Scalar(0, 0, 255), 2, 8, 0);
			putText(src, format("%.2f,%s", confidence, objNames[objIndex].c_str()), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 0), 2);
		}
	}
	imshow("ssd-demo",src);

	waitKey(0);
	return 0;
}

效果展示

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌小超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值