The new face detection and Processing

Here is the slightly modified version of the OpenCV Java tutorial example with face detection, ported to Processing.

import processing.video.*;
 
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.CvType;
import org.opencv.core.Size;
import org.opencv.objdetect.CascadeClassifier;
 
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.Raster;
 
Capture cap;
int pixCnt;
BufferedImage bm;
 
CascadeClassifier faceDetector;
MatOfRect faceDetections;
 
void setup() {
  size(640, 480);
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  println(Core.VERSION);
 
  cap = new Capture(this, width, height);
  cap.start();
  bm = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  pixCnt = width*height*4;
 
  faceDetector = new CascadeClassifier(dataPath("haarcascade_frontalface_default.xml"));
  faceDetections = new MatOfRect();
}
 
void convert(PImage _i) {
  bm.setRGB(0, 0, _i.width, _i.height, _i.pixels, 0, _i.width);
  Raster rr = bm.getRaster();
  byte [] b1 = new byte[pixCnt];
  rr.getDataElements(0, 0, _i.width, _i.height, b1);
  Mat m1 = new Mat(_i.height, _i.width, CvType.CV_8UC4);
  m1.put(0, 0, b1);
 
  Mat m2 = new Mat(_i.height, _i.width, CvType.CV_8UC1);
  Imgproc.cvtColor(m1, m2, Imgproc.COLOR_BGRA2GRAY);   
 
  faceDetector.detectMultiScale(m2, faceDetections, 3, 1, 
  Objdetect.CASCADE_DO_CANNY_PRUNING, new Size(40, 40), new Size(240, 240));
 
  bm.flush();
  m2.release();
  m1.release();
}
 
void draw() {
  if (!cap.available()) 
    return;
  background(0);
  cap.read();
  convert(cap);
  image(cap, 0, 0);
  for (Rect rect: faceDetections.toArray()) {
    noFill();
    stroke(255, 0, 0);
    rect(rect.x, rect.y, rect.width, rect.height);
  }
}

7 Replies to “The new face detection and Processing”

  1. excellent tutorial,it help me a lot.my puzzle is that i want to rotate every frame,but android seems not able to free memory in real time,so memory leak is serious.how to fix it?

  2. I’ll try this as soon as I have a chance to install that version of OpenCV. This approach looks more promising than the version that has hooks for Processing. One question, though: For efficiency sake, is it possible to allocate the space for m1 and m2 once (say at setup() time), so you don’t have to create and dispose memory at each frame?

  3. Hello! How you managed to install opencv 2.4.4 in processing?

    Can you explain step by step?

    thank´s

  4. hi there! Me again, here is Tealeaf! I figured out what the issue is. Just put haarcascade_frontalface_default.xml into data folder under sketch folder then everything works good!

  5. nice tutorial! But I installed OpenCV 2.4.5, used opencv-245.jar and libopencv_java245.dylib, then tested your face detection code, it didn’t work……. no errors, no warnings, I have no idea

  6. Hello,
    First, sorry for my bad english, i’m from France.
    Nice article, i’m glad to see a new solution to use OpenCV in processing. I have just one question about the installation : where put the file libopencv_java244.dylib ? where can i get this file ?
    I’m a newbie with external liv, so sorry if my question seems stupid 🙂
    Regards,

Comments are closed.