// From Learning Processing // Daniel Shiffman // https://www.learningprocessing.com import processing.video.*; import oscP5.*; import netP5.*; import java.util.*; OscP5 oscP5; NetAddress myRemoteLocation; // IP address of destination Capture video; PImage prevFrame; float normFrame; float threshold = 50; // motion threshold float mthresh = 2; // mouth motion threshold float stillness = 1; // total frame motion threshold int xT,yT; // rectangle coordinates for zones float normTile; void setup() { size(320,240); video = new Capture(this, width, height, 10); // Create an empty image the same size as the video prevFrame = createImage(video.width,video.height,RGB); xT = (int) (width*0.333) + 1; yT = (int) (height*0.333) + 1; normFrame = xT*yT; normTile = 1.0/xT/yT; // print(lx+" "+rx+" "+ty+" "+by); oscP5 = new OscP5(this,6600); myRemoteLocation = new NetAddress("127.0.0.1",6600); // print(width+" "+height+" "+x1+" "+y1); } 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(); int[] tiles = new int[9]; // = {0,0,0,0,0,0,0,0,0}; int totalSquare = 0; // Begin loop to walk through every pixel for (int x = 0; x < video.width; x ++ ) { for (int y = 0; y < video.height; y ++ ) { int loc = x + y*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) { totalSquare += diff; // If motion, display black pixels[loc] = color(255); int i = x / xT; int j = y / yT; tiles[i+j*3] += 1; } else { // If not, display the current pixel pixels[loc] = current; } } } OscMessage myMessage = new OscMessage("/scrit1"); for (int i = 0; i < 9; i++) { myMessage.add( tiles[i] * normTile ); } oscP5.send(myMessage, myRemoteLocation); for (int x = 0; x < video.width/2; x ++ ) { for (int y = 0; y < video.height; y ++ ) { int loc = x + y*video.width; int mir = (video.width - 1 - x) + y*video.width; color temp = pixels[loc]; pixels[loc] = pixels[mir]; pixels[mir] = temp; } } updatePixels(); stroke(255); line(0,yT,width,yT); line(0,yT*2,width,yT*2); line(xT*2,0,xT*2,height); line(xT,0,xT,height); }