xxxxxxxxxx
114
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Webcam Image Classification using a pre-trained customized model and p5.js
This example uses p5 preload function to create the classifier
=== */
let faceMesh;
let faces = [];
let options = { maxFaces: 1, refineLandmarks: false, flipHorizontal: false };
function preload() {
// Load the faceMesh model
faceMesh = ml5.faceMesh(options);
}
// Classifier Variable
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/1JNyrT4b1/' ;
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(320, 260);
// Create the video
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
flippedVideo = ml5.flipImage(video)
// Start classifying
classifyVideo();
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
}
function draw() {
background(0);
// Draw the video
image(flippedVideo, 0, 0);
if(label == "laban up"){
// Draw the webcam video
image(video, 0, 0, width, height);
}
// Draw all the tracked face points
for (let i = 0; i < faces.length; i++) {
let face = faces[i];
for (let j = 0; j < face.keypoints.length; j++) {
let keypoint = face.keypoints[j];
fill(0, 255, 0);
noStroke();
//texture("💀");
//textureWrap(0,1,1,1);
circle(keypoint.x, keypoint.y, 2);
// image(winter, 0,0, 320, 240);
// } else if(label == "alain up"){
// image(frankenstein,0,0, 320, 240);
// } else {
// image(astronomen, 0, 0, 320, 240);
}}
// Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
text(label, width / 2, height - 4);
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
}
// When we get a result
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;
// Classifiy again!
classifyVideo();
}