使用caffe训练的深度学习做目标检测(车辆检测)

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/dnn/dnn.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <string>
#include <iostream>
#include <stdio.h>
#include <time.h>

#include <vector>

int main(int argc,char* argv[]){
	//init time param
	clock_t t_start,t_end;

	//init dnn
	cv::Ptr<cv::dnn::Importer> importer;
	std::string modelTxt = "./deploy.prototxt";
	std::string modelBin = "./googlenet_finetune_web_car_iter_10000.caffemodel";
	importer = cv::dnn::createCaffeImporter(modelTxt,modelBin);
        
	if(!importer){
		std::cerr << "Can't load network by using the following files: " << std::endl;
		std::cerr << "prototxt:   " << modelTxt << std::endl;
		std::cerr << "caffemodel: " << modelBin << std::endl;
		exit(-1);
	}
	cv::dnn::Net net;
	importer->populateNet(net);
	importer.release();

	//init cascades
	std::string cascadeModel = "./cars.xml";
	cv::CascadeClassifier cascade;
	if( !cascade.load(cascadeModel) ){
		std::cerr << "Can't load cascade model:" << std::endl;
		std::cerr << "cascade:   " << cascadeModel << std::endl;
		exit(-1);
	}

	cv::Mat frame;
	char image_path[32];

	//int i = atoi(argv[1]);

	for(int i=1;i<=1700;i++){

		t_start = clock();

		sprintf(image_path,"/home/xyl/caffe/lesson4/cars_input/cars_input/in%06d.jpg",i);
		std::cout<<image_path<<std::endl;
		frame = cv::imread(image_path);
		std::vector<cv::Rect> cars;
		cascade.detectMultiScale(frame,cars,1.1,2,0);
		for(int j=0;j<cars.size();j++){
			cv::Mat car_candidate = frame(cars[j]);
			cv::resize(car_candidate,car_candidate,cv::Size(224,224));
			
			cv::dnn::Blob inputBlob = cv::dnn::Blob(car_candidate);
			net.setBlob(".data",inputBlob);
			net.forward();
			cv::dnn::Blob prob = net.getBlob("prob");
			cv::Mat probMat = prob.matRefConst().reshape(1,1);
			double classProb;
			cv::minMaxLoc(probMat,NULL,&classProb);

			char temp[10];
			sprintf(temp, "%lf", classProb);
			std::string s(temp);
			cv::putText(frame,s,cv::Point(cars[j].x,cars[j].y-10),cv::FONT_HERSHEY_SCRIPT_SIMPLEX,0.5,cv::Scalar::all(255),1,8);

			cv::rectangle(frame,cars[j],cv::Scalar(255,0,0));
		}

		t_end = clock();

		//show processing time
		std::cout<<"time:"<<double(t_end-t_start)/CLOCKS_PER_SEC<<std::endl;

		//show result
		cv::imshow("frame",frame);
		cv::waitKey(1);

	} //--end for loop
}

运行之后,结果是这样的:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值