前提・実現したいこと
xcodeにてopencvを使用しようとしたのですが、以下のようなエラーが出ます。
ヘッダーパスは /usr/local/Cellar/opencv
ライブラリパスは /usr/local/Cellar/opencv/3.4.1_5/lib
に設定しています。
ちなみにビルドは成功しています。
発生している問題・エラーメッセージ
dyld: lazy symbol binding failed: Symbol not found: __ZN2cv3dnn19experimental_dnn_v43NetC1Ev Referenced from: /Users/USERNAME/Library/Developer/Xcode/DerivedData/PROJECTNAME-awtuuflshkvodjdcdadhdthlmbwi/Build/Products/Debug/PROJECTNAME Expected in: /usr/local/opt/opencv/lib/libopencv_dnn.3.4.dylib dyld: Symbol not found: __ZN2cv3dnn19experimental_dnn_v43NetC1Ev Referenced from: /Users/USERNAME/Library/Developer/Xcode/DerivedData/PROJECTNAME-awtuuflshkvodjdcdadhdthlmbwi/Build/Products/Debug/PROJECTNAME Expected in: /usr/local/opt/opencv/lib/libopencv_dnn.3.4.dylib
該当のソースコード
C++
1#include <opencv2/dnn.hpp> 2#include <opencv2/imgproc.hpp> 3#include <opencv2/highgui.hpp> 4#include <opencv2/core/utils/trace.hpp> 5#include <fstream> 6#include <iostream> 7#include <cstdlib> 8using namespace cv; 9using namespace cv::dnn; 10using namespace std; 11 12static void getMaxClass(const Mat &probBlob, int *classId, double *classProb) 13{ 14 Mat probMat = probBlob.reshape(1, 1); 15 Point classNumber; 16 minMaxLoc(probMat, NULL, classProb, NULL, &classNumber); 17 *classId = classNumber.x; 18} 19 20static std::vector<String> readClassNames(const char *filename = "./Model/labels.txt") 21{ 22 std::vector<String> classNames; 23 std::ifstream fp(filename); 24 if (!fp.is_open()) 25 { 26 std::cerr << "File with classes labels not found: " << filename << std::endl; 27 exit(-1); 28 } 29 std::string name; 30 while (!fp.eof()) 31 { 32 std::getline(fp, name); 33 if (name.length()) 34 classNames.push_back(name.substr(name.find(' ') + 1)); 35 } 36 fp.close(); 37 return classNames; 38} 39 40 41int main(int argc, const char * argv[]) { 42 43 CV_TRACE_FUNCTION(); 44 String modelTxt = "/USERNAME/testCaffe/Model/deploy.prototxt"; 45 String modelBin = "/USERNAME/testCaffe/Model/snapshot_iter_1500.caffemodel"; 46 String imageFile = "./sample.jpg"; 47 Net net; 48 try 49 { 50 net = dnn::readNetFromCaffe(modelTxt, modelBin); 51 } 52 catch (cv::Exception &e) 53 { 54 std::cerr << "Exception: " << e.what() << std::endl; 55 if (net.empty()) 56 { 57 exit(-1); 58 } 59 } 60 61 net.setPreferableTarget(DNN_TARGET_CPU); 62 63 Mat img = imread(imageFile); 64 if (img.empty()) 65 { 66 std::cerr << "Can't read image from the file: " << imageFile << std::endl; 67 exit(-1); 68 } 69 70 Mat inputBlob = blobFromImage(img, 1.0f, Size(224, 224), false); 71 net.setInput(inputBlob, "data"); 72 Mat prob = net.forward("softmax"); 73 int classId; 74 double classProb; 75 getMaxClass(prob, &classId, &classProb); 76 vector<String> classNames = readClassNames(); 77 cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << endl; 78 cout << "Probability: " << classProb * 100 << "%" << endl; 79 resize(img, img, cv::Size(), 1, 1); 80 imshow("Image", img); 81 waitKey(0); 82 83 return 0; 84} 85
補足情報(FW/ツールのバージョンなど)
macbookpro(10.14.1)
opencv 3.4.1_5
xcode(10.1)
あなたの回答
tips
プレビュー