1

我正在使用带有 OpenCV Lib 的处理,并希望重写创建者 Git 的示例代码,以便我将加载视频 (.mp4),而不是在相机捕获上进行人脸检测。

链接到 Git 和示例代码(正在运行): 链接

这是我的草图:

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);
      }
    }
  }
}

我得到的是一个

IndexOutOfBoundsException:索引:3,大小:0

在 openCV.loadImage(myMovie) 我不知道为什么。

感谢任何帮助!

4

1 回答 1

1

让它工作。不要问我为什么,但有必要同时调用 Movie.loop() 和 Movie.play() 函数.. 给你:

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();
}
于 2017-11-01T19:28:33.350 回答