Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Linker error only with DNN module

I work on a C++ project with OpenCV 4.1.0_2 on the latest macOS Mojave with Xcode 11 beta. I used a lot of elements from the library without any problem but when I try to use the DNN module I got a linker error. Linker flags should not be the problem since I added it all, and because every other module I used is link fine I guess the header and source directories are should be fine as well. What could be the problem? The error messages:

Ld /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug/OpenCV normal x86_64 (in target: OpenCV)
    cd /Users/hordon/Desktop/GreenFox/OpenCV
    /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -target x86_64-apple-macos10.14 -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -L/Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug -L/usr/local/Cellar/opencv/4.1.0_2/lib -L/usr/local/Cellar/sqlite/3.28.0/lib -L/usr/local/Cellar/tesseract/4.0.0_1/lib -L/usr/local/Cellar/leptonica/1.78.0/lib -F/Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug -filelist /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Intermediates.noindex/project.build/Debug/OpenCV.build/Objects-normal/x86_64/OpenCV.LinkFileList -Xlinker -object_path_lto -Xlinker /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Intermediates.noindex/project.build/Debug/OpenCV.build/Objects-normal/x86_64/OpenCV_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -stdlib=libc++ -I/usr/local/Cellar/opencv/4.1.0_2/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.1.0_2/include/opencv4 -L/usr/local/Cellar/opencv/4.1.0_2/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core -ltesseract -lsqlite3 -Xlinker -dependency_info -Xlinker /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Intermediates.noindex/project.build/Debug/OpenCV.build/Objects-normal/x86_64/OpenCV_dependency_info.dat -o /Users/hordon/Library/Developer/Xcode/DerivedData/project-fmllzddvpyajkjbwntzsyqvmunjx/Build/Products/Debug/OpenCV

and

Undefined symbols for architecture x86_64:
  "cv::dnn::dnn4_v20180917::blobFromImage(cv::_InputArray const&, cv::_OutputArray const&, double, cv::Size_<int> const&, cv::Scalar_<double> const&, bool, bool, int)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::Net::forward(cv::_OutputArray const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::Net::setInput(cv::_InputArray const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, double, cv::Scalar_<double> const&)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::Net::~Net()", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::readNet(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
  "cv::dnn::dnn4_v20180917::NMSBoxes(std::__1::vector<cv::RotatedRect, std::__1::allocator<cv::RotatedRect> > const&, std::__1::vector<float, std::__1::allocator<float> > const&, float, float, std::__1::vector<int, std::__1::allocator<int> >&, float, int)", referenced from:
      detectText(cv::Mat, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in detectText.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

and a part from the cpp file which generates this error is:

std::vector<cv::Mat> detectText(cv::Mat image, std::string path)
{
    std::vector<cv::Mat> result;

    if (image.empty()) {
        return result;
    }

    cv::dnn::Net net = cv::dnn::readNet(path);

    float minConfidence = 0.5;
    float NMSThreshold = 0.4;
    int width = 320;
    int height = 320;

    cv::Mat resized;
    cv::resize(image, resized, cv::Size(width, height));

    std::vector<cv::Mat> layers;
    std::vector<std::string> layerNames = {"feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"};

    cv::Mat blob;
    cv::dnn::blobFromImage(resized, blob, 1.0, cv::Size(width, height), cv::Scalar(123.68, 116.78, 103.94), true, false);
    net.setInput(blob);
    net.forward(layers, layerNames);

    cv::Mat scores = layers.at(0);
    cv::Mat geometry = layers.at(1);

    std::vector<cv::RotatedRect> rects;
    std::vector<float>confidenceScores;

    decodeOutput(scores, geometry, minConfidence, rects, confidenceScores);

    std::vector<int> indicies;
    cv::dnn::NMSBoxes(rects, confidenceScores, minConfidence, NMSThreshold, indicies);

    float ratioWidth = image.cols / (float)width;
    float ratioHeight = image.rows / (float)height;

    for (int i = 0; i < indicies.size(); ++i) {
        cv::RotatedRect& rect = rects[indicies[i]];

        rect.size.width *= ratioWidth;
        rect.size.height *= ratioHeight;
        rect.center.x *= ratioWidth;
        rect.center.y *= ratioHeight;

        cv::Mat cropped = rotatedRectToMat(rect, image);
        result.push_back(cropped);
    }

    return result;
}