the app shuts down suddenly and then back on

asked 2017-07-06 00:24:11 -0600

I am making face recognition application now.

I used a Haar cascade and it suddenly ends after normal operation for about one minute. Then it turns on again. In fact, the camera input is gradually delayed over time The code looks like this, and no errors are found in the log.

public class StartActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 { static { System.loadLibrary("opencv_java3"); } private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);

private static final String  TAG = "OCVApplication::MainActivity";
private CameraBridgeViewBase mOpenCvCameraView;
private CascadeClassifier mJavaDetector;
private Mat mRgba;
private Mat mGray;


private float mRelativeFaceSize = 0.3f;
private int mAbsoluteFaceSize = 0;




private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");

                try {
                    // load cascade file from application resources
                    InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
                    File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
                    //      lbpcascade_frontalface.xml           haarcascade_frontalface_alt2.xml
                    File mCascadeFile = new File(cascadeDir, "haarcascade_frontalface_alt2.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());
                    mJavaDetector.load(mCascadeFile.getAbsolutePath());
                    if (mJavaDetector.empty()) {
                        Log.e(TAG, "Failed to load cascade classifier for face");
                        mJavaDetector = null;
                    } else
                        Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());

                    cascadeDir.delete();

                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
                }
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view_be);
    mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
    mOpenCvCameraView.setCvCameraViewListener(this);


    if (!OpenCVLoader.initDebug()) {
        Log.e(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), not working.");
    } else {
        Log.d(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), working.");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }

    mOpenCvCameraView.enableView();

}

@Override
public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    mGray = new Mat(height, width, CvType.CV_8UC1);
}
@Override
public void onCameraViewStopped() {
    mRgba.release();
    mGray.release();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame){
    //process before you return the frame
    mRgba = inputFrame.rgba(); 
    mGray = inputFrame.gray();

    MatOfRect faces = new MatOfRect();


    if (mAbsoluteFaceSize == 0) {
        int height = mGray.rows();
        if (Math.round(height * mRelativeFaceSize) > 0) {
            mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
        }
    }

    Core.flip(mGray.t(), mGray, 0);

    if (mJavaDetector != null) {
        mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
    }
    else {
        Log.e(TAG, "Detection method is not selected!");
    }


    Rect[] facesArray = faces.toArray();

    for (Rect aFacesArray : facesArray) {
        double tlx = mRgba.width() - aFacesArray.tl().y;
        double tly = aFacesArray.tl().x;
        double brx = mRgba.width() - aFacesArray.br().y;
        double bry = aFacesArray.br().x;
        int W = (int)(brx-tlx);
        int H = (int)(bry-tly);
        Imgproc.rectangle(mRgba, new Point(tlx, tly), new Point(brx, bry), FACE_RECT_COLOR, 3);
        Rect roi = new Rect((int)(tlx+W*0.5),(int)(tly+H*0.7),(int)(W*0.15),(int)(H*0.15));
        Imgproc.rectangle(mRgba, new Point(roi.x, roi.y), new Point(roi.x+roi.width, roi.y+roi.height), FACE_RECT_COLOR, 3 ...
(more)
edit retag flag offensive close merge delete