i want to play video on the base of motion tracking

edited January 2016 in Library Questions

i want to play video on the base of motion tracking but some how i cant play.


import processing.video.*;
Movie mov;
// Variable for capture device
Capture video;
// Previous Frame
PImage prevFrame;

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

int wid = 70; // for rect 
// for counting motion in the rect 
// and playing movie in reverse 
float count; 
float abc;

void setup() {
  size(640, 480);
  // Using the default capture device
  video = new Capture(this, 640, 480);
  video.start();

  mov = new Movie(this, "T_rex.mov");
  mov.play();
  // Create an empty image the same size as the video
  prevFrame = createImage(video.width, video.height, 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 movieEvent(Movie m ) {
  m.read();
}

void draw() {
  background(125, 240, 200);

  // You don't need to display it to analyze it!
  image(mov, 0, 0);
  tint(255, 126);
  image(video, 0, 0);

  // rect
  fill(255, 126);
  rect(width/6.5, 0, wid, height);
  rect(width/1.5, 0, wid, height);
  rect(width/2.5, 0, wid, height);


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

  // These are the variables we'll need to find the average X and Y
  float sumX = 0;
  float sumY = 0;
  int motionCount = 0; 
  int totalMotion = 0;

  float sumX2 = 0;
  float sumY2 = 0;
  int motionCount2 = 0; 
  int totalMotion2 = 0;

  float sumX3 = 0;
  float sumY3 = 0;
  int motionCount3 = 0; 
  int totalMotion3 = 0;



  // ----------------------- rect 1 -------------------------- //

  // Begin loop to walk through every pixel
  for (int x = 100; x < 170; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      // 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];

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

      // Motion for an individual pixel is the difference between the previous color and current color.
      float diff = dist(r1, g1, b1, r2, g2, b2);
      // totalMotion is the sum of all color differences. 
      totalMotion += diff;
      // If it's a motion pixel add up the x's and the y's
      if (diff > threshold) {
        sumX += x;
        sumY += y;
        motionCount++;
      }
    }
  }

  // average location is total location divided by the number of motion pixels.
  float avgX = sumX / motionCount; 
  float avgY = sumY / motionCount; 

  float avg = totalMotion / video.pixels.length;



  // ----------------------- rect 2 -------------------------- //

  // Begin loop to walk through every pixel
  for (int x = 230; x < 300; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      // 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];

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

      // Motion for an individual pixel is the difference between the previous color and current color.
      float diff = dist(r1, g1, b1, r2, g2, b2);
      // totalMotion is the sum of all color differences. 
      totalMotion2 += diff;
      // If it's a motion pixel add up the x's and the y's
      if (diff > threshold) {
        sumX2 += x;
        sumY2 += y;
        motionCount2++;
      }
    }
  }

  // average location is total location divided by the number of motion pixels.
  float avgX2 = sumX2 / motionCount2; 
  float avgY2 = sumY2 / motionCount2; 
  float avg2 = totalMotion2 / video.pixels.length;


  // ----------------------- rect 3 -------------------------- //

  // Begin loop to walk through every pixel
  for (int x = 430; x < 500; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      // 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];

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

      // Motion for an individual pixel is the difference between the previous color and current color.
      float diff = dist(r1, g1, b1, r2, g2, b2);
      // totalMotion is the sum of all color differences. 
      totalMotion3 += diff;
      // If it's a motion pixel add up the x's and the y's
      if (diff > threshold) {
        sumX3 += x;
        sumY3 += y;
        motionCount3++;
      }
    }
  }

  // average location is total location divided by the number of motion pixels.
  float avgX3 = sumX3 / motionCount3; 
  float avgY3 = sumY3 / motionCount3; 
  float avg3 = totalMotion3 / video.pixels.length;



  // --------------  play video --------------------- //


  //  playing movie reverse
  if (count == 1) {
    mov.speed(-1);
    println("movie reverse");
  }

// testing reverse direction only in rect 2 
  if (avg2 > 1) {
    mov.play();
    println("movin' playing");
    abc = 1;
  } else {
    abc= 0;
  }

  if (avg >1 || avg3 > 1) {
     mov.play();
  }
  
  // pause movie if no motion
  if (avg3 == 0 && avg2 == 0 && avg == 0) {
    mov.pause();
    println("pause");
  } 


  if (mov.time() == 0 ) {
    mov.play();
    println(" replaying movie ");
  }

  // for testing 
  if (mov.time() >11) {
    mov.jump(0);
    mov.play();
    println(" re start playing movie  ");
  }


  // Draw a circle based on average motion
  smooth();
  noStroke();
  fill(0, 0, 255);
  ellipse(avgX, avgY, 15, 15);
  ellipse(avgX2, avgY2, 15, 15);
  ellipse(avgX3, avgY3, 15, 15);

  textSize(30);
  text(" T : " + mov.time(), width/6, height - 50);

  // giving value 1 to count so that if 
  // that i got again motion on that same rect movie 
  //play in reverse direction  
  count = abc;
  println("count " + count );


  fill(255, 0, 0);
  text("avg", width/6.5, 100);
  text(avg, width/6.5, 130);

  text("avg3", width/1.5, 100);
  text(avg3, width/1.5, 130);

  text("avg2", width/2.5, 100);
  text(avg2, width/2.5, 130);
}

Sign In or Register to comment.