2016-08-22 1 views
0

iOS 기기에서 OpenCV와 함께 .caffemodel을 사용하려고합니다. 이 github repository을 찾았지만 Xcode 6에서만 빌드 할 수 있습니다. Xcode 7에서 작업 중이지만 Xcode 6을 다운로드했지만 여전히 빌드에 성공하지 못했습니다.iOS에서 OpenCV와 함께 caffemodel을 사용하는 방법은 무엇입니까?

iOS 9에서 OpenCV와 함께 caffemodel을 사용하려면 어떻게해야합니까?

추신 : this이지만 swift & 금속으로 작성되었으며 OpenCV와 함께 사용할 수 있어야합니다.

+0

당신은 그것을 할 수있는 방법을 알아낼나요 :

여기

는 그대로 작동하지 않습니다 때문에, 튜토리얼의 업데이트 버전이다? DNK를 ios와 함께 사용할 수 있습니까? cmakelists에 플래그가 표시되고 dnn 모듈이 apple 프레임 워크 및 winrt에 대해 비활성화됩니다. – 4nonymou5

답변

1

OpenCV DNN contrib module을 사용할 수 있습니다.

먼저 contrib 모듈을 사용하여 OpenCV를 빌드해야합니다. here 단계를 찾을 수 있습니다.

다음 this tutorial 다음에 .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 
+0

답변 해 주셔서 감사합니다. 또한이 튜토리얼을 발견하고 내 컴퓨터 (iOS 용이 아님)에서 실행하려고했습니다. 그러나,이 함수'dnn :: Blob :: fromImages (img);에 문제가있어서 찾을 수 없습니다. 그러나 이것을'cv :: dnn :: initModule(); '이라고 해보자. 나는 돌아올 것이다. 또한'contrib_modules'도 iOS opencv 버전에 없습니까? – cch

+0

아니요, contrib 모듈이 사전 빌드 바이너리에 없습니다. 나는이 튜토리얼을 처음 실행하는 데 몇 가지 어려움을 겪었다. D – Miki

+0

그래,'cv :: dnn :: initModule()'을 사용해 보았으나'cv :: dnn :: Blob'의 'Images '라는 이름의 멤버가 없다는 오류가 발생한다. – cch