Ask Your Question

Revision history [back]

How to resolve ExceptionsINinitializer Error

Hello OpenCV,

Apologize I know I am gonna asking this question twice but I was not familiar with it earlier.

Anyway when I compile the code I got error in my activity at File. . . private File mCascadeFile; This is how i initialize File object. . .Bellow is final code. . .

public FdView(Context context) { super(context);

    try {
        InputStream is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface);
        File cascadeDir = context.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();


        // --------------------------------- load left eye classificator -----------------------------------
        InputStream iser = context.getResources().openRawResource(R.raw.haarcascade_lefteye_2splits);
        File cascadeDirER = context.getDir("cascadeER", Context.MODE_PRIVATE);
        File cascadeFileER = new File(cascadeDirER, "haarcascade_eye_right.xml");
        FileOutputStream oser = new FileOutputStream(cascadeFileER);

        byte[] bufferER = new byte[4096];
        int bytesReadER;
        while ((bytesReadER = iser.read(bufferER)) != -1) {
            oser.write(bufferER, 0, bytesReadER);
        }
        iser.close();
        oser.close();
        //----------------------------------------------------------------------------------------------------


        // --------------------------------- load right eye classificator ------------------------------------
        InputStream isel = context.getResources().openRawResource(R.raw.haarcascade_lefteye_2splits);
        File cascadeDirEL = context.getDir("cascadeEL", Context.MODE_PRIVATE);
        File cascadeFileEL = new File(cascadeDirEL, "haarcascade_eye_left.xml");
        FileOutputStream osel = new FileOutputStream(cascadeFileEL);

        byte[] bufferEL = new byte[4096];
        int bytesReadEL;
        while ((bytesReadEL = isel.read(bufferEL)) != -1) {
            osel.write(bufferEL, 0, bytesReadEL);
        }
        isel.close();
        osel.close();

        // ------------------------------------------------------------------------------------------------------


        mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
        mCascadeER = new CascadeClassifier(cascadeFileER.getAbsolutePath());
        mCascadeEL = new CascadeClassifier(cascadeFileER.getAbsolutePath());
        if (mJavaDetector.empty()|| mCascadeER.empty() || mCascadeEL.empty()) {
            Log.e(TAG, "Failed to load cascade classifier");
            mJavaDetector = null;
            mCascadeER=null;
            mCascadeEL=null;
        } else
            Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());

        mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);

        cascadeDir.delete();
        cascadeFileER.delete();
        cascadeDirER.delete();
        cascadeFileEL.delete();
        cascadeDirEL.delete();

and I have exception on the following line. . .

mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);