Skip to content

Instantly share code, notes, and snippets.



Created Nov 12, 2020
What would you like to do?
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(
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}${}`
const data = {
timestamp: new Date(),
await firestore.collection(animal).doc().set(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.