Skip to content

Instantly share code, notes, and snippets.

@grant
Created November 12, 2020 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grant/8d4c7000a07f3b8076e88a7dd3a1cfd6 to your computer and use it in GitHub Desktop.
Save grant/8d4c7000a07f3b8076e88a7dd3a1cfd6 to your computer and use it in GitHub Desktop.
Critterwatch Vision
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient();
const {Firestore} = require('@google-cloud/firestore');
const firestore = new Firestore();
const trackedAnimals = ['raccoon', 'alpaca', 'peacock', 'dog', 'gecko', 'squirrel', 'red panda'];
/**
* Triggered from a change to a Cloud Storage bucket.
*/
exports.analyzeImage = async (event, context) => {
const file = event;
// Run the Cloud Vision API against this file.
const [result] = await visionClient.labelDetection(
`gs://${file.bucket}/${file.name}`
);
const descriptions = Array.from(result.labelAnnotations, la => la.description);
for (let i = 0; i < descriptions.length; ++i) {
const animal = descriptions[i].toLocaleLowerCase();
if (trackedAnimals.includes(animal)) {
const photoUrl = `http://${file.bucket}.storage.googleapis.com/${file.name}`
const data = {
animal,
timestamp: new Date(),
photoUrl,
};
await firestore.collection(animal).doc().set(data);
break;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment