Guest User

Untitled

a guest
Apr 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import processing.video.*;
  2.  
  3. import processing.sound.*;
  4.  
  5. SoundFile file;
  6.  
  7. Capture cam;//webcam pic one
  8. Capture cam2;
  9. int count=0;
  10. PImage myImg=null;//save pic as a PImage
  11. PImage myImg2=null;
  12.  
  13. void setup() {
  14. frameRate(1);
  15.  
  16. size(600, 300);
  17. cam = new Capture(this, 320, 240, 30);
  18. cam.start();
  19. cam2 = new Capture(this, 320, 240, 30);
  20. cam2.start();
  21. // file = new SoundFile(this, sketchPath("intruder.mp3"));//audio file
  22.  
  23. }
  24.  
  25. void draw() {
  26. println(count);
  27. if(count<2){
  28. if(cam.available()) {
  29. cam.read();//when camera is ready we'll take a pic
  30. myImg=cam;
  31. count++;
  32. }
  33. }else{
  34. if(cam2.available()) {
  35. cam2.read();//after first pic was taken, we'll take this one
  36. myImg2=cam2;//save it as a PImage
  37.  
  38. }
  39. }
  40. if(myImg!=null && myImg2!=null){//if both images have been
  41. //taken, we'll display them to the screen
  42. image(myImg, 0, 0,200,200);
  43. image(myImg2, 300, 0,200,200);
  44. loadPixels();//load the pixels
  45. myImg.loadPixels();
  46. myImg2.loadPixels();
  47. int pixelYPosition;
  48. int pixelXPosition;
  49. int counts=0;
  50. int spotter=0;
  51.  
  52.  
  53. //turns out you can typically get away with only looking at a small portion
  54. //of the pixels and it will still work.
  55. for (pixelYPosition = 0; pixelYPosition < 200; pixelYPosition++) {
  56. for (pixelXPosition = 0; pixelXPosition < 200; pixelXPosition++) {
  57.  
  58. int loc = spotter;//pixelXPosition + pixelYPosition * i1.width;
  59. int loc1 = spotter;//pixelXPosition + pixelYPosition * i2.width;
  60.  
  61. float r = red(myImg.pixels[spotter]);//break image into RGB values
  62. float g = green(myImg.pixels[spotter]);
  63. float b = blue(myImg.pixels[spotter]);
  64.  
  65. float r2 = red(myImg2.pixels[spotter]);//second image
  66. float g2 = green(myImg2.pixels[spotter]);
  67. float b2 = blue(myImg2.pixels[spotter]);
  68. spotter++;
  69. if (Math.abs(r - r2)>55){//only need to look at red //&& (!(g == g2) && (!(b == b2)))) {
  70. //if ABS difference is more than 55 we cound that as a difference
  71. counts++;
  72.  
  73. }
  74.  
  75. }
  76. }
  77.  
  78. println("num of diffs: " + counts);
  79. if(counts>800){//if we have found enough differences to make it significant
  80. println("WE HAVE MOTION DETECTED ");
  81. // file.play();//audio file
  82. count=0;
  83. myImg=null;
  84. myImg2=null;
  85. }else{
  86.  
  87. println("NO Movement ");
  88. }
  89.  
  90. updatePixels();
  91.  
  92. }//if NULLL
  93.  
  94. }
Add Comment
Please, Sign In to add comment