Skip to content

Instantly share code, notes, and snippets.

@dalequark
Created June 18, 2021 21:11
Show Gist options
  • Save dalequark/017d7ca83c21b9a894833c3335e0cc67 to your computer and use it in GitHub Desktop.
Save dalequark/017d7ca83c21b9a894833c3335e0cc67 to your computer and use it in GitHub Desktop.
Simple object tracker
function huntForPets(monitoredItem, detectionArray, target) {
// naughtyAnimals is an array that keeps track of each animal are in frame
var naughtyAnimals = [];
// Loop through all of the objects recognized by the API
for (let i = 0; i < detectionArray.length; i++) {
// If we found one of the objects we were looking for...
if (detectionArray[i].class === target && detectionArray[i].score > MIN_DETECTION_CONFIDENCE) {
// Draw a bounding box on screen
renderFoundObject(detectionArray[i]);
// Check if there's an intersection between the object of interest (i.e. dog) and the intersection object (i.e. couch)
if (checkIfNear(monitoredItem, detectionArray[i])) {
// If so, add it to the naughtyAnimals array!
naughtyAnimals.push(detectionArray[i]);
}
}
}
// If there are more animals in frame this time than there were last time, send an alert
if (naughtyAnimals.length > lastNaughtyAnimalCount) {
lastNaughtyAnimalCount = naughtyAnimals.length;
if (sendAlerts) {
sendAlerts = false;
sendAlert(naughtyAnimals);
// Cool down to avoid too many notifications too fast in any scenario
setTimeout(cooldown, MIN_ALERT_COOLDOWN_TIME * 1000);
}
} else if (naughtyAnimals.length === 0) {
// If all animals left, reset animal counter so we start triggering events
// again when one comes back in frame.
lastNaughtyAnimalCount = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment