xxxxxxxxxx
115
let capture;
let update_memory;
let cascadeurl;
let initialized = false;
let canvas;
let ctx;
let mic;
function setup() {
canvas = createCanvas(640, 480);
pixelDensity(1);
ctx = canvas.drawingContext;
capture = createCapture(VIDEO, () => {
capture.size(640, 480);
capture.hide();
});
mic = new p5.AudioIn();
mic.start();
pico_setup();
}
function pico_setup() {
update_memory = pico.instantiate_detection_memory(5); // use detections of the last 5 frames
cascadeurl = 'https://raw.githubusercontent.com/nenadmarkus/pico/c2e81f9d23cc11d1a612fd21e4f9de0921a5d0d9/rnt/cascades/facefinder';
fetch(cascadeurl).then(function (response) {
response.arrayBuffer().then(function (buffer) {
let bytes = new Int8Array(buffer);
facefinder_classify_region = pico.unpack_cascade(bytes);
console.log('Cascade loaded');
});
});
}
function draw() {
if (capture.loadedmetadata) {
pico_draw();
}
}
function rgba_to_grayscale(rgba, nrows, ncols) {
let gray = new Uint8Array(nrows * ncols);
for (let r = 0; r < nrows; ++r) {
for (let c = 0; c < ncols; ++c) {
// gray = 0.2*red + 0.7*green + 0.1*blue
gray[r * ncols + c] = (2 * rgba[r * 4 * ncols + 4 * c + 0] +
7 * rgba[r * 4 * ncols + 4 * c + 1] +
1 * rgba[r * 4 * ncols + 4 * c + 2]) / 10;
}
}
return gray;
}
function pico_process(video) {
// Render the video frame to the canvas
ctx.drawImage(video, 0, 0);
let imageData = ctx.getImageData(0, 0, 640, 480);
let rgba = imageData.data;
// Block size for pixelation
const blockSize = 10;
for (let y = 0; y < 480; y += blockSize) {
for (let x = 0; x < 640; x += blockSize) {
let redSum = 0, greenSum = 0, blueSum = 0, count = 0;
// Calculate average color within the block
for (let yy = y; yy < y + blockSize && yy < 480; yy++) {
for (let xx = x; xx < x + blockSize && xx < 640; xx++) {
let index = (yy * 640 + xx) * 4;
redSum += rgba[index];
greenSum += rgba[index + 1];
blueSum += rgba[index + 2];
count++;
}
}
// Calculate the average intensity
let avgRed = redSum / count;
let avgGreen = greenSum / count;
let avgBlue = blueSum / count;
let intensity = (avgRed + avgGreen + avgBlue) / 3;
// Determine the block color
let blockColor;
if (intensity < 64) {
// Black shades
blockColor = `rgb(${intensity * 0.5}, ${intensity * 0.5}, ${intensity * 0.5})`;
} else if (intensity < 128) {
// Beige (#ddd2a3)
blockColor = `rgb(221, 210, 163)`;
} else if (intensity < 192) {
// Red shades
blockColor = `rgb(${intensity * 1.2}, 0, 0)`;
} else {
// Blue shades
blockColor = `rgb(0, 0, ${intensity * 1.2})`;
}
// Draw the block
ctx.fillStyle = blockColor;
ctx.fillRect(x, y, blockSize, blockSize);
}
}
}
function pico_draw() {
pico_process(capture.elt);
}
var facefinder_classify_region = function (r, c, s, pixels, ldim) {
return -1.0; // Default value if cascade is not loaded
};