opencv facedetection在android sdk ERROR中

时间:2016-06-12 16:53:10

标签: java android opencv android-studio

InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface); 中的

错误 无法解析原始

Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB); 行中 错误 无法解析aInputFrame。

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.saisarath.opencvcv.MainActivity">

<org.opencv.android.JavaCameraView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    android:id="@+id/HelloOpenCvView"
    opencv:show_fps="true"
    opencv:camera_id="any" />

MainActivity.java:

public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2 {

private CameraBridgeViewBase openCvCameraView;
private CascadeClassifier cascadeClassifier;
private Mat grayscaleImage;
private int absoluteFaceSize;


private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
                initializeOpenCVDependencies();
                break;
            default:
                super.onManagerConnected(status);
                break;
        }
    }
};


private void initializeOpenCVDependencies() {


    try {
        // Copy the resource into a temp file so OpenCV can load it
        InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
        File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
        File 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();


        // Load the cascade classifier
        cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
    } catch (Exception e) {

    }


    // And we are ready to go
    openCvCameraView.enableView();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    openCvCameraView = new JavaCameraView(this, -1);
    setContentView(R.layout.activity_main);
    openCvCameraView.setCvCameraViewListener(this);
}

@Override
public void onCameraViewStarted(int width, int height) {
    grayscaleImage = new Mat(height, width, CvType.CV_8UC4);


    // The faces will be a 20% of the height of the screen
    absoluteFaceSize = (int) (height * 0.2);
}

@Override
public void onCameraViewStopped() {

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    // Create a grayscale image
    Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB);


    MatOfRect faces = new MatOfRect();


    // Use the classifier to detect faces
    if (cascadeClassifier != null) {
        cascadeClassifier.detectMultiScale(grayscaleImage, faces, 1.1, 2, 2,
                new Size(absoluteFaceSize, absoluteFaceSize), new Size());
    }


    // If there are any faces found, draw a rectangle around it
    Rect[] facesArray = faces.toArray();
    for (int i = 0; i <facesArray.length; i++)
        Core.rectangle(aInputFrame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0, 255), 3);


    return aInputFrame;//cannot resolve symbol aInputFrame.
}


@Override
public void onResume() {
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);
}}

1 个答案:

答案 0 :(得分:1)

R.raw 
由于位置错误而导致

错误。 我将原始文件夹位置从 main / raw 更改为 main / res / raw 。解决了一个错误。

和  无法解析aInputFrame。  这是由于拼写错误造成的,我在函数参数中将 inputFrame 更改为 aInputFrame

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) //I changed "inputFrame" to aInputFrame {
// Create a grayscale image
Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB);