嘿我正在使用OpenCV示例 - 面部检测我正在尝试使用静态初始化,因此我不必下载包管理器以使其工作 我已经添加了OpenCV库-2.4.9 我还将库添加到libs文件夹中,但在构建应用程序时它们总是被删除 我还将Android Mk文件更改为这样 LOCAL_PATH:= $(调用my-dir)
include $(CLEAR_VARS)
#OPENCV_CAMERA_MODULES:=on
#OPENCV_INSTALL_MODULES:=on
#OPENCV_LIB_TYPE:=SHARED
include ../../sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
我还在FdActivity上添加了静态初始化声明 这就是它的样子
package org.opencv.samples.facedetect;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.objdetect.CascadeClassifier;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
public class FdActivity extends Activity implements CvCameraViewListener2 {
static {
if(!OpenCVLoader.initDebug()) {
Log.d("ERROR", "Unable to load OpenCV");
} else {
Log.d("SUCCESS", "OpenCV loaded");
}
}
private static final String TAG = "OCVSample::Activity";
private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);
public static final int JAVA_DETECTOR = 0;
public static final int NATIVE_DETECTOR = 1;
private MenuItem mItemFace50;
private MenuItem mItemFace40;
private MenuItem mItemFace30;
private MenuItem mItemFace20;
private MenuItem mItemType;
private Mat mRgba;
private Mat mGray;
private File mCascadeFile;
private CascadeClassifier mJavaDetector;
private DetectionBasedTracker mNativeDetector;
private int mDetectorType = JAVA_DETECTOR;
private String[] mDetectorName;
private float mRelativeFaceSize = 0.2f;
private int mAbsoluteFaceSize = 0;
private CameraBridgeViewBase mOpenCvCameraView;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// Load native library after(!) OpenCV initialization
System.loadLibrary("detection_based_tracker");
try {
// load cascade file from application resources
InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
if (mJavaDetector.empty()) {
Log.e(TAG, "Failed to load cascade classifier");
mJavaDetector = null;
} else
Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());
mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);
cascadeDir.delete();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
}
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public FdActivity() {
mDetectorName = new String[2];
mDetectorName[JAVA_DETECTOR] = "Java";
mDetectorName[NATIVE_DETECTOR] = "Native (tracking)";
Log.i(TAG, "Instantiated new " + this.getClass());
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.face_detect_surface_view);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view);
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
@Override
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
//OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
}
public void onDestroy() {
super.onDestroy();
mOpenCvCameraView.disableView();
}
public void onCameraViewStarted(int width, int height) {
mGray = new Mat();
mRgba = new Mat();
}
public void onCameraViewStopped() {
mGray.release();
mRgba.release();
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
if (mAbsoluteFaceSize == 0) {
int height = mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0) {
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
}
MatOfRect faces = new MatOfRect();
if (mDetectorType == JAVA_DETECTOR) {
if (mJavaDetector != null)
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
}
else if (mDetectorType == NATIVE_DETECTOR) {
if (mNativeDetector != null)
mNativeDetector.detect(mGray, faces);
}
else {
Log.e(TAG, "Detection method is not selected!");
}
Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++)
Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
return mRgba;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "called onCreateOptionsMenu");
mItemFace50 = menu.add("Face size 50%");
mItemFace40 = menu.add("Face size 40%");
mItemFace30 = menu.add("Face size 30%");
mItemFace20 = menu.add("Face size 20%");
mItemType = menu.add(mDetectorName[mDetectorType]);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
if (item == mItemFace50)
setMinFaceSize(0.5f);
else if (item == mItemFace40)
setMinFaceSize(0.4f);
else if (item == mItemFace30)
setMinFaceSize(0.3f);
else if (item == mItemFace20)
setMinFaceSize(0.2f);
else if (item == mItemType) {
int tmpDetectorType = (mDetectorType + 1) % mDetectorName.length;
item.setTitle(mDetectorName[tmpDetectorType]);
setDetectorType(tmpDetectorType);
}
return true;
}
private void setMinFaceSize(float faceSize) {
mRelativeFaceSize = faceSize;
mAbsoluteFaceSize = 0;
}
private void setDetectorType(int type) {
if (mDetectorType != type) {
mDetectorType = type;
if (type == NATIVE_DETECTOR) {
Log.i(TAG, "Detection Based Tracker enabled");
mNativeDetector.start();
} else {
Log.i(TAG, "Cascade detector enabled");
mNativeDetector.stop();
}
}
}
}
我收到错误,相机无法在此打开日志文件
07-30 11:30:32.989: D/OpenCV/StaticHelper(6088): Trying to get library list
07-30 11:30:32.989: E/OpenCV/StaticHelper(6088): OpenCV error: Cannot load info library for OpenCV
07-30 11:30:32.989: D/OpenCV/StaticHelper(6088): Library list: ""
07-30 11:30:32.989: D/OpenCV/StaticHelper(6088): First attempt to load libs
07-30 11:30:32.989: D/OpenCV/StaticHelper(6088): Trying to init OpenCV libs
07-30 11:30:32.989: D/OpenCV/StaticHelper(6088): Trying to load library opencv_java
07-30 11:30:32.999: D/OpenCV/StaticHelper(6088): Cannot load library "opencv_java"
07-30 11:30:32.999: W/System.err(6088): java.lang.UnsatisfiedLinkError: Couldn't load opencv_java from loader dalvik.system.PathClassLoader[dexPath=/data/app/org.opencv.samples.facedetect- 2.apk,libraryPath=/data/app-lib/org.opencv.samples.facedetect-2]: findLibrary returned null
07-30 11:30:33.029: W/System.err(6088): at java.lang.Runtime.loadLibrary(Runtime.java:365)
07-30 11:30:33.029: W/System.err(6088): at java.lang.System.loadLibrary(System.java:535)
07-30 11:30:33.029: W/System.err(6088): at org.opencv.android.StaticHelper.loadLibrary(StaticHelper.java:64)
07-30 11:30:33.029: W/System.err(6088): at org.opencv.android.StaticHelper.initOpenCVLibs(StaticHelper.java:95)
07-30 11:30:33.029: W/System.err(6088): at org.opencv.android.StaticHelper.initOpenCV(StaticHelper.java:39)
07-30 11:30:33.029: W/System.err(6088): at org.opencv.android.OpenCVLoader.initDebug(OpenCVLoader.java:56)
07-30 11:30:33.029: W/System.err(6088): at org.opencv.samples.facedetect.FdActivity.<clinit>(FdActivity.java:32)
07-30 11:30:33.029: W/System.err(6088): at java.lang.Class.newInstanceImpl(Native Method)
07-30 11:30:33.029: W/System.err(6088): at java.lang.Class.newInstance(Class.java:1319)
07-30 11:30:33.029: W/System.err(6088): at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
07-30 11:30:33.029: W/System.err(6088): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
07-30 11:30:33.029: W/System.err(6088): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
07-30 11:30:33.029: W/System.err(6088): at android.app.ActivityThread.access$700(ActivityThread.java:150)
07-30 11:30:33.029: W/System.err(6088): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
07-30 11:30:33.029: W/System.err(6088): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 11:30:33.029: W/System.err(6088): at android.os.Looper.loop(Looper.java:176)
07-30 11:30:33.029: W/System.err(6088): at android.app.ActivityThread.main(ActivityThread.java:5279)
07-30 11:30:33.029: W/System.err(6088): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 11:30:33.029: W/System.err(6088): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 11:30:33.029: W/System.err(6088): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-30 11:30:33.029: W/System.err(6088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-30 11:30:33.029: W/System.err(6088): at dalvik.system.NativeStart.main(Native Method)
07-30 11:30:33.029: D/OpenCV/StaticHelper(6088): First attempt to load libs fails
07-30 11:30:33.029: D/ERROR(6088): Unable to load OpenCV
07-30 11:30:33.029: I/OCVSample::Activity(6088): Instantiated new class org.opencv.samples.facedetect.FdActivity
07-30 11:30:33.049: I/OCVSample::Activity(6088): called onCreate
07-30 11:30:33.109: D/CameraBridge(6088): Attr count: 3
07-30 11:30:33.109: I/OCVSample::Activity(6088): OpenCV loaded successfully
07-30 11:30:33.109: D/dalvikvm(6088): Trying to load lib /data/app-lib/org.opencv.samples.facedetect-2/libdetection_based_tracker.so 0x420de340
07-30 11:30:33.119: D/(6088): calling hookDlopen()!
07-30 11:30:33.119: D/(6088): calling hookCheckABI2Header()!
07-30 11:30:33.119: E/dalvikvm(6088): dlopen("/data/app-lib/org.opencv.samples.facedetect-2/libdetection_based_tracker.so") failed: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libopencv_java.so" needed by "libdetection_based_tracker.so"; caused by load_library(linker.cpp:745): library "libopencv_java.so" not found
07-30 11:30:33.119: D/AndroidRuntime(6088): Shutting down VM
07-30 11:30:33.119: W/dalvikvm(6088): threadid=1: thread exiting with uncaught exception (group=0x41ab8e10)
07-30 11:30:33.119: E/AndroidRuntime(6088): FATAL EXCEPTION: main
07-30 11:30:33.119: E/AndroidRuntime(6088): java.lang.UnsatisfiedLinkError: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libopencv_java.so" needed by "libdetection_based_tracker.so"; caused by load_library(linker.cpp:745): library "libopencv_java.so" not found
07-30 11:30:33.119: E/AndroidRuntime(6088): at java.lang.Runtime.loadLibrary(Runtime.java:371)
07-30 11:30:33.119: E/AndroidRuntime(6088): at java.lang.System.loadLibrary(System.java:535)
07-30 11:30:33.119: E/AndroidRuntime(6088): at org.opencv.samples.facedetect.FdActivity$1.onManagerConnected(FdActivity.java:73)
07-30 11:30:33.119: E/AndroidRuntime(6088): at org.opencv.samples.facedetect.FdActivity.onCreate(FdActivity.java:133)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.app.Activity.performCreate(Activity.java:5267)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.app.ActivityThread.access$700(ActivityThread.java:150)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.os.Looper.loop(Looper.java:176)
07-30 11:30:33.119: E/AndroidRuntime(6088): at android.app.ActivityThread.main(ActivityThread.java:5279)
07-30 11:30:33.119: E/AndroidRuntime(6088): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 11:30:33.119: E/AndroidRuntime(6088): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 11:30:33.119: E/AndroidRuntime(6088): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-30 11:30:33.119: E/AndroidRuntime(6088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-30 11:30:33.119: E/AndroidRuntime(6088): at dalvik.system.NativeStart.main(Native Method)
请帮我看看如何解决这个问题
答案 0 :(得分:1)
请更改
的路径../../sdk/native/jni/OpenCV.mk
到您的电脑中放置此OpenCV sdk的实际路径。 就像在我的系统中一样
/home/onkar_sharma/Documents/OpenCV_Android/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk