xxxxxxxxxx
74
let capture;
let previousPixels;
const halfthreshold = 12000;
function setup() {
createCanvas(640, 360);
capture = createCapture(VIDEO);
capture.size(640, 360);
capture.hide();
pixelDensity(1);
noStroke();
}
function copyImage(src, dst) {
let n = src.length;
if (!dst || dst.length != n) dst = new src.constructor(n);
while (n--) dst[n] = src[n];
return dst;
}
function draw() {
let halves = [0, 0];
capture.loadPixels();
let total = 0;
if (capture.pixels.length > 0) {
// don't forget this!
if (!previousPixels) {
previousPixels = copyImage(capture.pixels, previousPixels);
} else
{
let thresholdAmount = 150;
for (let y = 0; y < capture.height; y++) {
for (let x = 0; x < capture.width; x++) {
let index = 4 * (x + y * width);
// calculate the differences
let rdiff = Math.abs(
capture.pixels[index + 0] - previousPixels[index + 0]
);
let gdiff = Math.abs(
capture.pixels[index + 1] - previousPixels[index + 1]
);
let bdiff = Math.abs(
capture.pixels[index + 2] - previousPixels[index + 2]
);
previousPixels[index + 0] = capture.pixels[index + 0];
previousPixels[index + 1] = capture.pixels[index + 1];
previousPixels[index + 2] = capture.pixels[index + 2];
let diffs = rdiff + gdiff + bdiff;
let output = 0;
if (diffs > thresholdAmount) {
output = 255;
total += diffs;
halves[Math.floor(x/capture.width*2)] += diffs
}
}
}
}
}
// need this because sometimes the frames are repeated
if (total > 0) {
capture.updatePixels();
image(capture, 0, 0, 640, 480);
}
let avgMotion=total/capture.pixels.length;
for(let i =0; i<halves.length; i++){
if(halves[i] > halfthreshold){
rect(width/4+i*width/2, height/2, 100, 100);
print(`play sound ${i}`)
}
}
fill(255);
rect(0,height-avgMotion*20,100,height);
}