Processing+OpenCV - FaceDetection on a Movie

edited October 2017 in Library Questions

I am using Processing with the OpenCV Lib and wanted to rewrite the example Code from the creators Git so that instead of doing Face Detection on a Camera Capture I'll load a video (.mp4).

Link to the Git and the example Code (which is working): Link

Here is my Sketch:

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

OpenCV opencv;
Movie myMovie;
Rectangle[] faces;

void setup() {
  size(480, 270);

  myMovie = new Movie(this, "people3.mp4");
  myMovie.loop();
  opencv = new OpenCV(this, myMovie.width, myMovie.height);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}

void movieEvent(Movie myMovie) {
  myMovie.read();
}

void draw() {

  background(0);
  if (myMovie.available()) {    

    opencv.loadImage(myMovie);
    faces = opencv.detect();
    image(myMovie, 0, 0);

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

What I'm getting is an IndexOutOfBoundsException: Index: 3, Size: 0 at openCV.loadImage(myMovie) and I don't know why.

Appreciating any help! :-)

Answers

  • Sorry, but I don't really get how this should help me?

    I have the same Problem as jayson had in this thread: https://forum.processing.org/two/discussion/18536/opencv-problem-with-bootcamp

    As you can see here It totally should be possible to use the loadImage function with a movie argument:

    import gab.opencv.*;
    2   import processing.video.*;
    3   
    4   Movie video;
    5   OpenCV opencv;
    6   
    7   void setup() {
    8     size(720, 480);
    …   
    18  void draw() {
    19    image(video, 0, 0);  
    20    opencv.loadImage(video);
    21    
    22    opencv.updateBackground();
    23    
    24    opencv.dilate();
    
  • Answer ✓

    Made it work. Don't ask me why, but it's necessary to call both the Movie.loop() and Movie.play() function.. Here you go:

    import processing.video.*;
    import gab.opencv.*;
    import java.awt.Rectangle;
    
    OpenCV opencv;
    Movie myMovie;
    Rectangle[] faces;
    
    void setup() {
      size(480, 270);
    
      myMovie = new Movie(this, "people3.mp4");
      opencv = new OpenCV(this, 480, 270);
    
      opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
      myMovie.loop();
      myMovie.play();
    }
    
    void draw() {
       //Movie needs time to load into canvas, so we wait until we get a height
       while (myMovie.height == 0 )  delay(10); 
       image(myMovie, 0, 0);
       opencv.loadImage(myMovie);
       faces = opencv.detect();
    
    
      if (faces != null) {
        for (int i = 0; i < faces.length; i++) {
          strokeWeight(2);
          stroke(255, 0, 0);
          noFill();
          rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
        }
      }
    }
    
    
    void movieEvent(Movie myMovie) {
      myMovie.read();
    }
    
  • I tried with either loop or play and both worked. There was no need to call both simultaneously. I prefer not to use a while inside draw. Instead, you could do something like:

      if (myMovie!=null && myMovie.height>0)   {
        image(myMovie, 0, 0);
        opencv.loadImage(myMovie);
        faces = opencv.detect();
      }
    

    Thxs for sharing.

    Kf

Sign In or Register to comment.