We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi All,
As the title suggests I'm trying to get face detection in real time which is too slow when executed directly in draw. Therefore I'd like to implement it within a Runnable class but I'm not having much luck. Can someone point me in the right direction?
Error:
You are trying to draw outside OpenGL's animation thread.
Place all drawing commands in the draw() function, or inside
your own functions as long as they are called from draw(),
but not in event handling functions such as keyPressed()
or mousePressed().
OpenGL error 1282 at top endDraw(): invalid operation
OpenGL error 1282 at bot endDraw(): invalid operation
Code:
import processing.video.*;
import gab.opencv.*;
import java.awt.Rectangle;
Capture video;
OpenCV opencv;
Rectangle[] faces;
boolean detected = false;
FaceThread getFaces;
void setup() {
size(640, 480);
video = new Capture(this, width, height);
faces = new Rectangle[0];
getFaces = new FaceThread();
video.start();
}
void draw() {
background(0);
PImage img = video;
img.filter(GRAY);
image(img, 0, 0, 768, 432);
if (faces.length > 0) {
for (int i = 0; i < faces.length; i++) {
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
}
if (millis() % 500 == 0) {
println("starting thread");
getFaces.start();
}
}
void mousePressed() {
println(frameRate);
}
PImage getCanvas(){
return get();
}
class FaceThread implements Runnable {
Thread thread;
public FaceThread() {
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
println("New Thread");
opencv.loadImage(getCanvas());
faces = opencv.detect();
println(faces.length);
}
public void stop() {
thread = null;
}
public void dispose() {
stop();
}
}
Answers
Just a cpl of suggestions if you want speed:
Kf
Thanks for the suggestion unfortunately both 1 and 2 freeze the video stream for a short period which I'm really trying to avoid.
Bump?
Here's an updated "runnable" version of the code which seems close.
Error:
Code:
EDIT: Seems like it's unclear what parts of OpenCV are thread-safe
How to make OpenCV Detect method thread-safe?