opencv:Load Caffe framework models

3 篇文章 1 订阅

这个是怎么编译扩展包的呢?

首先在here下载两个包:opencv_contribopencv

然后按照正常的方法把opencv编译好:

这里的opencv必须要这个链接的版本,我试了一下自己的版本是不可以编译的。

cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=OFF -D WITH_OPENGL=OFF -D BUILD_TIFF=ON ..
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV ready to be used"
先编译安装了opencv然后就是将扩展包编译一下,中途编译时间挺长的。耐心等待...........................

扩展包contrib可以直接命令,也可使用cmake-gui编译,cmake-gui的使用参考opencv_contrib的read.md。

$ cd ~/opencv_caffe/build_opencv
$ cmake -DOPENCV_EXTRA_MODULES_PATH=~/opencv_caffe/opencv_contrib/modules ~/opencv_caffe/opencv-master
$ make -j8
编译完成,中途如果有什么错误可以留言,一起探讨。

然后就是dnn文件加下面的caffe模型加载了。还没有完成linux下面的实现,win7下面实现已经完成。

caffe_googlenet.cpp:

#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>
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)
{
    String modelTxt = "bvlc_googlenet.prototxt";
    String modelBin = "bvlc_googlenet.caffemodel";
    String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";

    //! [Create the importer of Caffe model]
    Ptr<dnn::Importer> importer;
    try                                     //Try to import Caffe GoogleNet model
    {
        importer = dnn::createCaffeImporter(modelTxt, modelBin);
    }
    catch (const cv::Exception &err)        //Importer can throw errors, we will catch them
    {
        std::cerr << err.msg << std::endl;
    }
    //! [Create the importer of Caffe model]

    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;
        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);
    }

    //! [Initialize network]
    dnn::Net net;
    importer->populateNet(net);
    importer.release();                     //We don't need importer anymore
    //! [Initialize network]

    //! [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(img);   //Convert Mat to dnn::Blob image batch
    //! [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
关于cpp文件的一些解释

1、首先下载GoogLeNet model files: bvlc_googlenet.prototxt and bvlc_googlenet.caffemodel

    和 ILSVRC2012类名: synset_words.txt.

 2、导入一个caffe模型接口
    
    Ptr<dnn::Importer> importer;
    try //Try to import Caffe GoogleNet model
    {
    importer = dnn::createCaffeImporter(modelTxt, modelBin);
    }
    catch (const cv::Exception &err) //Importer can throw errors, we will catch them
    {
    std::cerr << err.msg << std::endl;
    }
3、通过接口创建和初始化网络
    dnn::Net net;
    importer->populateNet(net);
    importer.release(); //We don't need importer anymore

4、读取一张图片并转换到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(img); //Convert Mat to dnn::Blob image batch
    首先我们resize图片和变换通道顺序,得到224x224x3的图片,然后转换为1x2x224x224的4维blob类型的数据

 5、将blob输入到网络
    net.setBlob(".data", inputBlob); //set the network input

    In bvlc_googlenet.prototxt the network input blob named as "data", therefore this blob labeled as ".data" in opencv_dnn API.
    Other blobs labeled as "name_of_layer.name_of_layer_output".

 6、进行前向传播
    net.forward(); //compute output

 During the forward pass output of each network layer is computed, but in this example we need output from "prob" layer only.

 7、获取概率值
    dnn::Blob prob = net.getBlob("prob"); //gather output of "prob" layer
    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class
    We put the output of "prob" layer, which contain probabilities for each of 1000 ILSVRC2012 image classes, to the prob blob. And find the index of element with maximal value in this one. This index correspond to the class of the image.

 8、输出结果
    std::vector<String> classNames = readClassNames();
    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
    For our image we get:

        Best class: #812 'space shuttle'

        Probability: 99.6378%

====================================================================================================================================

由于前一段时间比较忙,没来得及完成现在搞定了。记录一下:

之前编译opencv_contrib是在linux下面,每次都会找不到dnn.hpp?很郁闷按照官网教程编译了2天还是不可以,为什么呢?

后来现在被迫在window7下面配置一下了,毕竟官网也是在win7下面的,正好我的win7下面也没有配置caffe,可以试试能否成功。。。。。。。

我的配置环境(win7+opencv3.0+cmake3.3+opencv_contrib+VS2013旗舰版)

因为opencv3.0默认安装中没有加入SIFT、SURF等点特征检测,opencv3的contrib库中才有此方法,欢迎大家与我讨论其他opencv方面的问题。

一、下载安装准备

  下载对应版本的以上软件:opencv3.0.0可以直接解压,我的路径为D:\opencv3,解压完成后会生成D:\opencv3\opencv的目录形式。

  cmake_gui 软件直接安装。OK,现在准备工作就绪。

二、利用CMake编译opencv和opencv_contrib库

  1. 打开cmake_gui.
  2. 把opencv/source文件夹中的CMakeLists.txt文件直接拖入cmake_gui中,where is the source code自动选择opencv的安装目录为D:\opencv3\opencv\source.
  3. 更改where to build the binaries 目录,即我们将要编译的目录,我的是:D:\opencv3\opencv\Mybuild
  4. 点击configure,点击确定,选择默认编译器,点击finish。cmake将开始进行编译,如果一切顺利将最后显示Configure Done!表明我们configure完成,在出现的红色区域内找到OPENCV,子目录中找到OPENCV_EXTRA_MODULES_PATH,设置它的值为opencv_contrib/modules的绝对路径,在这里我的设置为D:\opencv3\opencv\sources\modules\opencv_contrib\modules(之前把opencv_contrib放在source文件夹外面,编译不成功,所以这里只介绍我的方法)。
  5. 可以再红色区域内再次去掉BUILD_OPENCV 和WITH_CUDA等有些硬件不支持的库,再次点击configure,正常情况会成功显示Configure Done! 然后点击Generate生成。

三、利用VS编译Debug和Release库

  1. 打开D:\opencv3\opencv\mybuild目录下的OpenCV.sln
  2. 在CMake Target中找到 INSTALL ,在Debug和Release的条件中分别右键选择build,生成debug和release库,这个过程大概持续10-20分钟。
  3. 我们会在D:\opencv3\opencv\Mybuild\install文件夹中看到这几个文件目录(生成的一定是x86因为我之前不知道怎么回事,每次自动生成的x64然后后面就出错了,如果想编译x64也是勉强能使用的,遇到“无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。”下面可以试一下)
       {
首先打开菜单 项目->项目属性页  
        1。选择 配置属性->链接器->调试->生成调试信息 改为 是  
        2。选择 配置属性->C/C++ ->常规->调试信息格式 改为 用于“编辑并继续”的程序数据库(/ZI)  

        3。选择 配置属性->C/C++ ->优化->优化 改为 禁用(/Od)  

      }

--bin
--etc
--include
   --opencv
   --opencv2
--x86
  --vc10
     --bin
     --lib
     --staticlib
--LICENSE
--OpenCVConfig.cmake
--OpenCVConfig-version.cmake

四、Windows和VS中配置Opencv3

  1. 添加环境变量PATH D:\opencv3\opencv\Mybuild\install\x86\vc10\bin ,记得多个需要用分号(;)隔开。
  2. 任意建立一个新的工程,打开视图(View)->Other Windows->Property Manager。点击Debug,双击Microsoft.Cpp.Win32.user,在其中设置:(以下设置完成之后,重复2的操作,选择Release,同样方式设置Microsoft.Cpp.Win32.user)
    1. VC++目录-->包含目录,添加:
      • D:\opencv3\opencv\Mybuild\install\include
      • D:\opencv3\opencv\Mybuild\install\include\opencv
      • D:\opencv3\opencv\Mybuild\install\include\opencv2
    2. VC++目录-->库目录,添加:
              D:\opencv3\opencv\Mybuild\install\x86\vc12\lib
    3. 链接器-->输入-->附加依赖项,添加:(里面那个库编译过不去可以删除添加,只要你需要的功能还在就行,Itseez也声明,所有的库不一定编译过去)

opencv_aruco300d.lib
opencv_bgsegm300d.lib
opencv_bioinspired300d.lib
opencv_calib3d300d.lib
opencv_ccalib300d.lib
opencv_core300d.lib
opencv_dnn300d.lib
opencv_face300d.lib
opencv_features2d300d.lib
opencv_flann300d.lib
opencv_highgui300d.lib
opencv_imgcodecs300d.lib
opencv_imgproc300d.lib
opencv_line_descriptor300d.lib
opencv_ml300d.lib
opencv_objdetect300d.lib
opencv_optflow300d.lib
opencv_photo300d.lib
opencv_reg300d.lib
opencv_rgbd300d.lib
opencv_saliency300d.lib
opencv_shape300d.lib
opencv_stereo300d.lib
opencv_stitching300d.lib
opencv_superres300d.lib
opencv_surface_matching300d.lib
opencv_video300d.lib
opencv_videoio300d.lib
opencv_videostab300d.lib
opencv_xfeatures2d300d.lib
opencv_ximgproc300d.lib
opencv_xobjdetect300d.lib
opencv_xphoto300d.lib
opencv_aruco300.lib
opencv_bgsegm300.lib
opencv_bioinspired300.lib
opencv_calib3d300.lib
opencv_ccalib300.lib
opencv_core300.lib
opencv_dnn300.lib
opencv_face300.lib
opencv_features2d300.lib
opencv_flann300.lib
opencv_highgui300.lib
opencv_imgcodecs300.lib
opencv_imgproc300.lib
opencv_line_descriptor300.lib
opencv_ml300.lib
opencv_objdetect300.lib
opencv_optflow300.lib
opencv_photo300.lib
opencv_reg300.lib
opencv_rgbd300.lib
opencv_saliency300.lib
opencv_shape300.lib
opencv_stereo300.lib
opencv_stitching300.lib
opencv_superres300.lib
opencv_surface_matching300.lib
opencv_video300.lib
opencv_videoio300.lib
opencv_videostab300.lib
opencv_xfeatures2d300.lib
opencv_ximgproc300.lib
opencv_xobjdetect300.lib
opencv_xphoto300.lib

五、遇到相关问题及解决方法
”无法启动此程序,因为计算机中丢失opencv_core300.dll或opencv_core300d.dll。请尝试重新安装改程序已解决此问题“

解决:只将该dll文件拷贝到C:\Windows\System32或者C:\Windows\SysWOW64是不行的,执行:regsvr32 C:\Windows\System32\opencv_core300d.dll,会报错。我是把dll文件拷贝到C:\Windows\SysWOW64中执行 regsvr32 C:\Windows\SysWOW64\opencv_core300d.dll就好了。

ippicv

解决:如果中途遇到下载失败,可以百度相应的版本,然后去github上下载,然后放在download里面就可以了。

我的项目中cpp代码:

#include "stdafx.h"
#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>
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 = "D:\\loadcaffe_model\\opencv3\\opencv\\sources\\modules\\opencv_contrib\\modules\\dnn\\samples\\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)
{
	String modelTxt = "D:\\loadcaffe_model\\opencv3\\opencv\\sources\\modules\\opencv_contrib\\modules\\dnn\\samples\\bvlc_googlenet.prototxt";
	String modelBin = "D:\\loadcaffe_model\\opencv3\\opencv\\sources\\modules\\opencv_contrib\\modules\\dnn\\samples\\bvlc_googlenet.caffemodel";
	String imageFile = (argc > 1) ? argv[1] : "D:\\loadcaffe_model\\opencv3\\opencv\\sources\\modules\\opencv_contrib\\modules\\dnn\\samples\\space_shuttle.jpg";

	//! [Create the importer of Caffe model]
	Ptr<dnn::Importer> importer;
	try                                     //Try to import Caffe GoogleNet model
	{
		importer = dnn::createCaffeImporter(modelTxt, modelBin);
	}
	catch (const cv::Exception &err)        //Importer can throw errors, we will catch them
	{
		std::cerr << err.msg << std::endl;
	}
	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;
		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);
	}
	//! [Initialize network]
	dnn::Net net;
	importer->populateNet(net);
	importer.release();                     //We don't need importer anymore
	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(img);   //Convert Mat to dnn::Blob image batch

	net.setBlob(".data", inputBlob);        //set the network input

	net.forward();                          //compute 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]
	system("PAUSE");
	return 0;
} //main
下面是我测试加载的结果:


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 30
    评论
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值