Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

how to combine two different codes together. webcam and ellipses

$
0
0

I am trying to make the the wave that I have follow my face on openCV face detector but i can't seem to rearrange it so it works. right now it works with just the wave following the mouse but how do I make it follow my face on my webcam instead of the mouse. how do i rearrange it so it works? any help would be very much appreciated! this is the code I have right now:

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

Capture video;
OpenCV opencv;
// creating the wave -> (ellipses transform into wave as it grows and dies
class Wave {

  PVector loc;
  int farOut;

  Wave() {
    loc = new PVector();
    loc.x = mouseX;
    loc.y = mouseY;


    farOut = 1;

    //colour stroke on wave set on random so it flashes.
    stroke(random(255), random(255), random(255));
  }

  void update() {
    farOut += 1;
  }
  void display() {
    ellipse(loc.x, loc.y, farOut, farOut);
  }

  boolean dead() {

    if(farOut > 90) {
      return true;
    }
    return false;
  }
}
//array list of waves
ArrayList<Wave> waves = new ArrayList<Wave>();

void setup() {
  size(640, 480);
  video = new Capture(this, 640/2, 480/2);
  opencv = new OpenCV(this, 640/2, 480/2);
  //dectating just the face
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
 ellipseMode(CENTER);
  video.start();

}

void draw() {
  scale(2);
  opencv.loadImage(video);
  image(video, 0, 0 );

  fill(255, 255, 255, 50);
  noStroke();
  //face detector code with openCV
  Rectangle[] faces = opencv.detect();
  println(faces.length);

  for (int i = 0; i < faces.length; i++) {
    println(faces[i].x + "," + faces[i].y);
    ellipse(faces[i].x, faces[i].y, 10, 10);

 Wave w = new Wave();
    //array list -> creating new waves
    waves.add(w);
  }

  //run through each of the waves
  //wave size will grow
  for(int i = 0; i < waves.size(); i ++) {
    //show waves - > each wave updated and displayed
    waves.get(i).update();
    waves.get(i).display();

   //kill the wave
    if(waves.get(i).dead()) {
      waves.remove(i);
    }
  }
}

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

Viewing all articles
Browse latest Browse all 2896

Trending Articles