windows下用opencv调用caffe训练的模型进行图像识别

// testOpencvDNN.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\dnn.hpp>
#include <iostream>

using namespace std;
using namespace cv;

const size_t inWidth = 300;
const size_t inHeight = 300;
const float WHRatio = inWidth / (float)inHeight;
const char* classNames[] = { "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow",
"diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};//这个需要根据训练的类别定义

int main() {

        //获取摄像头的图像处理
	//Mat frame1;
	//VideoCapture cap(0);

	//if (!cap.isOpened())
	//{
	//	cout << "error" << endl;
	//	waitKey(0);
	//	return -1;
	//}

	//cap >> frame1;
	//namedWindow("show");
	//imshow("show", frame1);
	//while (true)
	//{
	//	// Capture the current frame
	//	cap >> frame1;
	//	imshow("show", frame1);
	//	//waitKey(30);
	//	if (waitKey(30) >= 0)break;
	//}

	//cap.release();

	Mat frame = cv::imread("cat.jpg");
	Size frame_size = frame.size();


	String caffemodel = "F:\\models\\VGG_VOC0712_SSD_300x300_iter_120000.caffemodel";
	String prototxt = "F:\\models\\deploy.prototxt";
	dnn::Net net = cv::dnn::readNetFromCaffe(prototxt, caffemodel);

	/*Size cropSize;
	if (frame_size.width / (float)frame_size.height > WHRatio)
	{
		cropSize = Size(static_cast<int>(frame_size.height * WHRatio),
			frame_size.height);
	}
	else
	{
		cropSize = Size(frame_size.width,
			static_cast<int>(frame_size.width / WHRatio));
	}

	Rect crop(Point((frame_size.width - cropSize.width) / 2,
		(frame_size.height - cropSize.height) / 2),
		cropSize);*/

	cv::resize(frame, frame, cv::Size(inWidth, inHeight));
	cv::Mat blob = cv::dnn::blobFromImage(frame);
	//cout << "blob size: " << blob.size << endl;

	net.setInput(blob);
	Mat output = net.forward();
	//cout << "output size: " << output.size << endl;

	Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>());

	//frame = frame(crop);
	float confidenceThreshold = 0.20;
	for (int i = 0; i < detectionMat.rows; i++)
	{
		float confidence = detectionMat.at<float>(i, 2);

		if (confidence > confidenceThreshold)
		{
			size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));

			int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
			int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
			int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
			int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);

			ostringstream ss;
			ss << confidence;
			String conf(ss.str());

			Rect object((int)xLeftBottom, (int)yLeftBottom,
				(int)(xRightTop - xLeftBottom),
				(int)(yRightTop - yLeftBottom));

			rectangle(frame, object, Scalar(0, 255, 0), 2);
			String label = String(classNames[objectClass]) + ": " + conf;
			int baseLine = 0;
			Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
			rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
				Size(labelSize.width, labelSize.height + baseLine)),
				Scalar(0, 255, 0), CV_FILLED);
			putText(frame, label, Point(xLeftBottom, yLeftBottom),
				FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
		}
	}
	imshow("image", frame);
	waitKey(0);
	return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值