Annonce

>>> Bienvenue sur codelab! >>> Première visite ? >>> quelques mots sur codelab //// une carte des membres//// (apéros) codelab


#1 2015-06-01 13:56:18 Interaction via webcam pour lancer une vidéo

vaninoob
nouveau membre
Date d'inscription: 2015-06-01
Messages: 2

Interaction via webcam pour lancer une vidéo



Bonjour,

J'essaye de créer un code qui déclencherait une vidéo lorsque la caméra détecte du mouvement à droite de l'écran, et une autre lorsqu'elle détecte du mouvement à gauche.

Jusqu'ici, mon code crée des particules là où il capte du mouvement, j'essaye de coder une ligne qui dirait, en gros :
S'il y a plus de x particules du coté droit de l'écran, lance cette vidéo.

Voici mon code :





import processing.video.*;
// Variable for capture device
Capture video;
Movie myMovie;
// Previous Frame
PImage prevFrame;
// How different must a pixel be to be a "motion" pixel
float threshold = 9;

Particle p;
int total =350;
Particle[] parray = new Particle[total];
int pointsX[]= new int[total];
int pointsY[]= new int[total];
int incremente = 0;

int onoff=0;

void setup() {
  size(1920, 1080);         
  video = new Capture(this, 320, 240, 0);
  video.start();
  // Create an empty image the same size as the video
  prevFrame = createImage(video.width,video.height,RGB);
   p  = new Particle(new PVector(width/2,10));
 

 
   for (int i = 0; i < parray.length; i++) {
      parray[i] = new Particle(new PVector(width/2,height/2));
       parray[i].run();
    }
             
  //background (0);
    myMovie = new Movie(this, "Twist.mp4");
                myMovie.play();
     
}

void draw() {


   //
  // Capture video
  if (video.available()) {
     

    // Save previous frame for motion detection!!
    prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height); // Before we read the new frame, we always save the previous frame for comparison!
    prevFrame.updatePixels();
    video.read();
   
    pushMatrix();
    scale(1,1);
    image(video.get(),0,0);
    popMatrix();
  }
 
 
  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();
 
  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
     
      int loc = x + y*video.width;            // Step 1, what is the 1D pixel location
      color current = video.pixels[loc];      // Step 2, what is the current color
      color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
     
      // Step 4, compare colors (previous vs. current)
      float r1 = red(current); float g1 = green(current); float b1 = blue(current);
      float r2 = red(previous); float g2 = green(previous); float b2 = blue(previous);
      float diff = dist(r1,g1,b1,r2,g2,b2);
     
      // Step 5, How different are the colors?
      // If the color at that pixel has changed, then there is motion at that pixel.
      if (diff > threshold) {
        //println(diff);
            // If motion, display black
            pixels[loc] = color(20);
             int caca= int(random(250));
            if(caca==3 && onoff==0){
                  pointsX[incremente]=x;
                  pointsY[incremente]=y;
                  incremente++;
                  //   p  = new Particle(new PVector(width/2,10));
                  // println(caca);
                   //    p.run();
                //   if (parray[incremente].isDead()) {
                      parray[incremente]=new Particle(new PVector(0,0));
                     parray[incremente]=new Particle(new PVector(pointsX[incremente],pointsY[incremente]));
                 //  }
                   
                   if(incremente>=(total-1)){
                     incremente=0;
                     //onoff=1;
                   }
                  }
      } else {
        // If not, display white
        pixels[loc] = color(0);
       
         
               
       
      }
    } // fin parcours height
   
               
  }// fin parcours width
 
  updatePixels();
             
   for (int i = 0; i <= (total-1); i++) {
      // parray[i]=new Particle(new PVector(pointsX[incremente],pointsY[incremente]));
   
       parray[i].run();
    }
    //  println(caca);
             //  p.run();
    image(myMovie, 0, 0); 
   tint(255, 126);
}













class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;
  int[] colorz= new int[4];
 
  Particle(PVector l) {
//For demonstration purposes we assign the Particle an initial velocity and constant acceleration.
    acceleration = new PVector(random(-0.5,0.5),random(-0.5,0.5));
    velocity = new PVector(random(-0.2,0.2),random(-0.2,0.2));
    location = l.get();
    lifespan = 100.0;
   
    int lacol=int(random(4));
    if(lacol==0){
    colorz[1]=252;
    colorz[2]=255;
    colorz[3]=0;
    }else if(lacol==1){
    colorz[1]=251;
    colorz[2]=15;
    colorz[3]=204;
    }else if(lacol==2){
    colorz[1]=0;
    colorz[2]=246;
    colorz[3]=255;
    } else if(lacol==3){
    colorz[1]=0;
    colorz[2]=0;
    colorz[3]=0;
    }
  }

//Sometimes it’s convenient to have a “run” function that calls all the other functions we need.
  void run() {
    update();
    display();
  }

  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 5.0;
  }

  void display() {
    stroke(colorz[1],colorz[2],colorz[3],lifespan);
    fill(colorz[1],colorz[2],colorz[3],lifespan);
    ellipse(location.x,location.y,4,4);
    if(location.x>width ||location.y>height || location.y<0 || location.x<0 ){
      lifespan=-10;
    //println(location.x);
    }
  }
 
 


//Is the Particle alive or dead?
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}



 
 
  void movieEvent(Movie m) {
  if (m == myMovie) {
    myMovie.read();
  }
}

Hors ligne

 

#2 2015-06-02 00:05:35 Re : Interaction via webcam pour lancer une vidéo

Mushussu
membre
Lieu: Orléans
Date d'inscription: 2012-05-24
Messages: 802

Re: Interaction via webcam pour lancer une vidéo



Bonjour,

L'inconvénient du copier/coller de code, c'est que l'on ne saisi pas l'essence de celui-ci.
En reprenant les codes de Daniel Schiffman, tu n'as pas compris que chaque instance de la classe Particule possède ses propres coordonnées avec la variable location.
Pour résoudre ton problème, il faut que tu définisses quelle zone déclenchera la lecture d'une vidéo. Par exemple, si x < 50. ensuite il faut parcourir le tableau parray et comparer les coordonnées à cette valeur. Si le nombre de particules est supérieur à un seuil alors tu peux déclencher une vidéo.

Hors ligne

 

fil rss de cette discussion : rss

Pied de page des forums

Powered by FluxBB

codelab, graphisme & code : emoc / 2008-2024