opencv贾老师系列19——深度神经网络

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

使用googlenet模型实现图像分类

在这里插入图片描述
在这里插入图片描述

#include "pch.h" 
#include <opencv2/opencv.hpp>
#include <iostream>

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

String model_bin_file = "  .caffemodel";
String model_txt_file = "  .prototxt";
String labels_txt_file = "  .prototxt";
vector<String>readLabels();
int main(int argc, char** argv)
{
	Mat src = imread("",1);
	if (src.empty())
	{
		cout << "could not load the picture..." << endl;
		return -1;
	}
	namedWindow("input image", CV_WINDOW_NORMAL);
	imshow("input image", src);
	vector<String>labels = readLabels();
	Net net = readNetFromCaffe(model_txt_file, model_bin_file);
	if (net.empty())
	{
		cout << "read caffe model data failure..." << endl;
		return -1;
	}
	Mat inputBlob = blobFromImage(src, 1.0, Size(224, 224), Scalar(104, 117, 123));
	Mat prob; //最后一层
	for (int i = 0; i < 10; i++)
	{
		net.setInput(inputBlob, "data");
		prob = net.forward("prob");
	}
	Mat probMat = prob.reshape(1, 1);
	Point classNumber;
	double classProb;
	minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
	int classidx = classNumber.x;
	printf("\n current image classification :%s, Possible : %.2f", labels.at(classidx).c_str(), classProb);
	putText(src, labels.at(classidx), Point(20, 20), FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, Scalar(0, 0, 255), 2, 8);
	imshow("Image Classification",src);


	waitKey(0);
	return 0;
}
vector<String>readLabels()
{
	vector<String>classNames;
	ifstream fp(labels_txt_file);
	if (!fp.is_open())
	{
		cout << "could not open the file..." << endl;
		exit(-1);
	}
	string name;
	while (!fp.eof())  //如果不是文件结尾
	{
		getline(fp, name);
		if (name.length())
		{
			classNames.push_back(name.substr(name.find(' ') + 1));
		}
		fp.close();
		return classNames;
	}
}

结果显示:
在这里插入图片描述

使用SSD模型实现对象检测

SSD检测速度很快,检测速度与图片的大小无关,与图像内检测对象的数量有关。
在这里插入图片描述
在这里插入图片描述

#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;
String labelFile = "D:/vcprojects/VGGNet/ILSVRC2016/SSD_300x300/labelmap_det.txt";
String modelFile = "D:/vcprojects/VGGNet/ILSVRC2016/SSD_300x300/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel";
String model_text_file = "D:/vcprojects/VGGNet/ILSVRC2016/SSD_300x300/deploy.prototxt";

vector<String> readLabels();
const int meanValues[3] = { 104, 117, 123 };
static Mat getMean(const size_t &w, const size_t &h) {
	Mat mean;
	vector<Mat> channels;
	for (int i = 0; i < 3; i++) {
		Mat channel(h, w, CV_32F, Scalar(meanValues[i]));
		channels.push_back(channel);
	}
	merge(channels, mean);
	return mean;
}

static Mat preprocess(const Mat &frame) {
	Mat preprocessed;
	frame.convertTo(preprocessed, CV_32F);
	resize(preprocessed, preprocessed, Size(width, height)); // 300x300 image
	Mat mean = getMean(width, height);
	subtract(preprocessed, mean, preprocessed);
	return preprocessed;
}

int main(int argc, char** argv) {
	Mat frame = imread("D:/vcprojects/images/persons.png");
	if (frame.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", frame);

	vector<String> objNames = readLabels();
	// import Caffe SSD model
	Ptr<dnn::Importer> importer;
	try {
		importer = createCaffeImporter(model_text_file, modelFile);
	}
	catch (const cv::Exception &err) {
		cerr << err.msg << endl;
	}
	Net net;
	importer->populateNet(net);
	importer.release();

	Mat input_image = preprocess(frame);
	Mat blobImage = blobFromImage(input_image);

	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) * 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", objNames[objIndex].c_str()), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);
		}
	}
	imshow("ssd-demo", frame);

	waitKey(0);
	return 0;
}

vector<String> readLabels() {
	vector<String> objNames;
	ifstream fp(labelFile);
	if (!fp.is_open()) {
		printf("could not open the file...\n");
		exit(-1);
	}
	string name;
	while (!fp.eof()) {
		getline(fp, name);
		if (name.length() && (name.find("display_name:") == 0)) {
			string temp = name.substr(15);
			temp.replace(temp.end() - 1, temp.end(), "");
			objNames.push_back(temp);
		}
	}
	return objNames;
}

MobileNet模型与数据介绍

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

FCN模型图像分割


Model Zoo 模型库
在这里插入图片描述

在这里插入代码片

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值