Object Detection API(5)——使用OpenCV调用自己的模型

Object Detection API5)——使用OpenCV调用自己的模型

博客:https://blog.csdn.net/qq_34106574 

简书:https://www.jianshu.com/u/fb86cd4f8bf8

       本节将使用OpenCV的接口来调用我们上一节导出的“冰冻”模型。首先,如果要使用OpenCV的接口来调用我们借助特定模型迁移训练的结果,我们除了pb模型,还需要提供模型描述文件,即:.pbtxt文件,此文件由OpenCV官方提供,下载网址:https://github.com/opencv/opencv_extra/tree/master/testdata/dnn

      我们前面迁移训练的模型为:faster_rcnn_resnet101_coco模型,而OpenCV官方对此文件尚未更新,所以需要更换模型,这里选择ssd_mobilenet_v1_coco模型,同时我们按照此模型将我们前面的内容再进行回顾。

1, 数据组织同第二节相同,即可以直接使用第二节导出的record数据,这里不赘述。

2,训练自己的模型:

   (1)建立工程目录,将ssd_mobilenet_v1_coco模型文件夹放在工程目录下。

   (2)从G:\git\models-master\research\object_detection\samples\configs复制ssd_mobilenet_v1_coco.config配置文件到你的工程  目录下,修改此配置文件如下:将fine_tune_checkpoint:XXX/XXX,input_path:XXX/XXX,input_map_path:XXX/XXX等处进行修改,主要是数据,数据描述,模型的路径。

   (3)]编辑my_label_map.pbtxt如下:

             item{name:“face” id:1 dispaly name:”Face”}

   (4)运行训练脚本如下:

             python ../research/object_detection/train.py --logtostderr --train_dir=train/ --                  pipeline_config_path=train/ssd_mobilenet_v1_coco.config

   (5)训练结束后按照第四节的方法输出模型


     训练时可能遇到的问题:

                 E:TypeError: `pred` must be a Tensor, or a Python bool, or 1 or 0. Found instead: None 

     解决方法:

   在ssd_mobilenet_v1_feature_extractor.py文档的109行修改:将

              is_training=None, regularize_depthwise=True)):

   修改为:

              is_training=True, regularize_depthwise=True)):

3,训练结束后,使用OpenCV调用

  (1)下载OpenCV提供的ssd_mobilenet_v1_coco.pbtxt。然后把这个文件的第2222行修改为attr { key: "num_classes" value { i: 2       } }。

  (2)使用vs2015建立工程,配置OpenCV3.3.1,参照官方的例子进行修改,将之前导出的模型放置在工程科访问的路径。

  (3)给出实例程序如下,编译运行即可:

int main() {

	String weights = "face_frozen_inference_graph.pb";
	String prototxt = "ssd_mobilenet_v1_coco.pbtxt";
	dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt);

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

	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::Mat blob = cv::dnn::blobFromImage(frame,1./255,Size(300,300));

	net.setInput(blob);
	Mat output = net.forward();


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

	frame = frame(crop);
	float confidenceThreshold = 0.50;
	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;
}

4,测试结果如下:



参考资料:

https://blog.csdn.net/xingchenbingbuyu/article/details/78416887


注:更多内容分享及源码获取欢迎关注微信公众号:ML_Study


版权声明:本文为博主原创文章,转载请联系作者取得授权。https://blog.csdn.net/qq_34106574/article/category/7628923 


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Arthur.AI

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

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

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

打赏作者

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

抵扣说明:

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

余额充值