opencv调用caffemodle报'Blob' is not a member of 'CV:dnn',使用的opencv3.3.1请问各位大神有没有遇到过。x谢谢

报错信息如下:
 
/home/chenlei2830/mytest_cpp/lpr_study/15-qt_mnist/qt_mnist/main.cpp:17: error: variable or field 'getMaxClass' declared void
 void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb)
 
/home/chenlei2830/mytest_cpp/lpr_study/15-qt_mnist/qt_mnist/main.cpp:17: error: 'Blob' is not a member of 'cv::dnn'
 void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb)
                  ^
/home/chenlei2830/mytest_cpp/lpr_study/15-qt_mnist/qt_mnist/main.cpp:17: error: 'probBlob' was not declared in this scope
 void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb)
                             ^                  
 
 
 
一下为测试代码,在qt上运行。
 
QT += core
QT -= gui

TARGET = qt_mnist
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    alg.cpp

INCLUDEPATH += /usr/local/include/opencv \
               /usr/local/include/opencv2\
               /usr/local/include/opencv2/dnn\
               /home/chenlei2830/ncnn/ncnn/build/install/include

LIBS += /usr/local/lib/libopencv_video.so   \
  /usr/local/lib/libopencv_objdetect.so     \
  /usr/local/lib/libopencv_ml.so            \
  /usr/local/lib/libopencv_core.so          \
  /usr/local/lib/libopencv_features2d.so    \
  /usr/local/lib/libopencv_imgproc.so       \
  /usr/local/lib/libopencv_highgui.so       \
  /usr/local/lib/libopencv_flann.so         \
  /usr/local/lib/libopencv_calib3d.so       \
  /usr/local/lib/libopencv_imgcodecs.so     \
  /usr/local/lib/libopencv_dnn.so           \
  /home/chenlei2830/ncnn/ncnn/build/install/lib/libncnn.a \
 -fopenmp

QMAKE_CXXFLAGS += -fopenmp
QMAKE_CXXFLAGS += -std=c++11


HEADERS += \
    alg.h
源码
#include <QCoreApplication>
#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;



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



int main(int argc,char* argv[]){

    String modelTxt = "mnist_deploy.prototxt";
    String modelBin = "lenet_iter_10000.caffemodel";
    String imageFile = (argc > 1) ? argv[1] : "5.jpg";

    //! [Create the importer of Caffe model] 导入一个caffe模型接口
    Ptr<dnn::Importer> importer;
    importer = 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);
    }

    //! [Initialize network] 通过接口创建和初始化网络
    Net net;
    importer->populateNet(net);
    importer.release();

    //! [Prepare blob] 读取一张图片并转换到blob数据存储
    Mat img = imread(imageFile,0); //[<Important>] "0" for 1 channel, Mnist accepts 1 channel
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }
    resize(img, img, Size(28, 28));                   //[<Important>]Mnist accepts only 28x28 RGB-images

    dnn::Blob inputBlob = cv::dnn::Blob(img);   //Convert Mat to dnn::Blob batch of images

    //! [Set input blob] 将blob输入到网络
    net.setBlob(".data", inputBlob);        //set the network input

    //! [Make forward pass] 进行前向传播
    net.forward();                          //compute output

    //! [Gather output] 获取概率值
    dnn::Blob prob = net.getBlob("prob");   //[<Important>] gather output of "prob" layer
    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class

    //! [Print results] 输出结果
    std::cout << "Best class: #" << classId << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;

    return 0;
}

 
已邀请:

Eleven - 90后IT男

赞同来自:

opencv3.4.1做了改动,dnn.hpp没有Blob,参考如下用法
int main(int argc,char* argv[]){

String modelTxt = "mnist_deploy.prototxt";
String modelBin = "lenet_iter_10000.caffemodel";
String imageFile = (argc > 1) ? argv[1] : "5.jpg";

//! [Create the importer of Caffe model] 导入一个caffe模型接口
Net net = dnn::readNetFromCaffe(modelTxt,modelBin);

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;
exit(-1);
}

//! [Prepare blob] 读取一张图片并转换到blob数据存储
Mat img = imread(imageFile,0); //[<Important>] "0" for 1 channel, Mnist accepts 1 channel
if (img.empty())
{
std::cerr << "Can't read image from the file: " << imageFile << std::endl;
exit(-1);
}
// resize(img, img, Size(28, 28)); //[<Important>]Mnist accepts only 28x28 RGB-images

// dnn::Blob inputBlob = cv::dnn::Blob(img); //Convert Mat to dnn::Blob batch of images
Mat inputBlob = blobFromImage(img, 1, Size(28, 28));

Mat prob;
for(int i = 0; i < 10; i++)
{
net.setInput(inputBlob, "data");
prob = net.forward("prob");
}
Mat probMat = prob.reshape(1, 1); //reshape the blob to 1x1000 matrix // 1000个分类
Point classNumber;
double classProb;
minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
int classIdx = classNumber.x; // 分类索引号
printf("n current image classification : %d, possible : %.2f n", classIdx, classProb);
// putText(img, labels.at(classIdx), Point(20, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2, 8);
// imshow("Image Category", img);
// waitKey(0);
return 0;

//! [Set input blob] 将blob输入到网络
// net.setInput(inputBlob,"data"); //set the network input

// //! [Make forward pass] 进行前向传播
// net.forward(); //compute output
// // Mat probMat = probBlob.matRefConst().reshape(1, 1);
// Mat probMat = prob.reshape(1, 1);
// //! [Gather output] 获取概率值
// dnn::Blob prob = net.getBlob("prob"); //[<Important>] gather output of "prob" layer
// int classId;
// double classProb;
// getMaxClass(prob, &classId, &classProb);//find the best class

// //! [Print results] 输出结果
// std::cout << "Best class: #" << classId << "'" << std::endl;
// std::cout << "Probability: " << classProb * 100 << "%" << std::endl;

// return 0;
}

要回复问题请先登录注册