<commit_before>package org.opencv.samples.mobilehaar;

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.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
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.objdetect.CascadeClassifier;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class FdActivity extends Activity implements CvCameraViewListener2 {

    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               mscissor;
    private MenuItem               mfork;
    private MenuItem               mItemType;
    private MenuItem               mMyCascade;

    private Mat                    mRgba;
    private Mat                    mGray;
    private File                   mCascadeFile;
    private CascadeClassifier      mJavaDetector;
    private DetectionBasedTracker  mNativeDetector;


    private int                    mDetectorType       = JAVA_DETECTOR;
    private String[]               mDetectorName;

    boolean                        scissorIsLoaded     = false;
    boolean                        forkIsLoaded        = false;
    boolean                        firstForkLoaded     = false;
    boolean                        myCascadeIsLoaded   = false;

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

    private InputStream            is;

    private CameraBridgeViewBase   mOpenCvCameraView;
    private String                 currentCascade      = "scissor.xml";
    private String                 path                = "";


    public void setCurrentCascade(String CascadeName){ currentCascade = CascadeName; }

    public String getCurrentCascade(){ return currentCascade; }

    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");




                    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.haar_tester_surface_view);

        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view);
        mOpenCvCameraView.setCvCameraViewListener(this);



        final EditText edit = (EditText) findViewById(R.id.editText2);
        final Button button = (Button) findViewById(R.id.button2);

        edit.setTextIsSelectable(true);


        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                path =  downloadXML(edit.getText().toString());
                edit.setText("");

            }
        });

    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, 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();


        MatOfRect faces = new MatOfRect();

        try {
            // load cascade file from application resources
            File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
            mCascadeFile = new File(cascadeDir, "scissor.xml");
            is = getResources().openRawResource(R.raw.scissor);
            if(!firstForkLoaded) {

                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());
            }


            firstForkLoaded = true;


            if(getCurrentCascade().equals("scissor.xml") && !scissorIsLoaded) {
                is = getResources().openRawResource(R.raw.scissor);
                mCascadeFile = new File(cascadeDir, "scissor.xml");
                mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
                scissorIsLoaded = true;
                forkIsLoaded = false;
                myCascadeIsLoaded = false;
                if (mJavaDetector.empty()) {
                    Log.e(TAG, "Failed to load cascade classifier");
                    mJavaDetector = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());



            }
            else if(getCurrentCascade().equals("stop_sign.xml") && !forkIsLoaded) {
                is = getResources().openRawResource(R.raw.stop_sign);
                mCascadeFile = new File(cascadeDir, "stop_sign.xml");
                mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
                forkIsLoaded = true;
                scissorIsLoaded = false;
                myCascadeIsLoaded = false;
                if (mJavaDetector.empty()) {
                    Log.e(TAG, "Failed to load cascade classifier");
                    mJavaDetector = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());


            }

            else if(getCurrentCascade().equals("my_cascade.xml") && !myCascadeIsLoaded && !path.equalsIgnoreCase("")) {
                Log.i(TAG, "downloaded path: " +  path);
                mJavaDetector = new CascadeClassifier(path);
                forkIsLoaded = false;
                scissorIsLoaded = false;
                myCascadeIsLoaded = true;
                if (mJavaDetector.empty()) {
                    Log.e(TAG, "Failed to load cascade classifier");
                    mJavaDetector = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());<commit_msg>added exception handeling for malformed XML files.<commit_after>package org.opencv.samples.mobilehaar;

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.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
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.objdetect.CascadeClassifier;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class FdActivity extends Activity implements CvCameraViewListener2 {

    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               mscissor;
    private MenuItem               mfork;
    private MenuItem               mItemType;
    private MenuItem               mMyCascade;

    private Mat                    mRgba;
    private Mat                    mGray;
    private File                   mCascadeFile;
    private CascadeClassifier      mJavaDetector;
    private DetectionBasedTracker  mNativeDetector;


    private int                    mDetectorType       = JAVA_DETECTOR;
    private String[]               mDetectorName;

    boolean                        scissorIsLoaded     = false;
    boolean                        forkIsLoaded        = false;
    boolean                        firstForkLoaded     = false;
    boolean                        myCascadeIsLoaded   = false;

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

    private InputStream            is;

    private CameraBridgeViewBase   mOpenCvCameraView;
    private String                 currentCascade      = "scissor.xml";
    private String                 path                = "";


    public void setCurrentCascade(String CascadeName){ currentCascade = CascadeName; }

    public String getCurrentCascade(){ return currentCascade; }

    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");




                    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.haar_tester_surface_view);

        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view);
        mOpenCvCameraView.setCvCameraViewListener(this);



        final EditText edit = (EditText) findViewById(R.id.editText2);
        final Button button = (Button) findViewById(R.id.button2);

        edit.setTextIsSelectable(true);


        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                path =  downloadXML(edit.getText().toString());
                edit.setText("");

            }
        });

    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, 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();


        MatOfRect faces = new MatOfRect();

        try {
            // load cascade file from application resources
            File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
            mCascadeFile = new File(cascadeDir, "scissor.xml");
            is = getResources().openRawResource(R.raw.scissor);
            if(!firstForkLoaded) {

                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());
            }


            firstForkLoaded = true;


            if(getCurrentCascade().equals("scissor.xml") && !scissorIsLoaded) {
                is = getResources().openRawResource(R.raw.scissor);
                mCascadeFile = new File(cascadeDir, "scissor.xml");
                mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
                scissorIsLoaded = true;
                forkIsLoaded = false;
                myCascadeIsLoaded = false;
                if (mJavaDetector.empty()) {
                    Log.e(TAG, "Failed to load cascade classifier");
                    mJavaDetector = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());



            }
            else if(getCurrentCascade().equals("stop_sign.xml") && !forkIsLoaded) {
                is = getResources().openRawResource(R.raw.stop_sign);
                mCascadeFile = new File(cascadeDir, "stop_sign.xml");
                mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
                forkIsLoaded = true;
                scissorIsLoaded = false;
                myCascadeIsLoaded = false;
                if (mJavaDetector.empty()) {
                    Log.e(TAG, "Failed to load cascade classifier");
                    mJavaDetector = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());


            }

            else if(getCurrentCascade().equals("my_cascade.xml") && !myCascadeIsLoaded && !path.equalsIgnoreCase("")) {
                Log.i(TAG, "downloaded path: " +  path);
                try {
                    mJavaDetector = new CascadeClassifier(path);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
                forkIsLoaded = false;
                scissorIsLoaded = false;
                myCascadeIsLoaded = true;
                if (mJavaDetector.empty()) {
                    Log.e(TAG, "Failed to load cascade classifier");
                    mJavaDetector = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());
{'fraction_non_alphanumeric': 0.0873798005136475, 'fraction_numerical': 0.00250851101953055, 'mean_word_length': 1.9703743125776123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2769273', 'n_tokens_mistral': 4743, 'n_tokens_neox': 4383, 'n_words': 920}