OpenCV on Raspberry Pi - Face Detection sketch errors

edited September 2017 in Raspberry PI

I am looking to have a raspberry pi with a working openCV face detection sketch. I have used a similar sketch on my normal PC. When I brought it over to my Pi I had to download glvideo library and change a few things. However now when I run the sketch it get the error "Width (0) and height (0) cannot be <=0" - I've tried changing a few things with no luck. Any know what to do? Cheers (The aim of the sketch is to detect faces on camera and when there are no faces to display text - this works on my PC.)

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

GLCapture video;
OpenCV opencv;
Rectangle[] faces;

void setup() {
  size(320, 240, P2D);

  String[] devices = GLCapture.list();
  println("Devices:");
  printArray(devices);
  if (0 < devices.length) {
    String[] configs = GLCapture.configs(devices[0]);
    println("Configs:");
    printArray(configs);
  }

  // this will use the first recognized camera by default
  video = new GLCapture(this, devices[0], 320, 240, 30);

  // you could be more specific also, e.g.
  //video = new GLCapture(this, devices[0]);
  //video = new GLCapture(this, devices[0], 640, 480, 25);
  //video = new GLCapture(this, devices[0], configs[0]);

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

void draw() {
  opencv.loadImage(video);
  faces = opencv.detect();
  background(0,0,0);
  image(video, 320, 240);
  if (video.available()) {
    video.read();
  }
  if (faces!=null) {
    for (int i=0; i< faces.length; i++) {
      noFill();
      stroke(255, 255, 0);
      strokeWeight(10);
      rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
    }
  }
  if (faces==null) {
    rect(100,100, 100, 100);
    textAlign(CENTER);
    fill(255, 0, 0);
    textSize(56);
    text("UNDETECTED", 100, 100);

}}

Answers

  • @EmRod video.width and video.height are zero at the time you're initializing the OpenCV object because their values are computed asynchronously - they are only known when the first frame comes back from the decoder.

    Since here you know that your camera is capable of 320x240, and you're also requesting this resolution, you could put in those constants directly. Or, use video.width() and video.height(). Those will block (wait) until the values are known. (But this will currently likely hang on macOS because of an unrelated bug.)

    Hope this helps!

  • Thanks @gohai, sadly this didn't work on my sketch. I understand the logic but the error still remains. It highlights :

    opencv.loadImage(video);

    And shows the error. Super confused and don't know if processing isnt developed enough yet for Pi...

  • There is just no frame yet to analyze...

    Change the code in draw to read:

    void draw() {
      if (video.available()) {
        video.read();
        opencv.loadImage(video);
        faces = opencv.detect();
    }
    // ...
    
Sign In or Register to comment.