Skip to content

Instantly share code, notes, and snippets.

@gguuss
Last active November 16, 2017 07:55
Show Gist options
  • Save gguuss/3c69f7fb5457f1824a0fa286e01e4922 to your computer and use it in GitHub Desktop.
Save gguuss/3c69f7fb5457f1824a0fa286e01e4922 to your computer and use it in GitHub Desktop.
const Vision = require("@google-cloud/vision");
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const vision = new Vision();
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.callVision = functions.storage.object().onChange(event => {
const gcsPath = `gs://${event.data.bucket}/${event.data.name}`;
const req = { source: { imageUri: gcsPath } };
return vision.labelDetection(req).then(response => {
return {labels: response[0].labelAnnotations};
}).then((visionResp) => {
return db.collection('images').doc(filePath.slice(7)).set(visionResp);
}).catch(err => {
console.log('vision api error', err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment