|
// Derived from: https://ml5js.org/docs/video-classification-example |
|
|
|
let classifier; |
|
let video; |
|
|
|
const threshold = 0.6; |
|
|
|
let iftttEventInput; |
|
let iftttKeyInput; |
|
let targetClassNameInput; |
|
|
|
let count = 0; |
|
let isRunning = false; |
|
|
|
function setup() { |
|
noCanvas(); |
|
iftttEventInput = createInput("IFTTT event"); |
|
iftttKeyInput = createInput("IFTTT key"); |
|
targetClassNameInput = createInput("class name"); |
|
|
|
// Create a camera input |
|
video = createCapture(VIDEO); |
|
|
|
// Initialize the Image Classifier method with MobileNet and the video as the second argument |
|
classifier = ml5.imageClassifier("MobileNet", video, modelReady); |
|
} |
|
|
|
function modelReady() { |
|
// Change the status of the model once its ready |
|
select("#status").html("Model Loaded"); |
|
// Call the classifyVideo function to start classifying the video |
|
classifyVideo(); |
|
} |
|
|
|
// Get a prediction for the current video frame |
|
function classifyVideo() { |
|
classifier.predict(gotResult); |
|
} |
|
|
|
// When we get a result |
|
function gotResult(err, results) { |
|
// The results are in an array ordered by probability. |
|
select("#result").html(results[0].className); |
|
select("#probability").html(nf(results[0].probability, 0, 2)); |
|
|
|
const className = results[0].className; |
|
const probability = results[0].probability; |
|
|
|
if (className === targetName && probability > threshold) { |
|
count = count + 1; |
|
select("#status").html("Count: " + count); |
|
if (count >= 10) { |
|
triggerEvent(); |
|
count = 0; |
|
} |
|
} else { |
|
count = 0; |
|
} |
|
|
|
if (isRunning) { |
|
classifyVideo(); |
|
} |
|
} |
|
|
|
function mousePressed() { |
|
if (isRunning) { |
|
isRunning = false; |
|
select("#status").html("Paused"); |
|
} else { |
|
isRunning = true; |
|
select("#status").html("Running"); |
|
classifyVideo(); |
|
} |
|
} |
|
|
|
function triggerEvent() { |
|
console.log("event: " + iftttEventInput.value()); |
|
console.log("key: " + iftttKeyInput.value()); |
|
|
|
const url = |
|
"https://maker.ifttt.com/trigger/" + |
|
event.value() + |
|
"/with/key/" + |
|
apiKey.value(); |
|
|
|
console.log(url); |
|
|
|
httpPost( |
|
url, |
|
result => { |
|
console.log("OK: " + result); |
|
}, |
|
error => { |
|
console.log("ERROR: " + error); |
|
} |
|
); |
|
} |