How to create a timer for face tracking or advice to best send sketch to MadMapper without lag?

edited March 2016 in Programming Questions

I'm currently running a code using the open cv face tracking libraries. It displays the face and tracks it and when the face isn't seen anymore it displays a captured image from before until a face is detected again. It works how I want it to when running on processing, however when i send it to MadMapper using Syphon, it lags and doesn't really work. So I think that I need to add a timer to this code so that it waits 10 seconds and then runs the find a face code again, and repeat, so that it wont lag on MadMapper as it's not constantly looking for a face. However, I'm not sure how to do this, or even if this is the best way to do it? The code is below, if anyone wants to run this using processing it needs a latestFace.jpg in the same folder just fyi.

:)

import codeanticode.syphon.*; import processing.opengl.*; import gab.opencv.*; import processing.video.*; import java.awt.*; SyphonServer server;

Capture video;

OpenCV opencv;

PImage mask;

PImage smaller;

int scale = 4;

void settings() { size(1280, 720, P3D); PJOGL.profile=1; }

void setup() {

server = new SyphonServer(this, "Processing Syphon 2");

size(1280, 720);

frameRate(25);

video = new Capture(this, width, height);

opencv = new OpenCV(this, video.width/scale, video.height/scale);

opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);

video.start();

smaller = createImage(opencv.width, opencv.height, RGB); }

void draw() {

opencv.loadImage(smaller);

image(video, 0, 0 );

Rectangle[] faces = opencv.detect();

println(faces.length);

PImage[] captured = new PImage[faces.length];

for (int i = 0; i < faces.length; i++) { captured[i] = get(faces[i].xscale, faces[i].yscale, faces[i].widthscale, faces[i].heightscale);

}

if (captured.length > 0) {


  captured[0].save("latestFace.jpg");


  noStroke();
  fill(255,255,255);
  rect(0,0,width,height);

  image(captured[0],0,0, width, height);

}

if (captured.length < 1) {


  PImage latest = loadImage("latestFace.jpg");


  noStroke();
  fill(255,255,255);
  rect(0,0,width,height);


  image(latest,0,0, width, height);
}

if (keyPressed) { if (key == ' ') { saveFrame("#####.jpg"); } }

server.sendScreen();

}

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

smaller.copy(video, 0, 0, video.width, video.height, 0, 0, smaller.width, smaller.height);

smaller.updatePixels(); }

Sign In or Register to comment.