xxxxxxxxxx
55
let classifier
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/AI5i76oG/'
let video
let flippedVideo
let label = ''
let voice = new p5.Speech()
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json')
}
function setup() {
createCanvas(windowWidth, windowWidth * 0.75)
video = createCapture(VIDEO)
video.size(width, height)
video.hide()
flippedVideo = ml5.flipImage(video)
classifyVideo()
}
function draw() {
background(0)
image(flippedVideo, 0, 0)
noStroke()
fill(100, 100, 100, 200)
rect(0, height - 64, width, 64)
fill(255)
textFont('system-ui')
textSize(48)
textAlign(CENTER)
text(label, width / 2, height - 15)
}
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult)
}
function gotResult(error, results) {
if (error) {
console.error(error)
return
}
const newLabel = results[0].label
if (newLabel !== label) {
voice.speak(label)
label = newLabel
}
classifyVideo()
}