昨天有人问我关于调用mask_rcnn模型的问题,忽然想到最近三个月都没用opencv调用训练好的mask_rcnn模型了,今晚做个尝试,所以重新编译了 opencv4,跑个案例试试

#include <fstream>
#include <sstream>
#include <iostream>
#include <string.h> #include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp> using namespace cv;
using namespace dnn;
using namespace std; RNG rng1; // Initialize the parameters
float confThreshold = 0.5; // Confidence threshold
float maskThreshold = 0.3; // Mask threshold //vector<string> classes;
//vector<Scalar> colors; // Draw the predicted bounding box
void drawBox(Mat& frame, int classId, float conf, Rect box, Mat& objectMask); // Postprocess the neural network's output for each frame
void postprocess(Mat& frame, const vector<Mat>& outs); int main()
{
// Give the configuration and weight files for the model
//String textGraph = "./mask_rcnn_inception_v2_coco_2018_01_28/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt";
//String modelWeights = "./mask_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb"; String modelWeights = "E:\\Opencv\\model_1\\mask_rcnn_inception_v2_coco_2018_01_28\\frozen_inference_graph.pb";
String textGraph = "E:\\Opencv\\model_1\\mask_rcnn_inception_v2_coco_2018_01_28\\mask_rcnn_inception_v2_coco_2018_01_28.pbtxt";
// Load the network
Net net = readNetFromTensorflow(modelWeights, textGraph);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU); // Open a video file or an image file or a camera stream.
string str, outputFile;
VideoCapture cap();//根据摄像头端口id不同,修改下即可
//VideoWriter video;
Mat frame, blob; // Create a window
static const string kWinName = "Deep learning object detection in OpenCV";
namedWindow(kWinName, WINDOW_NORMAL); // Process frames.
if (>)
{
// get frame from the video
//cap >> frame;
frame = cv::imread("D:\\image\\5.png"); // Stop the program if reached end of video
if (frame.empty())
{
cout << "Done processing !!!" << endl;
cout << "Output file is stored as " << outputFile << endl;
}
// Create a 4D blob from a frame.
blobFromImage(frame, blob, 1.0, Size(frame.cols, frame.rows), Scalar(), true, false);
//blobFromImage(frame, blob); //Sets the input to the network
net.setInput(blob); // Runs the forward pass to get output from the output layers
std::vector<String> outNames();
outNames[] = "detection_out_final";
outNames[] = "detection_masks";
vector<Mat> outs;
net.forward(outs, outNames); // Extract the bounding box and mask for each of the detected objects
postprocess(frame, outs); // Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
vector<double> layersTimes;
double freq = getTickFrequency() / ;
double t = net.getPerfProfile(layersTimes) / freq;
string label = format("Mask-RCNN on 2.5 GHz Intel Core i7 CPU, Inference time for a frame : %0.0f ms", t);
putText(frame, label, Point(, ), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(, , )); // Write the frame with the detection boxes
Mat detectedFrame;
frame.convertTo(detectedFrame, CV_8U); imshow(kWinName, frame); }
//cap.release();
waitKey();
return ;
} // For each frame, extract the bounding box and mask for each detected object
void postprocess(Mat& frame, const vector<Mat>& outs)
{
Mat outDetections = outs[];
Mat outMasks = outs[]; // Output size of masks is NxCxHxW where
// N - number of detected boxes
// C - number of classes (excluding background)
// HxW - segmentation shape
const int numDetections = outDetections.size[];
const int numClasses = outMasks.size[]; outDetections = outDetections.reshape(, outDetections.total() / );
for (int i = ; i < numDetections; ++i)
{
float score = outDetections.at<float>(i, );
if (score > confThreshold)
{
// Extract the bounding box
int classId = static_cast<int>(outDetections.at<float>(i, ));
int left = static_cast<int>(frame.cols * outDetections.at<float>(i, ));
int top = static_cast<int>(frame.rows * outDetections.at<float>(i, ));
int right = static_cast<int>(frame.cols * outDetections.at<float>(i, ));
int bottom = static_cast<int>(frame.rows * outDetections.at<float>(i, )); left = max(, min(left, frame.cols - ));
top = max(, min(top, frame.rows - ));
right = max(, min(right, frame.cols - ));
bottom = max(, min(bottom, frame.rows - ));
Rect box = Rect(left, top, right - left + , bottom - top + ); // Extract the mask for the object
Mat objectMask(outMasks.size[], outMasks.size[], CV_32F, outMasks.ptr<float>(i, classId)); // Draw bounding box, colorize and show the mask on the image
drawBox(frame, classId, score, box, objectMask); }
}
} // Draw the predicted bounding box, colorize and show the mask on the image
void drawBox(Mat& frame, int classId, float conf, Rect box, Mat& objectMask)
{
//Draw a rectangle displaying the bounding box
rectangle(frame, Point(box.x, box.y), Point(box.x + box.width, box.y + box.height), Scalar(, , ), ); //Get the label for the class name and its confidence
/*string label = format("%.2f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ":" + label;
}*/ //Display the label at the top of the bounding box
/*
int baseLine;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
box.y = max(box.y, labelSize.height);
rectangle(frame, Point(box.x, box.y - round(1.5*labelSize.height)), Point(box.x + round(1.5*labelSize.width), box.y + baseLine), Scalar(255, 255, 255), FILLED);
putText(frame, label, Point(box.x, box.y), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
*/
//Scalar color = colors[classId%colors.size()];
Scalar color = Scalar(rng1.uniform(, ), rng1.uniform(, ), rng1.uniform(, )); // Resize the mask, threshold, color and apply it on the image
resize(objectMask, objectMask, Size(box.width, box.height));
Mat mask = (objectMask > maskThreshold);
Mat coloredRoi = (0.3 * color + 0.7 * frame(box));
coloredRoi.convertTo(coloredRoi, CV_8UC3); // Draw the contours on the image
vector<Mat> contours;
Mat hierarchy;
mask.convertTo(mask, CV_8U);
findContours(mask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
drawContours(coloredRoi, contours, -, color, , LINE_8, hierarchy, );
coloredRoi.copyTo(frame(box), mask); }

检测速度和python比起来偏慢

运行日志:

[ INFO:0] global E:\Opencv\opencv-4.1.1\modules\videoio\src\videoio_registry.cpp (187) cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(7, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940)
[ INFO:0] global E:\Opencv\opencv-4.1.1\modules\videoio\src\backend_plugin.cpp (340) cv::impl::getPluginCandidates Found 2 plugin(s) for GSTREAMER
[ INFO:0] global E:\Opencv\opencv-4.1.1\modules\videoio\src\backend_plugin.cpp (172) cv::impl::DynamicLib::libraryLoad load E:\Opencv\opencv_4_1_1_install\bin\opencv_videoio_gstreamer411_64.dll => FAILED
[ INFO:0] global E:\Opencv\opencv-4.1.1\modules\videoio\src\backend_plugin.cpp (172) cv::impl::DynamicLib::libraryLoad load opencv_videoio_gstreamer411_64.dll => FAILED
[ INFO:0] global E:\Opencv\opencv-4.1.1\modules\core\src\ocl.cpp (888) cv::ocl::haveOpenCL Initialize OpenCL runtime...

opencv4 mask_rcnn模型调(c++)的更多相关文章

  1. 使用sklearn进行数据挖掘-房价预测(6)—模型调优

    通过上一节的探索,我们会得到几个相对比较满意的模型,本节我们就对模型进行调优 网格搜索 列举出参数组合,直到找到比较满意的参数组合,这是一种调优方法,当然如果手动选择并一一进行实验这是一个十分繁琐的工 ...

  2. 【新人赛】阿里云恶意程序检测 -- 实践记录10.27 - TF-IDF模型调参 / 数据可视化

    TF-IDF模型调参 1. 调TfidfVectorizer的参数 ngram_range, min_df, max_df: 上一篇博客调了ngram_range这个参数,得出了ngram_range ...

  3. 深度学习模型调优方法(Deep Learning学习记录)

    深度学习模型的调优,首先需要对各方面进行评估,主要包括定义函数.模型在训练集和测试集拟合效果.交叉验证.激活函数和优化算法的选择等. 那如何对我们自己的模型进行判断呢?——通过模型训练跑代码,我们可以 ...

  4. python的随机森林模型调参

    一.一般的模型调参原则 1.调参前提:模型调参其实是没有定论,需要根据不同的数据集和不同的模型去调.但是有一些调参的思想是有规律可循的,首先我们可以知道,模型不准确只有两种情况:一是过拟合,而是欠拟合 ...

  5. 机器学习笔记——模型调参利器 GridSearchCV(网格搜索)参数的说明

    GridSearchCV,它存在的意义就是自动调参,只要把参数输进去,就能给出最优化的结果和参数.但是这个方法适合于小数据集,一旦数据的量级上去了,很难得出结果.这个时候就是需要动脑筋了.数据量比较大 ...

  6. 【新人赛】阿里云恶意程序检测 -- 实践记录11.3 - n-gram模型调参

    主要工作 本周主要是跑了下n-gram模型,并调了下参数.大概看了几篇论文,有几个处理方法不错,准备下周代码实现一下. xgboost参数设置为: param = {'max_depth': 6, ' ...

  7. 【新人赛】阿里云恶意程序检测 -- 实践记录10.20 - 数据预处理 / 训练数据分析 / TF-IDF模型调参

    Colab连接与数据预处理 Colab连接方法见上一篇博客 数据预处理: import pandas as pd import pickle import numpy as np # 训练数据和测试数 ...

  8. tenorflow 模型调优

    # Create the Timeline object, and write it to a json from tensorflow.python.client import timeline t ...

  9. SPSS数据分析-时间序列模型

    我们在分析数据时,经常会碰到一种数据,它是由时间累积起来的,并按照时间顺序排列的一系列观测值,我们称为时间序列,它有点类似于重复测量数据,但是区别在于重复测量数据的时间点不会很多,而时间序列的时间点非 ...

随机推荐

  1. P1341 无序字母对[欧拉路]

    题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 解析 毒瘤字符串读入 我就是不喜欢邻接 ...

  2. 《少年先疯队》第八次团队作业:Alpha冲刺第二天

    前言   第一天冲刺会议   时间:2019.6.15   地点:宿舍 2.1 今日完成任务情况以及遇到的问题.   2.1.1今日完成任务情况 姚玉婷:房间信息管理功能的实现,如房间的显示, 马丽莎 ...

  3. Python爬取网页信息

    Python爬取网页信息的步骤 以爬取英文名字网站(https://nameberry.com/)中每个名字的评论内容,包括英文名,用户名,评论的时间和评论的内容为例. 1.确认网址 在浏览器中输入初 ...

  4. Linux secureCRT 介绍和安装和优化

    修改背景颜色

  5. Djiango权限组件

    一. login中注册 权限url def login(request): if request.method == "POST": username = request.POST ...

  6. Apache Solr Velocity模板远程代码执行

    更多内容,欢迎关注微信公众号:信Yang安全,期待与您相遇. 这里用的docker环境 很简单的 在这里不再介绍 本地搭建好环境然后访问8983端口 网页如下: 查下节点名称 同样名字可以访问http ...

  7. The Last Goodbye 电影《霍比特人3:五军之战》插曲

    https://music.163.com/#/song?id=29755223 I saw the light fade from the sky我看到天空褪去色彩On the wind I hea ...

  8. CODE FESTIVAL 2016 qual A题解

    传送门 不知道为什么\(AGC\)系列的题里突然多了这些--那就做吧-- \(A\) 什么玩意儿-- upd:因为没看到最后要加换行居然没有\(1A\)好气哦-- const int N=15; ch ...

  9. 《挑战30天C++入门极限》新手入门:C++中的函数重载

        新手入门:C++中的函数重载 函数重载是用来iostream>  using namespace std;  int test(int a,int b);  float test(flo ...

  10. vmware exsi安装部署

    本文章参考:https://blog.csdn.net/fishinhouse/article/details/80980051 1.VMware-ESXi-6.5.0镜像下载 网盘链接:https: ...