xxxxxxxxxx
// computer vision test
// hipster burglar alarm
var capture, previous, diff;
var thresh = 30;
var camerax = 64;
var cameray = 64;
// centroid values:
var cx = 0;
var cy = 0;
var osc, delay;
var thenotes = [50, 52, 53, 55, 57, 58, 60, 62];
var t = 0.;
function preload() {
}
function setup() {
createCanvas(windowWidth, windowHeight);
capture = createCapture(VIDEO); // this opens up the camera
pixelDensity(1); // turns off oversampling ("retina display")
capture.size(camerax, cameray); // this is in pixels
capture.hide(); // telling the browser to hide the camera so we can draw it ourselves
previous = createImage(camerax, cameray); // create an empty picture
diff = createImage(camerax, cameray); // create an empty picture
osc = new p5.Oscillator('square');
osc.freq(200);
osc.amp(0.3);
osc.start();
delay = new p5.Delay();
// delay.process() accepts 4 parameters:
// source, delayTime (in seconds), feedback, filter frequency
delay.process(osc, 0.02, .5, 2000);
}
function draw() {
//thresh = mouseY/height * 255; // uncomment this for playing with the threshold
background(0);
capture.loadPixels();
previous.loadPixels();
diff.loadPixels();
noSmooth(); // this makes everything not interpolate
var x, y, p;
var rsum=0; // running sum
var avgx = 0; // average x value
var avgy = 0; // average y value
var numpix = capture.pixels.length/4;
for(let i = 0;i<capture.pixels.length;i+=4)
{
let avg = (abs(capture.pixels[i]-previous.pixels[i]) + abs(capture.pixels[i+1]-previous.pixels[i+1]) + abs(capture.pixels[i+2]-previous.pixels[i+2]))/3;
let bval = (avg>thresh); // binary value
rsum+=bval; // add to running sum
p = floor(i/4);
x = p%capture.width;
y = floor(p/capture.width);
avgx+= bval?x:0;
avgy+= bval?y:0;
// just for show:
diff.pixels[i]=diff.pixels[i+1]=diff.pixels[i+2]=bval*255;
diff.pixels[i+3]=255;
}
if(rsum>0) cx = avgx/rsum; // centroidx
if(rsum>0) cy = avgy/rsum; // centroidy
diff.updatePixels();
for(let i = 0;i<capture.pixels.length;i++)
{
previous.pixels[i] = capture.pixels[i];
}
previous.updatePixels();
image(capture, 0, 0, width/2, height/2);
image(diff, width/2, 0, width/2, height/2);
let cxx = map(cx, 0, capture.width, width/2, width);
let cyy = map(cy, 0, capture.height, 0, height/2);;
fill(255,0,0);
ellipse(cxx, cyy, 80, 80);
textSize(80);
fill(255);
text(cx, width/2, height/2 + 80);
text(cy, width/2, height/2 + 160);
text(rsum, width/2, height/2 + 240);
//text(numpix, width/2, height/2 + 320);
let n = floor(thenotes.length*cx/capture.width);
osc.freq(midiToFreq(thenotes[n])*2);
delay.feedback(cy/capture.height);
let dtime = map(sin(t), -1., 1., 0.02, 0.05);
delay.delayTime(dtime);
t = (t+0.1)%TWO_PI;
}