xxxxxxxxxx
141
let img;
let cam;
let prevCam;
let rowArray;
let noiseValue = 2;
let song;
let isHitting = false;
let hittingTime = 0;
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/9Zbro_T2N/';
let classifier;
let label = "";
let confidence = "";
function preload(){
classifier = ml5.imageClassifier(imageModelURL + 'model.json', modelReady);
song = loadSound('whitenoise.mp3');
song.loop();
}
function setup() {
song.setVolume(0.0);
song.play();
rowArray = [];
createCanvas(640, 480);
cam = createCapture(VIDEO);
//cam.size(640, 480);
cam.hide();
for (let i = 0; i < height; i++)
{
rowArray[i] = random(255);
//console.log(rowArray[i]);
}
img = createImage(width, height);
classifyVideo();
}
function draw() {
background(0);
cam.loadPixels();
img.loadPixels();
if (label == "flap") {
// do something
isHitting = true;
//if (hittingTime)
hittingTime += 1;
//console.log(hittingTime);
}
else
{
//console.log(hittingTime);
isHitting = false;
hittingTime = 0;
}
if (hittingTime > 50)
{
//console.log(hittingTime);
noiseValue = 2;
}
//console.log(deltaTime);
if (noiseValue > 0)
{
song.setVolume(1 - (noiseValue/2));
noiseValue -= deltaTime/20000;
}
//console.log(cam.pixels.length); // w * h * 4(RGBA)
let tempRowArray = [];
tempRowArray[0] = rowArray[height-1];
for (let i = 1; i < height; i++)
{
tempRowArray[i] = rowArray[i-1];
//console.log(rowArray[i]);
}
rowArray = tempRowArray;
for (let y = 0; y < img.height; y++) {
for (let x = 0; x < img.width; x++) {
let index = (x + y * img.width) * 4;
let r = 0;
let g = 0;
let b = 0;
if (random(2) < noiseValue)
{
let offset = floor(random((2 - noiseValue)* 20)) * 4;
r = cam.pixels[index + offset + 0];
g = cam.pixels[index + 1];
b = cam.pixels[index + offset + 2];
}
else{
let tr = (random() < 0.5)? rowArray[y]:255;
r = tr; //cam.pixels[index + 0];
g = tr;//cam.pixels[index + 1];
b = tr;//cam.pixels[index + 2];
}
// original color from the cam
img.pixels[index + 0] = r; // R
img.pixels[index + 1] = g; // G
img.pixels[index + 2] = b; // B
img.pixels[index + 3] = 255; // A
}
}
img.updatePixels();
image(img, 0, 0);
// store the current cam image (one frame)
}
function classifyVideo() {
classifier.classify(cam, gotResult);
}
function gotResult(error, results) {
// If there is an error
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
// console.log(results[0]);
label = results[0].label;
confidence = results[0].confidence;
//console.log(results);
// Classifiy again!
classifyVideo();
}
function modelReady() {
console.log("Model Loaded!");
}