xxxxxxxxxx
108
const density = ' \u2661';
const density1 = ' ?';
let video;
let label = "waiting...";
// The classifier
let classifier;
let asciiDiv;
let modelURL = 'https://teachablemachine.withgoogle.com/models/J0UOkIUPS/';
let gotResults;
// STEP 1: Load the model!
function preload() {
classifier = ml5.imageClassifier(modelURL + 'model.json');
}
function setup() {
createCanvas(640, 520);
// Create the video
video = createCapture(VIDEO);
video.hide();
// STEP 2: Start classifying
classifyVideo();
}
// STEP 2 classify the videeo!
function classifyVideo() {
classifier.classify(video, gotResults);
}
function draw() {
background(0);
// Draw the video
image(video, 0, 0);
// STEP 4: Draw the label
textSize(32);
textAlign(CENTER, CENTER);
fill(255);
text(label, width / 2, height - 16);
if (label == "One Phone") {
video.loadPixels();
fill(255,0,0);
stroke(255,0,0);
strokeWeight(1);
let asciiImage = "";
for (let j = 0; j < video.height; j++) {
for (let i = 0; i < video.width; i++) {
const pixelIndex = (i + j * video.width) * 4;
const r = video.pixels[pixelIndex + 0];
const g = video.pixels[pixelIndex + 1];
const b = video.pixels[pixelIndex + 2];
const avg = (r + g + b) / 3;
const len = density1.length;
const charIndex = floor(map(avg, 0, 255, 0, len));
const c = density1.charAt(charIndex);
if (c == " ") asciiImage += " ";
else asciiImage += c;
}
asciiImage += '<br/>';
}
asciiDiv.html(asciiImage);
} else if (label == "Two Phone") {
video.loadPixels();
fill(255,0,0);
stroke(255,0,0);
strokeWeight(1);
let asciiImage = "";
for (let j = 0; j < video.height; j++) {
for (let i = 0; i < video.width; i++) {
const pixelIndex = (i + j * video.width) * 4;
const r = video.pixels[pixelIndex + 0];
const g = video.pixels[pixelIndex + 1];
const b = video.pixels[pixelIndex + 2];
const avg = (r + g + b) / 3;
const len = density.length;
const charIndex = floor(map(avg, 0, 255, 0, len));
const c = density.charAt(charIndex);
if (c == " ") asciiImage += " ";
else asciiImage += c;
}
asciiImage += '<br/>';
}
asciiDiv.html(asciiImage);
}
// Draw the emoji
// textSize(256);
// text(emoji, width / 2, height / 2);
// }
// STEP 3: Get the classification!
function gotResults(error, results) {
// Something went wrong!
if (error) {
console.error(error);
return;
}
// Store the label and classify again!
label = results[0].label;
classifyVideo();
}
}