code giving a voice alert only and only when motion is detected in front of laptop camera

edited January 2016 in Library Questions

I am a beginner, so I want to know the code by which the laptop's camera detects motion and speaks something like "Alert we have an intruder" this is the code which I found out in examples....

Code for Background subtraction...

/** * Background Subtraction * by Golan Levin. * * GSVideo version by Andres Colubri. * * Detect the presence of people and objects in the frame using a simple * background-subtraction technique. To initialize the background, press a key. */

import codeanticode.gsvideo.*;

int numPixels; int[] backgroundPixels; GSCapture video;

void setup() { // Change size to 320 x 240 if too slow at 640 x 480 size(640, 480);

video = new GSCapture(this, width, height); video.start();
numPixels = video.width * video.height; // Create array to store the background image backgroundPixels = new int[numPixels]; // Make the pixels[] array available for direct manipulation loadPixels(); }

void draw() { if (video.available()) { video.read(); // Read a new video frame video.loadPixels(); // Make the pixels of video available // Difference between the current frame and the stored background int presenceSum = 0; for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... // Fetch the current color in that location, and also the color // of the background in that spot color currColor = video.pixels[i]; color bkgdColor = backgroundPixels[i]; // Extract the red, green, and blue components of the current pixel�s color int currR = (currColor >> 16) & 0xFF; int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // Extract the red, green, and blue components of the background pixel�s color int bkgdR = (bkgdColor >> 16) & 0xFF; int bkgdG = (bkgdColor >> 8) & 0xFF; int bkgdB = bkgdColor & 0xFF; // Compute the difference of the red, green, and blue values int diffR = abs(currR - bkgdR); int diffG = abs(currG - bkgdG); int diffB = abs(currB - bkgdB); // Add these differences to the running tally presenceSum += diffR + diffG + diffB; // Render the difference image to the screen pixels[i] = color(diffR, diffG, diffB); // The following line does the same thing much faster, but is more technical //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB; } updatePixels(); // Notify that the pixels[] array has changed println(presenceSum); // Print out the total amount of movement } }

// When a key is pressed, capture the background image into the backgroundPixels // buffer, by copying each of the current frame�s pixels into it. void keyPressed() { video.loadPixels(); arraycopy(video.pixels, backgroundPixels); }

Code for frame differencing.....

/** * Frame Differencing * by Golan Levin. * * GSVideo version by Andres Colubri.
* * Quantify the amount of movement in the video frame using frame-differencing. */

import codeanticode.gsvideo.*;

int numPixels; int[] previousFrame; GSCapture video;

void setup() { size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 // Uses the default video input, see the reference if this causes an error video = new GSCapture(this, width, height); video.start();
numPixels = video.width * video.height; // Create an array to store the previously captured frame previousFrame = new int[numPixels]; loadPixels(); }

void draw() { if (video.available()) { // When using video to manipulate the screen, use video.available() and // video.read() inside the draw() method so that it's safe to draw to the screen video.read(); // Read the new frame from the camera video.loadPixels(); // Make its pixels[] array available

int movementSum = 0; // Amount of movement in the frame
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
  color currColor = video.pixels[i];
  color prevColor = previousFrame[i];
  // Extract the red, green, and blue components from current pixel
  int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
  int currG = (currColor >> 8) & 0xFF;
  int currB = currColor & 0xFF;
  // Extract red, green, and blue components from previous pixel
  int prevR = (prevColor >> 16) & 0xFF;
  int prevG = (prevColor >> 8) & 0xFF;
  int prevB = prevColor & 0xFF;
  // Compute the difference of the red, green, and blue values
  int diffR = abs(currR - prevR);
  int diffG = abs(currG - prevG);
  int diffB = abs(currB - prevB);
  // Add these differences to the running tally
  movementSum += diffR + diffG + diffB;
  // Render the difference image to the screen
  pixels[i] = color(diffR, diffG, diffB);
  // The following line is much faster, but more confusing to read
  //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
  // Save the current color into the 'previous' buffer
  previousFrame[i] = currColor;
}
// To prevent flicker from frames that are all black (no movement),
// only update the screen if the image has changed.
if (movementSum > 0) {
  updatePixels();
  println(movementSum); // Print the total amount of movement to the console
}

} }

Sign In or Register to comment.