integrating PS3 Eye cam for motion detection

edited April 2015 in How To...

Hi,

I managed to get my first code to work (written on Processing 2.1.1): it detects motion via my mac built-in webcam, and applies a reverse-explode effect on an image. Here's the initial code:


//

import processing.video.*;
Capture video;
PImage prevFrame;
PImage img;       // The source image
int cellsize = 2; // Dimensions of each cell in the grid
int columns, rows;   // Number of columns and rows in our system
int largeur,hauteur,largeurInit,hauteurInit,echelle=1,nbPixelsBlancs=0,nbPixelsNoirs=0,saut=10;
float threshold = 10;


//----------------------------------------------------------

void setup() {
  
  
  frameRate(80);
    video = new Capture(this, 640, 480, 80);
  prevFrame = createImage(video.width,video.height,RGB);
  
  size(1000, 562, P3D);
  img = loadImage("fougeres.jpg");  // Load the image
  columns = img.width / cellsize;  // Calculate # of columns
  rows = img.height / cellsize;  // Calculate # of rows
}

//----------------------------------------------------------

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();
  }
  
  //loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();
 nbPixelsBlancs=0;
  nbPixelsNoirs=0;
 
  
  for (int a = 0; a < video.width; a ++ ) {
    for (int c = 0; c < video.height; c ++ ) {
     
      int loc = a + c*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) {
        // If motion, display black
        //pixels[loc] = color(0);
        nbPixelsNoirs=nbPixelsNoirs+1;
       
      } else {
        // If not, display white
        //pixels[loc] = color(255);
        nbPixelsBlancs=nbPixelsBlancs+1;

      }
    }
  }
   
//----------------------------------------------------------
   
    background(0);

// Begin loop for columns

  
   for ( int i = 0; i < columns; i++) {
    // Begin loop for rows
    for ( int j = 0; j < rows; j++) {
      
      int x = i*cellsize + cellsize/2 ;  // x position
      int y = j*cellsize + cellsize/2 ;  // y position
      int loc = x + y*img.width;  // Pixel array location
      color c = img.pixels[loc];  // Grab the color
     
        // Calculate a z position as a function of mouseX and pixel brightness
      float z = (nbPixelsBlancs / (float(width)) * brightness(img.pixels[loc])/-800);
      float w = (nbPixelsBlancs / (float(width)) * brightness(img.pixels[loc])/-5000);
      
      // Translate to the location, set fill and stroke, and draw the rect
    
     pushMatrix(); 
     translate(x, y, z+w);
     fill(c, 204);
      noStroke();
      rectMode(CENTER);
      rect(0, 0, cellsize, cellsize);
      
     popMatrix();
       
  
    }
   }
 }
 

Here's my problem: I don't want it to work with my built-in webcam, but with a PS3 EYE cam.

Apparently, PS3 EYE only works on Processing 1.5.1, so I tried opening the code above in this version, changing the name of my video source to:

video = new Capture(this, width, height, "Sony HD Eye for PS3 (SLEH 00201)", 1000);

as according to tricks I found there: https://processing.org/discourse/beta/num_1251903672.html

But I receive: "Error while setting up Capture"

I've tried several things, nothing does seem to work… Can someone help me? Maybe a fresh look on it would help… Thanks!

Answers

Sign In or Register to comment.