content: package cultoftheunicorn.marvel;
  
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.os.Environment;
  import android.os.Handler;
  import android.os.Message;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.CompoundButton;
  import android.widget.ImageView;
  import android.widget.Toast;
  import android.widget.ToggleButton;
  
  import org.opencv.android.BaseLoaderCallback;
  import org.opencv.android.CameraBridgeViewBase;
  import org.opencv.android.LoaderCallbackInterface;
  import org.opencv.android.OpenCVLoader;
  import org.opencv.android.Utils;
  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.cultoftheunicorn.marvel.R;
  import org.opencv.objdetect.CascadeClassifier;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  
  public class Training extends AppCompatActivity implements CameraBridgeViewBase.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;
  
      public static final int TRAINING= 0;
      public static final int IDLE= 2;
  
      private static final int frontCam =1;
      private static final int backCam =2;
  
  
      private int faceState=IDLE;
  
      private Mat                    mRgba;
      private Mat                    mGray;
      private File mCascadeFile;
      private CascadeClassifier mJavaDetector;
  
      private int                    mDetectorType       = JAVA_DETECTOR;
      private String[]               mDetectorName;
  
      private float                  mRelativeFaceSize   = 0.2f;
      private int                    mAbsoluteFaceSize   = 0;
      private int mLikely=999;
  
      String mPath="";
  
      private Tutorial3View   mOpenCvCameraView;
  
      String text;
      private ImageView Iv;
      Bitmap mBitmap;
      Handler mHandler;
  
      PersonRecognizer fr;
      ToggleButton capture;
  
      static final long MAXIMG = 10;
  
      int countImages=0;
  
      Labels labelsFile;
      static {
          OpenCVLoader.initDebug();
          System.loadLibrary("opencv_java");
      }
  
      public Training() {
          mDetectorName = new String[2];
          mDetectorName[JAVA_DETECTOR] = "Java";
          mDetectorName[NATIVE_DETECTOR] = "Native (tracking)";
  
          Log.i(TAG, "Instantiated new " + this.getClass());
      }
  
      private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
          @Override
          public void onManagerConnected(int status) {
              switch (status) {
                  case LoaderCallbackInterface.SUCCESS:
                  {
                      Log.i(TAG, "OpenCV loaded successfully");
  
                      fr=new PersonRecognizer(mPath);
                      String s = getResources().getString(R.string.Straininig);
                      //Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show();
                      fr.load();
  
                      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.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());
  
                          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;
  
  
              }
          }
      };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_training);
  
          /*Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
          setSupportActionBar(toolbar);
  
          if(getSupportActionBar() != null) {
              getSupportActionBar().setTitle("Training");
          }*/
  
          text = getIntent().getStringExtra("name");
          Iv = (ImageView) findViewById(R.id.imagePreview);
  
          capture = (ToggleButton) findViewById(R.id.capture);
          capture.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                  captureOnClick();
              }
          });
  
          mOpenCvCameraView = (Tutorial3View) findViewById(R.id.tutorial3_activity_java_surface_view);
          mOpenCvCameraView.setCvCameraViewListener(this);
  
          //mPath=getFilesDir()+"/facerecogOCV/";
          mPath = Environment.getExternalStorageDirectory()+"/facerecogOCV/";
  
          Log.e("Path", mPath);
  
          labelsFile= new Labels(mPath);
  
          mHandler = new Handler() {
              @Override
              public void handleMessage(Message msg) {
                  if (msg.obj=="IMG")
                  {
                      Canvas canvas = new Canvas();
                      canvas.setBitmap(mBitmap);
                      Iv.setImageBitmap(mBitmap);
                      if (countImages>=MAXIMG-1)
                      {
                          capture.setChecked(false);
                          captureOnClick();
                      }
                  }
              }
          };
  
          boolean success=(new File(mPath)).mkdirs();
  
          if (!success)
              Log.e("Error","Error creating directory");
  
      }
  
      void captureOnClick()
      {
          if (capture.isChecked())
              faceState = TRAINING;
          else {
              Toast.makeText(this, "Captured", Toast.LENGTH_SHORT).show();
              countImages=0;
              faceState=IDLE;
              Iv.setImageResource(R.drawable.user_image);
          }
      }
  
      @Override
      public void onCameraViewStarted(int width, int height) {
          mGray = new Mat();
          mRgba = new Mat();
      }
  
      @Override
      public void onCameraViewStopped() {
          mGray.release();
          mRgba.release();
      }
  
      @Override
      public Mat onCameraFrame(CameraBridgeViewBase.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();
  
          if ((facesArray.length==1)&&(faceState==TRAINING)&&(countImages<MAXIMG)&&(!text.equals("")))
          {
  
  
              Mat m;
              Rect r=facesArray[0];
  
  
              m=mRgba.submat(r);
              mBitmap = Bitmap.createBitmap(m.width(),m.height(), Bitmap.Config.ARGB_8888);
  
  
              Utils.matToBitmap(m, mBitmap);
  
              Message msg = new Message();
              String textTochange = "IMG";
              msg.obj = textTochange;
              mHandler.sendMessage(msg);
              if (countImages<MAXIMG)
              {
                  fr.add(m, text);
                  countImages++;
              }
  
          }
          for (int i = 0; i < facesArray.length; i++)
              Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
  
          return mRgba;
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          if (mOpenCvCameraView != null)
              mOpenCvCameraView.disableView();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          mOpenCvCameraView.disableView();
      }
  }
  