Interactive Movie: play (while motion), go to second 12 (when no motion)

edited August 2014 in Library Questions

Hello I am relatively new to processing.

We have to make an interactive installation for our bachelor exhibition pretty soon. And we can´t get the code to fully work. The thing is:

There should be a movie playing, while people are moving around. If someone stands still in front of the installation, the movie should play until a specific second, where you can read text. And if motion is detected again, it should play the movie in a loop.

The problem is, that the movie is playing and stopping, but it just stops. It should not just stop, but play until this very second. And if in motion, the video should play in loop, except for the part, where the type is readable.

I use Processing 2.2.1

Screen Shot 2014-08-18 at 16.23.11 Screen Shot 2014-08-18 at 16.23.17

// Import stuff
import processing.video.*;
import processing.opengl.*;
import jmcvideo.*;

// Global Variables
JMCMovie gangVideo;
double videoDuration;
double videoCurrentTime;

boolean init = false;

Capture video;
PImage prevFrame;

// How different must a pixel be to be a "motion" pixel
float threshold = 50;

void setup() {

  size(1080, 360);

  // Getting the Webcam
  String[] cameras = Capture.list();

  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  }

  else {
    println("Available cameras.");

    /* uncomment to see full list of webcams
    for (int i = 0; i < cameras.length; i++) {
    println(cameras[i]);
    }
    */

    video = new Capture(this, cameras[0]);
    video.start();

    // Create an empty image the same size as the video
    // don't know why but we can't get video.width and video.height :(
    prevFrame = createImage(1280, 720, RGB);
  }

  gangVideo = movieFromDataPath("Gang_Animation_blur_kurz.mov");
  videoDuration = gangVideo.getDuration();

  // play the first millisecond to see a picture
  gangVideo.play(0, 0.1);

}

JMCMovie movieFromDataPath(String filename)
{
  return new JMCMovie(this, filename, RGB);
}

// New frame available from camera
void captureEvent(Capture video) {

  // Save previous frame for motion detection!!
  prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
  prevFrame.updatePixels();  
  video.read();

}

void draw() {
  background(0); 

  // Display the video
  image(gangVideo, 0, 0);

  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();

  if( init ) {
  // These are the variables we'll need to find the average X and Y
  float sumX = 0;
  float sumY = 0;
  int motionCount = 0;

  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x++ ) {
    for (int y = 0; y < video.height; y++ ) {

      // TEST
      // println("X: " + x + ", Y: " + y + ", Width: " + video.width);

      // What is the current color
      color current = video.pixels[x+y*video.width];

      // What is the previous color
      color previous = prevFrame.pixels[x+y*video.width];

      // 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);

      // Motion for an individual pixel is the difference between the previous color and current color.
      float diff = dist(r1, g1, b1, r2, g2, b2);

      // If it's a motion pixel add up the x's and the y's
      if (diff > threshold) {
        sumX += x;
        sumY += y;
        motionCount++;
      }
    }
  }

  videoCurrentTime = gangVideo.getCurrentTime();
  println("current time: " + videoCurrentTime);
  println("motion: " + motionCount);
  println("-----");


  // if motion is big enough and video is not already being played, than go on.. 
  if( motionCount > 800 && !gangVideo.isPlaying() ) {

    // loop the video from beginning (0) to the end of motion (8) 
    gangVideo.loop(3,8);

  }


   // if there is no motion go to second 12.. 
  if( motionCount < 800 ) {

    // play the video from beginning (0) to the end (0) 
    // in this case we only play until second #12 and then stop
    gangVideo.play(0,0);

  }

  } // init

  // wait one complete cycle to establish the webcam image
  if( frameCount > frameRate ) {
    init = true;
  }
}

I would be so happy, if anyone can help me! Thanks a lot in advance.

Sign In or Register to comment.