Using opencv with processing

I intend to use opencv with processing for face detection
while the examples are explicit in how to do face detection on a static image when i am trying to apply that using video from a webcam the method is not working
is thr a tutorial fr that
this hw i tried without much luck-

import processing.video.*;

import gab.opencv.*;
import java.awt.Rectangle;

OpenCV opencv;
Rectangle[] faces;
Capture video;

void setup() {
  size(640,480);
  video=new Capture(this,640,480);
  video.start();
  opencv = new OpenCV(this, video);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
  faces = opencv.detect();
}

void draw() {
  if(video.available()){
   video.read(); 
   image(opencv.getInput(), 0, 0);
  }
  noFill();
  stroke(0, 255, 0);
  strokeWeight(10);
  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  }
}

please help
thanks

1 Like

If the video doesn’t need to be live you can use ffmpeg to split the video into frames

But I want the video to be live

Hey,

  1. Please try to format your code properly when posting, otherwise it’s hard to read. (Preformatted Text “</>”-icon on the top).

  2. I think the opencv-lib is image based and doesn’t handle videos by default. But you can pass the frames of your webcam to opencv with the OPENCV.loadImage() method. Here is an adapted example:

import processing.video.*;
import gab.opencv.*;
import java.awt.Rectangle;

Capture video;
OpenCV opencv;
Rectangle[] faces;

void setup() {
  size(320, 180);
  video = new Capture(this, width, height);
  video.start();  

  opencv = new OpenCV(this, video.width, video.height);  
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}

void draw() {
  opencv.loadImage(video);
  faces = opencv.detect();

  image(opencv.getInput(), 0, 0);

  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  }
}

void captureEvent(Capture c) {
  c.read();
}

1 Like

Lotsa old forum threads about OpenCV::detect(): :angel:

1 Like

Hi
for computer vision and face detection using the webcam what library should i use in processing.
My programming skills are pretty limited so would like to know of a library that is well documented with tutorial.

Not sure about tutorials. But the opencv-library comes with some examples, which might serve as a starting point: https://github.com/atduskgreg/opencv-processing

1 Like

/You can may to use LiveCamTest, one of OpenCV libraries, i did use this with a webcam in real-time video, this detect one or two faces at the same time, I hope it helps you!
/
import gab.opencv.
;
import processing.video.
;
import java.awt.
;

Capture video;
OpenCV opencv;

void setup() {
size(640, 480);
video = new Capture(this, 640/2, 480/2);
opencv = new OpenCV(this, 640/2, 480/2);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);

video.start();
}

void draw() {
scale(2);
opencv.loadImage(video);

image(video, 0, 0 );

noFill();
stroke(#FF0080);
strokeWeight(3);
Rectangle[] faces = opencv.detect();
println(faces.length);

for (int i = 0; i < faces.length; i++) {
println(faces[i].x + “,” + faces[i].y);
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
}

void captureEvent(Capture c) {
c.read();
}