【问题标题】:Error in loading libraries in JavaCV in Android在 Android 中加载 JavaCV 中的库时出错
【发布时间】:2015-06-03 10:10:57
【问题描述】:

我正在尝试使用 javaCV 实现一个简单的人脸识别代码。我收到一个错误,不知道为什么。

我已将每个 .so 文件复制到几乎所有目录 -> app/libs/armeabi 和 app/src/main/jniLibs 还将 javacpp.jar 和 javacv.jar 复制到 app/libs 目录

这是错误:

原因:java.lang.UnsatisfiedLinkError: dlopen failed: could not 加载所需的库“libopencv_contrib.so” “libjniopencv_contrib.so”;导致无法加载库 “libopencv_contrib.so”需要的“../../lib/libopencv_legacy.so”; 由找不到库“../../lib/libopencv_legacy.so”引起 在 java.lang.Runtime.load(Runtime.java:330) 在 java.lang.System.load(System.java:511) 在 com.googlecode.javacpp.Loader.loadLibrary(Loader.java:700) 在 com.googlecode.javacpp.Loader.load(Loader.java:586) 在 com.googlecode.javacpp.Loader.load(Loader.java:540) 在 com.googlecode.javacv.cpp.opencv_contrib.(opencv_contrib.java:97) 在 com.ifta.face.OpenCVFaceRecognizer.recognise(OpenCVFaceRecognizer.java:102) 在 com.ifta.face.FinalActivity.testPreviousImage(FinalActivity.java:126) 在 com.ifta.face.FinalActivity.onTestClicked(FinalActivity.java:50) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:525) 在 android.view.View$1.onClick(View.java:3809) 在 android.view.View.performClick(View.java:4421) 在 android.view.View$PerformClick.run(View.java:17903) 在 android.os.Handler.handleCallback(Handler.java:730) 在 android.os.Handler.dispatchMessage(Handler.java:92) 在 android.os.Looper.loop(Looper.java:213) 在 android.app.ActivityThread.main(ActivityThread.java:5225) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:525) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 在 dalvik.system.NativeStart.main(Native Method)

我使用的java函数是:

public static void recognise(Context context, File sampleDir, File testFile) {

    // DEBUG-1
    if(sampleDir!=null)
        log(context, "1. sampleDir OK. " + testFile.getAbsolutePath());
    else {
        log(context, "1. sampleDir is Null");
        return;
    }


    FilenameFilter pngFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".png");
        }
    };

    File[] imageFiles = sampleDir.listFiles(pngFilter);

    // DEBUG-2
    if(imageFiles!=null) {
        log(context, "2. ImageFile OK, Files :");
        for(File imageFile : imageFiles)
            log(context, "File : " + imageFile.getAbsolutePath() );
    }
    else {
        log(context, "2. ImageFile Array Null");
        return;
    }

    // DEBUG-.5
    if(testFile!=null)
        log(context, "2.5. testFile OK. " + testFile.getAbsolutePath());
    else {
        log(context, "2.5. testFile is Null");
        return;
    }

    IplImage testImage = cvLoadImage(testFile.getAbsolutePath());
    // DEBUG-3
    if(imageFiles!=null)
        log(context, "3. testImageIpl OK");
    else {
        log(context, "3. testImageIpl is Null");
        return;
    }

    MatVector images = new MatVector(imageFiles.length);

    int[] labels = new int[imageFiles.length];

    int counter = 0;
    int label;

    IplImage img;
    IplImage grayImg;

    for (File image : imageFiles) {
        // Get image and label:
        img = cvLoadImage(image.getAbsolutePath());
        label = Integer.parseInt(image.getName().split("\\-")[0]);

        // Convert image to gray scale:
        grayImg = IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);
        cvCvtColor(img, grayImg, CV_BGR2GRAY);
        // Append it in the image list:
        images.put(counter, grayImg);
        // And in the labels list:
        labels[counter] = label;
        // Increase counter for next image:
        counter++;
    }

    //FaceRecognizer faceRecognizer = createFisherFaceRecognizer();
    FaceRecognizer faceRecognizer = createEigenFaceRecognizer();
    // FaceRecognizer faceRecognizer = createLBPHFaceRecognizer();

    faceRecognizer.train(images, labels);

    // Load the test image:
    IplImage greyTestImage = IplImage.create(testImage.width(), testImage.height(), IPL_DEPTH_8U, 1);
    cvCvtColor(testImage, greyTestImage, CV_BGR2GRAY);

    // And get a prediction:
    //int predictedLabel = faceRecognizer.predict(greyTestImage);
    //System.out.println("Predicted label: " + predictedLabel);

    int[] imageLabels = new int[1];
    double[] confidences = new double[1];

    faceRecognizer.predict(greyTestImage, imageLabels, confidences);

    log(context, "Result : Label - " + imageLabels[0] + "  Conf - " + confidences[0]);


}

【问题讨论】:

    标签: android javacv opencv4android


    【解决方案1】:

    我在 2 天后解决了这个问题……我想手动加载库。我添加了几行

        System.load("/data/data/com.ifta.face/lib/libopencv_photo.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_flann.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_features2d.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_calib3d.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_ml.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_video.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_legacy.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_objdetect.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_gpu.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_nonfree.so");
        System.load("/data/data/com.ifta.face/lib/libopencv_contrib.so");
    

    有一件事我发现名为“libjni*.so”的库是自动加载的,而像“libopencv_*.so”这样的库必须手动加载。加载顺序也必须像上面提到的那样,因为库依赖于以前的库。有谁知道为什么会这样?

    【讨论】:

    • 你能告诉我应该放入哪些 jar 文件:app/libs/armeabi、app/src/main/jniLibs、app/libs。我正在使用 javacv 1.2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2013-07-14
    • 2021-09-10
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    相关资源
    最近更新 更多