Cmake编译OpenCV3.1源码+运行自带的dnn分类识别
2017-01-17 23:12
591 查看
深度学习作为今年来一个新兴的研究方向,如今真的是不要太火;而OpenCV作为一个久负盛名的开源视觉处理库,也一直被用在卷积神经网络的开源工具——caffe中,用来处理图像。而OpenCV再进入3.0时代以后,也顺应民意加入了DNN模块,能够与caffe无缝对接!
1、要想在OpenCV中运行dnn,首先得编译OpenCV3以上版本的源码,因为dnn模块封装在OpenCV的contrib库中,本博客选用的是OpenCV-3.1,采用的配置是VS2013加OpenCV-3.1,需要下载contrib库,然后在cmake中,
在“开始”菜单中点击“CMake (cmake-gui)”,打开CMake程序,此时将弹出编译设置界面。如下图所示
然后点击configure,选择与你对应的vs版本,然后很快就会配置完成;别急,接下来这步很重要,如下图,找到OPENCV_EXTRA_MODULES_PATH,然后再后面添加contrib的路径,一直到contrib(例如:F:/OpenCV/opencv-3.1.0/opencv_contrib-master/opencv_contrib-master/modules),然后再次点击Configure,完成后点击Generate,就可以生成vs工程了。注意:在这过程中可能会出现一个ocl_text的错误,在搜索框找到text,然后取消后面的对勾,再次Generate就可以了。还有一个需要注意的地方:这Configure的时候选择对应的平台VS2012对应的是Win32,而VS2012
X64对应的是X64,这个和你最后生成的sln有影响,意味着每次只能生成X86的OpenCV或者是X64的版本,而不能在OpenCV.sln中通过切换同时生成两个!
2、编译源码,打开OPENCV.sln,然后rebuild所有项目,等待,大概30分钟左右,可能因每个人电脑性能不同,时间会不同,然后编译生成,右击install,仅重新生成install,然后就编译完成了。这中间可能会有的dll无法生成,因为不影响我的工作,就没再管了!如下图,源码编译的工作就完成了!
--------------------------------------分割线-----------------------------------------------
3、接下来就是使用了,其实很简单,新建一个控制台工程,然后配置OpenCV。将
4、生成成功后,就可以按F5运行了,但是可能会报一个OpenCL的错误,这里,我卡了好久,后来在于在OpenCV论坛的一个评论中搜到解决方法,因为是intel的显卡和cuda有冲突,需要关闭OpenCL。这里我把整个程序贴上,读者只需要复制到工程中就可以运行了!
下面这个图就是成功运行后,控制台输出的结果!
1、要想在OpenCV中运行dnn,首先得编译OpenCV3以上版本的源码,因为dnn模块封装在OpenCV的contrib库中,本博客选用的是OpenCV-3.1,采用的配置是VS2013加OpenCV-3.1,需要下载contrib库,然后在cmake中,
在“开始”菜单中点击“CMake (cmake-gui)”,打开CMake程序,此时将弹出编译设置界面。如下图所示
然后点击configure,选择与你对应的vs版本,然后很快就会配置完成;别急,接下来这步很重要,如下图,找到OPENCV_EXTRA_MODULES_PATH,然后再后面添加contrib的路径,一直到contrib(例如:F:/OpenCV/opencv-3.1.0/opencv_contrib-master/opencv_contrib-master/modules),然后再次点击Configure,完成后点击Generate,就可以生成vs工程了。注意:在这过程中可能会出现一个ocl_text的错误,在搜索框找到text,然后取消后面的对勾,再次Generate就可以了。还有一个需要注意的地方:这Configure的时候选择对应的平台VS2012对应的是Win32,而VS2012
X64对应的是X64,这个和你最后生成的sln有影响,意味着每次只能生成X86的OpenCV或者是X64的版本,而不能在OpenCV.sln中通过切换同时生成两个!
2、编译源码,打开OPENCV.sln,然后rebuild所有项目,等待,大概30分钟左右,可能因每个人电脑性能不同,时间会不同,然后编译生成,右击install,仅重新生成install,然后就编译完成了。这中间可能会有的dll无法生成,因为不影响我的工作,就没再管了!如下图,源码编译的工作就完成了!
--------------------------------------分割线-----------------------------------------------
3、接下来就是使用了,其实很简单,新建一个控制台工程,然后配置OpenCV。将
opencv_contrib\modules\dnn\samples目录下的4个文件拷贝至项目文件夹内覆盖。
至于model文件
bvlc_googlenet.caffemodel需要单独下载,即训练好的分类器模型。
4、生成成功后,就可以按F5运行了,但是可能会报一个OpenCL的错误,这里,我卡了好久,后来在于在OpenCV论坛的一个评论中搜到解决方法,因为是intel的显卡和cuda有冲突,需要关闭OpenCL。这里我把整个程序贴上,读者只需要复制到工程中就可以运行了!
/**M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2013, OpenCV Foundation, all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #include <opencv2/dnn.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> using namespace cv; using namespace cv::dnn; #include <fstream> #include <iostream> #include <cstdlib> #include <opencv2/core/ocl.hpp> using namespace std; /* Find best class for the blob (i. e. class with maximal probability) */ void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb) { Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix Point classNumber; minMaxLoc(probMat, NULL, classProb, NULL, &classNumber); *classId = classNumber.x; } std::vector<String> readClassNames(const char *filename = "synset_words.txt") { std::vector<String> classNames; std::ifstream fp(filename); if (!fp.is_open()) { std::cerr << "File with classes labels not found: " << filename << std::endl; exit(-1); } std::string name; while (!fp.eof()) { std::getline(fp, name); if (name.length()) classNames.push_back( name.substr(name.find(' ')+1) ); } fp.close(); return classNames; } int main(int argc, char **argv) { cv::dnn::initModule(); //Required if OpenCV is built as static libs ocl::setUseOpenCL(false);//关闭OpenCL,就不会出错了 String modelTxt = "bvlc_googlenet.prototxt"; String modelBin = "bvlc_googlenet.caffemodel"; String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg"; //! [Read and initialize network] Net net = dnn::readNetFromCaffe(modelTxt, modelBin); //! [Read and initialize network] //! [Check that network was read successfully] if (net.empty()) { 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; std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl; std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl; exit(-1); } //! [Check that network was read successfully] //! [Prepare blob] Mat img = imread(imageFile); if (img.empty()) { std::cerr << "Can't read image from the file: " << imageFile << std::endl; exit(-1); } resize(img, img, Size(224, 224)); //GoogLeNet accepts only 224x224 RGB-images dnn::Blob inputBlob = dnn::Blob::fromImages(img); //Convert Mat to dnn::Blob batch of images //! [Prepare blob] //! [Set input blob] net.setBlob(".data", inputBlob); //set the network input //! [Set input blob] //! [Make forward pass] net.forward(); // //compute output //! [Make forward pass] //! [Gather output] dnn::Blob prob = net.getBlob("prob"); //gather output of "prob" layer int classId; double classProb; getMaxClass(prob, &classId, &classProb);//find the best class //! [Gather output] //! [Print results] std::vector<String> classNames = readClassNames(); std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl; std::cout << "Probability: " << classProb * 100 << "%" << std::endl; //! [Print results] return 0; } //main
下面这个图就是成功运行后,控制台输出的结果!
相关文章推荐
- Cmake编译OpenCV3.1源码
- 【OpenCV人脸识别入门教程之三】CMake编译OpenCV源码
- 用Cmake编译Opencv源码,生成动态库
- CMAKE编译opencv源码错误解决办法
- OpenCV3.0.1源码编译及自带样例工程代码编译
- 在不CMake编译的情况下,查看OpenCV的源码!
- 将系统自带的Launcher2源码导入eclipse中编译运行
- 将系统自带的Launcher2源码导入eclipse中编译运行
- cmake编译opencv3.1+contribute模块,并提供编译后的dll、lib等文件下载
- OpenCV3.1开发系列-CMake编译OpenCV3.1及opencv_contrib
- Win10 下Cmake编译配置 Opencv3.1 + Cuda7.5 + VS2013
- 64位+VS2013+OpenCV2.4.9+CMake+TBB 编译OpenCV源码
- OpenCV 3_0源码编译(CMake 3.2.3)
- cmake3.6编译opencv3.1时出现变量设置错误
- Android中如何编译运行系统自带桌面Launcher2源码
- 学习OpenCV的学习笔记系列(二)源码编译及自带样例工程(转)
- CMake编译运行OpenCV程序_debug总结
- OpenCV学习笔记(四):CMake 编译 OpenCV源码 on Windows
- OpenCV3.0 或OpenCV3.1 与cuda编译出现LINK: warning LNK4044: 无法识别的选项/LC:/Program Files/XXXXXXXX/lib/x64
- Android中如何编译运行系统自带桌面Launcher2源码