我正在尝试使用javaCV实现一个简单的人脸识别代码。我得到一个错误,不知道为什么。
我已经复制了每一个。so文件到几乎所有的目录-> app/libs/armeabi和app/src/main/jniLibs也复制javacpp.jar和javacv.jar到app/libs目录
错误如下:
原因:java.lang.UnsatisfiedLinkError: dlopen failed: could not加载libopencv_contrib库。所以" needed by "libjniopencv_contrib.so";无法加载库". ./. ./lib/libopencv_legacy。所以"libopencv_contrib.so"需要;由库"../../lib/libopencv_legacy. "引起。所以"未找到"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(本地方法)java.lang.reflect.Method.invoke (Method.java: 525)在1.美元android.view.View 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(本地方法)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。主要(本地方法)
我使用的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]);
}
我在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*"。因此,"libopencv_*"是自动加载的,而"libopencv_*"是自动加载的。所以必须手动加载。此外,加载顺序必须像上面提到的那样,因为库依赖于先前的库。有人知道为什么会这样吗?