使用OpenCV调用Caffe-SSD训练好的模型

14 篇文章 2 订阅

前言

1.OpenCV3之后的dnn模型可以可以调用好多用深度学习框架的训练好的模型。
2.这里我演示是我自己训练的一个围棋棋盘识别模型,使用的神经网络是VGG16,OpenCV3.3,IDE是VS2015。
3.训练好模型之后把标签文件,模型文件,配置文件复制到当前工程目录方便调用。

代码

1.标签文件内容:

label: 0
display_name: "background"
label:1
display_name: "B"
label:2
display_name: "H"

2.代码

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


using namespace cv;
using namespace cv::dnn;
using namespace std;
const int meanValues[3] = { 104, 117, 123 };
const size_t width = 300;
const size_t height = 300;
//得到标签名
static Mat getMean(const size_t &w, const size_t &h);
static Mat preprocess(const Mat &frame);
vector<string> readLabels(string label_file);
//得到棋盘
void getBarod(Mat &src, Mat &go_barod,  string model_file, string model_text_file, string label_file);

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));
	Mat mean = getMean(width, height);
	subtract(preprocessed, mean, preprocessed);
	return preprocessed;
}

void getBarod(Mat &src, Mat &go_barod, string model_file, string model_text_file, string label_file)
{
	Rect rect;
	vector<string> obj_names = readLabels(label_file);
	
	Ptr<dnn::Importer> importer;
	try
	{
		importer = createCaffeImporter(model_text_file, model_file);
	}
	catch (const cv::Exception &err)
	{
		cerr << err.msg << endl;
	}
	Net net;
	importer->populateNet(net);
	importer.release();

	Mat input_image = preprocess(src);
	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) * 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 = Rect((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
				rectangle(src, rect, Scalar(i * 10, 0, 255), 2, 8, 0);
				putText(src, format("%s", obj_names[objIndex].c_str()), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);
		}
	}
	namedWindow("src", 0);
	imshow("src",src);
}

//得到标签名
void readLabels(vector<string> obj_names, string label_file)
{
	ifstream fp(label_file);
	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(), "");
			obj_names.push_back(temp);
		}
	}
}

void adaptiveLogarithmicMapping(const Mat& img, Mat &dst)
{
	Mat ldrDrago;
	img.convertTo(ldrDrago, CV_32FC3, 1.0f / 255);
	cvtColor(ldrDrago, ldrDrago, cv::COLOR_BGR2XYZ);
	Ptr<TonemapDrago> tonemapDrago = createTonemapDrago(1.f, 1.f, 0.85f);
	tonemapDrago->process(ldrDrago, dst);
	cvtColor(dst, dst, cv::COLOR_XYZ2BGR);
	dst.convertTo(dst, CV_8UC3, 255);
}


vector<string> readLabels(string label_file)
{
	vector<string> objNames;
	ifstream fp(label_file);
	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;
}

主函数:

#include "function.h"

string label_file = "weiqi.txt";
string model_file = "weiqi.caffemodel";
string model_text_file = "weiqi.prototxt";

int main(void)
{
	Mat backbackground = imread("b1.jpg");
	if (backbackground.empty())
	{
		cout<<"could not load image...\n")<<endl;
		return -1;
	}
	
	Mat frame, go_barod,standard_barod;

	getBarod(backbackground, go_barod,model_file,model_text_file, label_file);

	waitKey(0);
	return 0;
}

测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

知来者逆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值