Videos triggered by color tracking?

edited February 2016 in Library Questions

EDIT TO PREVIOUS QUESTION: Alright so I've sort of found answer to my previous question, but now a new one has been brought up. First off, I am in no way familiar with coding. I'm just an artist desperately trying to get an installation piece prepared for a show. I'm very sorry that my code is probably an inelegant hodge-podge. Secondly I'm using Processing 2.2.1.

For this project, I have Processing create two windows. In one window, it searches all the colors found in a webcam display for certain colors.

Based on whether it finds the specific colors, different videos will play in the second window. (For instance, if the webcam sees the color red, the video Rune1 will play in the second window).

Now I've gotten the code to work before, but it just layers the videos on top of one another. What I want is to try to have the videos switch out instead (One video stops playing when another starts). I've tried some code I found in the forums, but I'm such a newbie and my code is so convoluted that I can't get it to work. Would anyone be willing to take a look at this mess?

Code:

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 
import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 


import javax.swing.*; 
SecondApplet s;


import processing.video.*;


int VideoPlaying;

boolean Vid1 = false;
boolean Vid2 = false;


Movie Rune1;
Movie Rune2;

Capture video;

// Colors being tracked
color trackColor2 = color (136,33,27);
color trackColor3 = color (55,68,100);
  float worldRecord1 = 500;
  float worldRecord2 = 500;
  float worldRecord3 = 500;
  float worldRecord4 = 500;
  float worldRecord5 = 500;
  float worldRecord6 = 500;

public void setup() {

       Rune1 = new Movie (this, "C:/Users/I/Desktop/WORKING3/data/Video1.avi");
       Rune2 = new Movie (this, "C:/Users/I/Desktop/WORKING3/data/Video2.avi");



  frameRate (200);
  size(1280, 720);


  PFrame f = new PFrame(width, height);
  frame.setTitle("first window");
  f.setTitle("second window");


//Changes WebCam source


  video = new Capture(this, "name=Logitech HD Webcam C615,size=1280x720,fps=30");


 video.start(); 
trackColor2 = color (136,33,27);
trackColor3 = color (39,123,165);
 smooth();
}
public void draw() {


 if (video.available()) {
   video.read();
}
 video.loadPixels();
 image(video,0,0);


float worldRecord0 = 500;

 int closestX = 0;
 int closestY = 0;

 for (int x = 0; x < video.width; x ++ ) {
   for (int y = 0; y < video.height; y ++ ) {
     int loc = x + y*video.width;
     // What is current color
     color currentColor = video.pixels[loc];
      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);


      float r3 = red(trackColor2);
      float g3 = green(trackColor2);
      float b3 = blue(trackColor2);

      float r4 = red(trackColor3);
      float g4 = green(trackColor3);
      float b4 = blue(trackColor3);    


     float d2 = dist(r1,g1,b1,r3,g3,b3); 

     float d3 = dist(r1,g1,b1,r4,g4,b4);

     if (d2 < worldRecord1) {
        worldRecord1 = d2;
      }

     if (d3 < worldRecord2) {
        worldRecord2 = d3;
      }

   }
 }

}

public class PFrame extends JFrame {
  public PFrame(int width, int height) {
    setBounds(100, 100, width, height);
    s = new SecondApplet();
    add(s);
    s.init();
    show();
  }
}
public class SecondApplet extends PApplet {
  int ghostX, ghostY;

  public void setup() {

    background(0);
    noStroke();
  }

  public void draw() {

   toggle();
  if (Vid1) {

    println("Rune1");
    Rune2.stop();
    background(0);
    Rune1.play();
    background(0);
    image(Rune1, 0, 0, width, height);
  }

  if (Vid2) {
    println("Rune2");
    Rune1.stop();
    background(0);
    Rune2.play();
    image(Rune2, 0, 0, width, height);
  }

}
void movieEvent(Movie m) { 
  m.read();
} 


     if (worldRecord1 < 50) {

     VideoPlaying = 1 ;
     println(VideoPlaying); 
     }


           if (worldRecord2 < 50) { 
            VideoPlaying = 2 ;
              println(VideoPlaying);
              }

  }
  }
}

void movieEvent(Movie m) {
  m.read();
}


void toggle() {

  if (VideoPlaying == 1) {
    Vid1 = true;
    Vid2 = false;

  }

  if (VideoPlaying == 2) {
    Vid1 = false;
    Vid2 = true;

  }

  }

Answers

  • edited March 2016

    I edited some of the code so it's running BUT instead of switching videos from Video 1 to Video 2 back to Video 1 depending on the color change, it sticks on Video 2 and will not change to anything else. Any suggestions?

    import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import java.io.PrintWriter; 
    import javax.swing.*; 
    import processing.core.*; 
    import processing.data.*; 
    import processing.event.*; 
    import processing.opengl.*; 
    import processing.video.*;
    
    SecondApplet s;
    
    Movie theMov; 
    int VideoPlaying;
    
    boolean Vid1 = false;
    boolean Vid2 = false;
    
    //Videos
    Movie Video1; 
    Movie Video2; 
    
    Capture video;
    
    color trackColor2 = color (136, 33, 27);
    color trackColor3 = color (55, 68, 100);
    float worldRecord1 = 500;
    float worldRecord2 = 500;
    float worldRecord3 = 500;
    float worldRecord4 = 500;
    float worldRecord5 = 500;
    float worldRecord6 = 500;
    
    //Fim Videos
    
    void setup() { 
      // size(600, 300);
    
      //videos
      Video1 = new Movie(this, "C:/Users/I/Desktop/WORKING3/data/Rune1.avi");
      Video2 = new Movie(this, "C:/Users/I/Desktop/WORKING3/data/Rune2.avi");
      //Fim videos
    
      frameRate (200);
      size(1280, 720);
    
      PFrame f = new PFrame(width, height);
      frame.setTitle("first window");
      f.setTitle("second window");
    
      video = new Capture(this, "name=Logitech HD Webcam C615,size=1280x720,fps=30");
      video.start(); 
    
      trackColor2 = color (136, 33, 27);
      trackColor3 = color (39, 123, 165);
      smooth();
    }
    
    void draw() { 
    
      ////everything that happens in the first window
    
      if (video.available()) {
        video.read();
      }
      video.loadPixels();
      image(video, 0, 0);
    
      float worldRecord0 = 500;
    
      // XY coordinate of closest color
      int closestX = 0;
      int closestY = 0;
    
      // 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;
          // What is current color
          color currentColor = video.pixels[loc];
          float r1 = red(currentColor);
          float g1 = green(currentColor);
          float b1 = blue(currentColor);
    
          float r3 = red(trackColor2);
          float g3 = green(trackColor2);
          float b3 = blue(trackColor2);
    
          float r4 = red(trackColor3);
          float g4 = green(trackColor3);
          float b4 = blue(trackColor3);    
    
          // Using euclidean distance to compare colors
          float d2 = dist(r1, g1, b1, r3, g3, b3); // We are using the dist( ) function to compare the current color with the color we are tracking.
    
          float d3 = dist(r1, g1, b1, r4, g4, b4);
    
          // If current color is more similar to tracked color than
          //closest color, save current location and current difference
          if (d2 < worldRecord1) {
            worldRecord1 = d2;
          }
    
          if (d3 < worldRecord2) {
            worldRecord2 = d3;
          }
        }
      }
    }
    
    ////////////// second screen below
    
    public class PFrame extends JFrame {
      public PFrame(int width, int height) {
        setBounds(100, 100, width, height);
        s = new SecondApplet();
        add(s);
        s.init();
        show();
      }
    }
    
    public class SecondApplet extends PApplet {
    
      public void setup() {
        //background(0);
        noStroke();
      }
    
      public void draw() {
    
        toogle();
        if (Vid1) {
          println("video1");
          Video2.stop();
          background(0);
          Video1.loop();
          background(0);
          image(Video1, 0, 0, width, height);
        }
    
        if (Vid2) {
          println("video2");
          Video1.stop();
          background(0);
          Video2.loop();
          image(Video2, 0, 0, width, height);
        }
    
        //video
        if (worldRecord1 < 20) {
          VideoPlaying = 1 ;
          println(VideoPlaying);
        }
    
        if (worldRecord2 < 20) {
          VideoPlaying = 2 ;
          println(VideoPlaying);
        }
      }
    
      void toogle() {
    
        if (VideoPlaying == 1) {
          Vid1 = true;
          Vid2 = false;
        }
    
        if (VideoPlaying == 2) {
          Vid1 = false;
          Vid2 = true;
        }
      }
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
  • if (worldRecord1 < 20) {
      VideoPlaying = 1 ;
      println(VideoPlaying);
    }
    
    if (worldRecord2 < 20) {
      VideoPlaying = 2 ;
      println(VideoPlaying);
    }
    

    }

    can both these conditions be true at the same time?

Sign In or Register to comment.