Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.video.*;
- import processing.sound.*;
- SoundFile file;
- Capture cam;//webcam pic one
- Capture cam2;
- int count=0;
- PImage myImg=null;//save pic as a PImage
- PImage myImg2=null;
- void setup() {
- frameRate(1);
- size(600, 300);
- cam = new Capture(this, 320, 240, 30);
- cam.start();
- cam2 = new Capture(this, 320, 240, 30);
- cam2.start();
- // file = new SoundFile(this, sketchPath("intruder.mp3"));//audio file
- }
- void draw() {
- println(count);
- if(count<2){
- if(cam.available()) {
- cam.read();//when camera is ready we'll take a pic
- myImg=cam;
- count++;
- }
- }else{
- if(cam2.available()) {
- cam2.read();//after first pic was taken, we'll take this one
- myImg2=cam2;//save it as a PImage
- }
- }
- if(myImg!=null && myImg2!=null){//if both images have been
- //taken, we'll display them to the screen
- image(myImg, 0, 0,200,200);
- image(myImg2, 300, 0,200,200);
- loadPixels();//load the pixels
- myImg.loadPixels();
- myImg2.loadPixels();
- int pixelYPosition;
- int pixelXPosition;
- int counts=0;
- int spotter=0;
- //turns out you can typically get away with only looking at a small portion
- //of the pixels and it will still work.
- for (pixelYPosition = 0; pixelYPosition < 200; pixelYPosition++) {
- for (pixelXPosition = 0; pixelXPosition < 200; pixelXPosition++) {
- int loc = spotter;//pixelXPosition + pixelYPosition * i1.width;
- int loc1 = spotter;//pixelXPosition + pixelYPosition * i2.width;
- float r = red(myImg.pixels[spotter]);//break image into RGB values
- float g = green(myImg.pixels[spotter]);
- float b = blue(myImg.pixels[spotter]);
- float r2 = red(myImg2.pixels[spotter]);//second image
- float g2 = green(myImg2.pixels[spotter]);
- float b2 = blue(myImg2.pixels[spotter]);
- spotter++;
- if (Math.abs(r - r2)>55){//only need to look at red //&& (!(g == g2) && (!(b == b2)))) {
- //if ABS difference is more than 55 we cound that as a difference
- counts++;
- }
- }
- }
- println("num of diffs: " + counts);
- if(counts>800){//if we have found enough differences to make it significant
- println("WE HAVE MOTION DETECTED ");
- // file.play();//audio file
- count=0;
- myImg=null;
- myImg2=null;
- }else{
- println("NO Movement ");
- }
- updatePixels();
- }//if NULLL
- }
Add Comment
Please, Sign In to add comment