Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在iOS上使用带OpenCV的caffemodel?_C++_Ios_Opencv_Caffe - Fatal编程技术网

C++ 如何在iOS上使用带OpenCV的caffemodel?

C++ 如何在iOS上使用带OpenCV的caffemodel?,c++,ios,opencv,caffe,C++,Ios,Opencv,Caffe,我试图在iOS设备上与OpenCV一起使用.caffemodel。我发现了这个,但它只能用Xcode 6构建。我正在使用Xcode 7,但我也下载了Xcode 6,但仍然没有成功构建它 如何在iOS 9上使用带有OpenCV的caffemodel 另一种选择是,它是用swift&metal编写的,我需要能够将它与OpenCV一起使用。您可以使用 您首先需要使用contrib模块构建OpenCV,您可以找到以下步骤 然后可以导入并使用下面的.caffemodel 以下是本教程的更新版本,因为它不能

我试图在iOS设备上与OpenCV一起使用
.caffemodel
。我发现了这个,但它只能用Xcode 6构建。我正在使用Xcode 7,但我也下载了Xcode 6,但仍然没有成功构建它

如何在iOS 9上使用带有OpenCV的caffemodel

另一种选择是,它是用swift&metal编写的,我需要能够将它与OpenCV一起使用。

您可以使用

您首先需要使用contrib模块构建OpenCV,您可以找到以下步骤

然后可以导入并使用下面的
.caffemodel

以下是本教程的更新版本,因为它不能正常工作:

#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)
{
    cv::dnn::initModule();        

    String modelTxt = "bvlc_googlenet.prototxt";
    String modelBin = "bvlc_googlenet.caffemodel";
    String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";
    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);
    }
    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 batch of images
    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
    std::vector<String> classNames = readClassNames();
    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
    return 0;
} //main
#包括
#包括
#包括
使用名称空间cv;
使用名称空间cv::dnn;
#包括
#包括
#包括
使用名称空间std;
/*找到blob的最佳类(即具有最大概率的类)*/
void getMaxClass(dnn::Blob&probBlob,int*classId,double*classProb)
{
Mat probMat=probBlob.matRefConst().重塑(1,1);//将blob重塑为1x1000矩阵
点类号;
minMaxLoc(probMat、NULL、classProb、NULL和classNumber);
*classId=classNumber.x;
}
std::vector readClassNames(const char*filename=“synset_words.txt”)
{
std::向量类名称;
std::ifstream fp(文件名);
如果(!fp.is_open())
{

我将发布另一个答案,因为与最新版本有一些不同

首先,现在
dnn
已经在标准OpenCV库中,因此您不必从
contrib_模块构建它

加载网络的功能是
readnetfromcafe

例如,以下代码加载NN:

  std::string modelName = "path/to/mymodel.caffemodel";

  std::string protoName = "path/to/deploy.prototxt";
  cv::dnn::Net net;
  try
  {
    net = cv::dnn::readNetFromCaffe(protoName, modelName);
  }
  catch (cv::Exception& e)
  {
    std::cerr << "Exception: " << e.what() << std::endl;
    if (net.empty())
    {
      std::cerr << "Can't load network by using the following files: " << std::endl;
      std::cerr << "prototxt:   " << protoName << std::endl;
      std::cerr << "caffemodel: " << modelName << 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);
    }
  }
std::string modelName=“path/to/mymodel.caffemodel”;
std::string protoName=“path/to/deploy.prototxt”;
cv::dnn::Net;
尝试
{
net=cv::dnn::readnetfromcafe(原型名、模型名);
}
捕获(cv::异常和e)
{

std::cerr谢谢你的回答。我也找到了本教程,并尝试在我的机器上运行它(不适用于iOS)。但是,我的函数
dnn::Blob::fromImages(img);
找不到它。但是让我用
cv::dnn::initModule()试试看
我会回来的。
contrib_模块
也不在iOS opencv版本中,对吗?不,contrib模块不在预构建二进制文件中。我第一次运行本教程时也遇到了一些困难。所以,让我知道;好吧,所以我尝试使用
cv::dnn::initModule()
但仍然会出现此错误
在cv::dnn::Blob中没有名为“fromImages”的成员
我确信我有OpenCV和contrib模块您有哪个OpenCV版本?您找到方法了吗?是否可以在ios中使用dnn,正如我在CmakeList中看到的那样,dnn模块将被apple framework和winrt禁用。是的,我想要ed也这么做了,发现自己也处于同样的情况。但是使用了更新版本的OpenCV
  cv::Mat res_mat;
  float res;
  cv::Mat inputBlob = cv::dnn::blobFromImage(roi, 1.0f, cv::Size(227, 227),
                                  cv::Scalar(0, 0, 0), false);
  net.setInput(inputBlob);
  //During the forward pass output of each network layer is computed,
  //but in this example we need output from "prob" layer only.
  res_mat = net.forward("score");
  std::cout<<res_mat<<std::endl;
  res_mat = res_mat.reshape(1, 1); //reshape the blob to 1x2 matrix
  return res_mat.at<float>(0);