在Android中的JavaCV中加载库时出错

时间:2015-05-31 07:42:40

标签: android javacv opencv4android

我试图使用javaCV实现一个简单的人脸识别代码。我收到错误并且不知道原因。

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

这是错误:

  

引起:java.lang.UnsatisfiedLinkError:dlopen失败:无法   加载库" libopencv_contrib.so"需要的   " libjniopencv_contrib.so&#34 ;;导致无法加载库   " ../../ LIB / libopencv_legacy.so"需要" libopencv_contrib.so&#34 ;;   由图书馆引起的#34; ../../ 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)               at com.ifta.face.OpenCVFaceRecognizer.recognise(OpenCVFaceRecognizer.java:102)               at com.ifta.face.FinalActivity.testPreviousImage(FinalActivity.java:126)               在com.ifta.face.FinalActivity.onTestClicked(FinalActivity.java:50)               at 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)               at 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(本地方法)

我使用的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]);


}

1 个答案:

答案 0 :(得分:0)

我在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”之类的库必须手动加载。加载序列也必须如上所述,因为库依赖于前者。任何人都知道为什么会这样吗?