how to combine two different codes together. webcam and ellipses

edited September 24 in Kinect

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

Answers

  • Try This

    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
    
    //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(faces[i].x, faces[i].y);
        //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
        Wave wx = (Wave) waves.get(i);
        wx.display();
        wx.update();
    
        //    waves.get(i).update(); // sorry I have commented this because I am not aware of using this syntax :)
        //    waves.get(i).display();
    
        //kill the wave 
        if (waves.get(i).dead()) {
          waves.remove(i);
        }
      }
    }
    
    void captureEvent(Capture c) {
      c.read();
    }
    
    //----------------------------------------------------
    class Wave {
    
      PVector loc = new PVector(width/2, height/2);
      int farOut;
    
      Wave(float x, float y) {
        loc.x = x;
        loc.y = y;
        farOut = 1;
        stroke((color)random(#000000));
      }
    
      void update() {   
        farOut += 1;
      } 
      void display() {   
        strokeWeight(3);
        noFill();
        ellipse(loc.x, loc.y, farOut, farOut);
      }
    
      boolean dead() {
    
        if (farOut > 90) {      
          return true;
        }   
        return false;
      }
    }
    

    and then tell what is not working

  • JaiJai
    edited April 2016

    @blyk is there a reason why we cant use elipse rather then Rectangle[] faces = opencv.detect(); ? i just like the look of the egg or circle shape rather then the usual box....thanks also is there a way rather then using this ellipse(loc.x, loc.y, farOut, farOut); that you made maybe we can use wave amplitudes from vocals? on to the bottom corner of the face detect Rectangle[] ?

    this is what i done so far

    Vokal()
    {
      pushMatrix();
      translate(0, 0 );
      noFill();  
      stroke(255, 0, 255);
      fft.forward( in.mix );
      for (int i = 0; i < fft.specSize (); i++)
      {
        // draw the line for frequency band i, scaling it up a bit so we can see it
        line( i, height, i, height - fft.getBand(i)*50 );
      }
      // draw the waveforms so we can see what we are monitoring
      for (int i = 0; i < in.bufferSize () - 1; i++)
      {
        line( i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50 );
        line( i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50 );
      }
    
      String monitoringState = in.isMonitoring() ? "enabled" : "disabled";
      text( "Vokal User Reg " + monitoringState + ".", 5, 15 );
      popMatrix();
    
      //*****************************************//
    
      pushMatrix();
      translate(0, 0 );
      noFill();  
      scale(2);
      opencv.loadImage(video);
      image(video, 0, 0 );
      popMatrix();
    }
    

    i cant get the sound waves to appear only near faces and on top of all the video is jumpy and im not sure why ? i have a pretty fast pc so it cant be my processor or graphics card so then what ?

    i would like to see this amplitudes shorten and stacked to the bottom of the rectangle of faces when they get detected

Sign In or Register to comment.