简体   繁体   中英

What is my error using opencv4android and haarcascade

I'm trying to create one application that take pictures from frontal camera than analyze it using Viola-Jones algorithm. Is there a way?

I am following this example Face Detection using Haar Cascades (I'm passing this code to Java) but my application is giving an error.

package br.ufla.violajones;

import org.opencv.objdetect.CascadeClassifier;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

private CascadeClassifier faceCascade = new CascadeClassifier("haarcascade_frontalface_default.xml");
private CascadeClassifier eyeCascade = new CascadeClassifier("haarcascade_eye.xml");
private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = getApplicationContext();      

                ImageView imgView = (ImageView) findViewById(R.id.image);
        Bitmap img =        BitmapFactory.decodeResource(context.getResources(),R.drawable.lena);

        imgView.setImageBitmap(img);    
    }
}

And here is my error. What is my problem?

05-26 06:16:20.085: W/dalvikvm(12355): No implementation found for native Lorg/opencv/objdetect/CascadeClassifier;.CascadeClassifier_1:(Ljava/lang/String;)J
05-26 06:16:20.090: D/AndroidRuntime(12355): Shutting down VM
05-26 06:16:20.090: W/dalvikvm(12355): threadid=1: thread exiting with uncaught exception (group=0x418ddc80)
05-26 06:16:20.095: E/AndroidRuntime(12355): FATAL EXCEPTION: main
05-26 06:16:20.095: E/AndroidRuntime(12355): Process: br.ufla.violajones, PID: 12355
05-26 06:16:20.095: E/AndroidRuntime(12355): java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.objdetect.CascadeClassifier.CascadeClassifier_1:(Ljava/lang/String;)J
05-26 06:16:20.095: E/AndroidRuntime(12355):    at org.opencv.objdetect.CascadeClassifier.CascadeClassifier_1(Native Method)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at org.opencv.objdetect.CascadeClassifier.<init>(CascadeClassifier.java:58)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at br.ufla.violajones.MainActivity.<init>(MainActivity.java:15)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at java.lang.Class.newInstanceImpl(Native Method)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at java.lang.Class.newInstance(Class.java:1208)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2134)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.os.Looper.loop(Looper.java:136)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at android.app.ActivityThread.main(ActivityThread.java:5088)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at java.lang.reflect.Method.invokeNative(Native Method)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at java.lang.reflect.Method.invoke(Method.java:515)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-26 06:16:20.095: E/AndroidRuntime(12355):    at dalvik.system.NativeStart.main(Native Method)
05-26 06:16:21.935: I/Process(12355): Sending signal. PID: 12355 SIG: 9
05-26 06:17:44.910: D/libEGL(12674): loaded /system/lib/egl/libEGL_mali.so
05-26 06:17:44.915: D/libEGL(12674): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-26 06:17:44.915: D/libEGL(12674): loaded /system/lib/egl/libGLESv2_mali.so
05-26 06:17:44.930: E/(12674): Device driver API match
05-26 06:17:44.930: E/(12674): Device driver API version: 23
05-26 06:17:44.930: E/(12674): User space API version: 23 
05-26 06:17:44.930: E/(12674): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct  9 21:05:57 KST 2013 
05-26 06:17:44.970: D/OpenGLRenderer(12674): Enabling debug mode 0

you can't use opencv code, until the BaseLoader finished loading the native libs

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        if (status == LoaderCallbackInterface.SUCCESS ) {

            // now we can call opencv code !

        } else {
            super.onManagerConnected(status);
        }
    }
};

@Override
public void onResume() {;
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
    // you may be tempted, to do something here, but it's *async*, and may take some time,
    // so any opencv call here will lead to unresolved native errors.
}

another thing to keep in mind is, that those opencv(c++ native) functions can't read from a zip or apk.

you will have to copy your cascade files to some other place, like sdcard, and load it from there.

I think this error java.lang.UnsatisfiedLinkError: Native method not found happened because native libraries not found, maybe because they are not linked properly. You may want to take a look here and here on a detailed documentation how to use OpenCV with Android apps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM