Ask Your Question

Melvin's profile - activity

2018-05-01 07:12:36 -0600 received badge  Notable Question (source)
2017-09-08 13:07:51 -0600 received badge  Notable Question (source)
2016-07-13 09:55:14 -0600 received badge  Student (source)
2016-07-03 22:08:09 -0600 received badge  Popular Question (source)
2015-09-02 13:37:08 -0600 received badge  Popular Question (source)
2013-10-13 13:09:26 -0600 commented answer Detect faces on an image in Android OpenCV java

I have checked the logcat and It says detection method is not selected. There must be something wrong with loading the cascade file. I also manage to check the imwrite when converting the img to graysacle and its worked fine. I uploaded my code for loading cascade file please take a look. Much appreciated.

2013-10-13 01:42:35 -0600 commented answer Detect faces on an image in Android OpenCV java

for (Rect rect : faceDetections.toArray()){ Core.rectangle(a, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } this is suppose to draw circle on the detected faces. It worked on my opencv with java desktop. But my problem now is that highgui.imwrite does not write the image file into the sdcard.

2013-10-12 05:13:06 -0600 commented answer Detect faces on an image in Android OpenCV java

Thanks Daniil, I didn't notice that. I changed it to Mat a, but It still not showing the rect. I'm not sure whats wrong. I modified a little by putting the detection inside onTouch method. Please take a look, If there is any problem. Thanks again.

2013-10-12 00:27:24 -0600 asked a question Detect faces on an image in Android OpenCV java

When touch on the screen, this method is invoked, highgui.imwrite is suppose to overwrite the previous store into sdcard method.

 public boolean onTouch(View v, MotionEvent event) {
         Log.i(TAG,"onTouch event");

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
     String currentDateandTime = sdf.format(new Date());
     String fileName = Environment.getExternalStorageDirectory().getPath() +
                               "/sample_picture_" + currentDateandTime + ".jpg";
     mOpenCvCameraView.takePicture(fileName);
     Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
  Mat a = Highgui.imread(fileName);

  MatOfRect faceDetections = new MatOfRect();


  if (mDetectorType == JAVA_DETECTOR) {
      if (mJavaDetector != null)
  mJavaDetector.detectMultiScale(a, faceDetections);
  }
  else if (mDetectorType == NATIVE_DETECTOR) {
      if (mNativeDetector != null)
          mNativeDetector.detect(mGray, faceDetections);
  }
  else {
      Log.e(TAG, "Detection method is not selected!");
  }


     for (Rect rect : faceDetections.toArray()){

       Core.rectangle(a, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
      Boolean bool = null;
      bool =   Highgui.imwrite(fileName,a);
     if (bool == true)
        Log.d(TAG, "SUCCESS writing image to external storage");
      else
        Log.d(TAG, "Fail writing image to external storage");

    }
    return false;
}

Then this method will create fileName for storing in external storage and takes the picture.

public void takePicture(final String fileName) {
            Log.i(TAG, "Taking picture");
            this.mPictureFileName = fileName;
            // Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
            // Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue   

            mCamera.setPreviewCallback(null);

            // PictureCallback is implemented by the current class    

            mCamera.takePicture(null, null, this);        
        }

This method takes bytes array and store into sdcard.

 public void onPictureTaken(byte[] data, Camera camera) {
    Log.i(TAG, "Saving a bitmap to file");
    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();

  //...

    mCamera.setPreviewCallback(this);

    // Write the image in a file (in jpeg format)
    try {
        FileOutputStream fos = new FileOutputStream(mPictureFileName);

        fos.write(data);
        fos.close();

    } catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
    }



}

There are 2 constructor for the class and the cascade file loaders

public ActivityView(Context context, AttributeSet attrs) {
         super(context, attrs);

         }
    public ActivityView(Context context) {
        super(context, null);
        try {
         // copy the resource into an XML file so that we can create the classifier
         final File cascadeFile = getCascade(R.raw.lbpcascade_frontalface);

        _javaDetector = new CascadeClassifier(cascadeFile.getAbsolutePath());
         if (_javaDetector.empty()) {
         Log.e(TAG, "Failed to load cascade classifier");
         _javaDetector = null;
         } else{
         Log.i(TAG, "Loaded cascade classifier from " + cascadeFile.getAbsolutePath());
         }

         _nativeDetector = new DetectionBasedTracker(cascadeFile.getAbsolutePath(), 0);

        cascadeFile.getParentFile().delete();
         cascadeFile.delete();

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

    }    

        private File getCascade(final int cascadeRessourceId) throws IOException{
         InputStream is = null;
         OutputStream os = null;

         try{
         is = getContext().getResources().openRawResource(cascadeRessourceId);
         File cascadeDir = getContext().getDir("cascade", Context.MODE_PRIVATE);
         File cascadeFile = new File(cascadeDir, "cascade.xml");
         os = new FileOutputStream(cascadeFile);

         byte[] buffer = new byte[4096];
         int bytesRead;
         while ((bytesRead = is.read(buffer)) != -1) {
         os.write(buffer, 0, bytesRead);
         }

         return cascadeFile;
         }finally{
         is.close();
         os.close();
         }
         }

Now I believe I have converted to type Mat with highgui.imread(directory). But now I want to able to detect faces ... (more)

2013-10-11 08:28:52 -0600 received badge  Editor (source)
2013-10-11 07:02:16 -0600 asked a question convert byte array to mat in java

public void onPictureTaken(byte[] data, Camera camera) {

    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();
    byte buf[] = new byte[100];

  Mat m = new Mat(1, 100, CvType.CV_8UC1);
m.put(0, 0, buf);
//  MatOfRect faceDetections = new MatOfRect();
  //mCamera.
//  fd.NATIVE_DETECTOR.detectMultiScale(m, faceDetections);
    mCamera.setPreviewCallback(this)
}

Error: Cannot invoke detectMultiScale(Mat, MatofRect) on primitive type int

The image data will be passed into this method so I can convert it to Mat so i can use the detect multi scale to detect faces on the image. But I do not know how to convert it. Any idea or suggestions is greatly appreciated. Thank you.

2013-09-26 11:56:36 -0600 received badge  Scholar (source)
2013-09-25 12:39:32 -0600 asked a question Opencv for desktop Java port into Android development

Hi, I have managed to do face detection in desktop using the libraries provided by opencv for java development. It includes functions like draw rectangle around the face, crop image and resize image. Is it possible to port into android? Are the methods to open camera, grab and retrieve image the same as in android? Besides that, is loading the opencv library different as well? Or do I have to do everything from scratch again in android?

Thanks in advance. All answers are welcome and much appreciated.