Created
November 12, 2020 04:29
-
-
Save grant/8d4c7000a07f3b8076e88a7dd3a1cfd6 to your computer and use it in GitHub Desktop.
Critterwatch Vision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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