Skip to content

Instantly share code, notes, and snippets.

@sararob
Last active September 29, 2023 06:46
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save sararob/275b252d1eda3a5baa27d6464d2f2198 to your computer and use it in GitHub Desktop.
Save sararob/275b252d1eda3a5baa27d6464d2f2198 to your computer and use it in GitHub Desktop.
'use strict';
// Dependencies
const gcloud = require('google-cloud', {
projectId: 'sara-bigquery',
keyfileName: 'keyfile.json'
});
const vision = gcloud.vision();
const fs = require('fs');
const async = require('async');
// Convert the newline delimited JSON file into JSON to send to our queue
let imageIds = [];
const data = fs.readFileSync('./image-ids.json').toString();
imageIds = data.split("\n").map(function(obj) {
return JSON.parse(obj);
});
let imageData = [];
const features = [
{ "type": "FACE_DETECTION" },
{ "type": "LABEL_DETECTION" },
{ "type": "LANDMARK_DETECTION" },
{ "type": "WEB_DETECTION" },
{ "type": "IMAGE_PROPERTIES" }
];
// Call the Vision API with our GCS image URL
function callVision(task, callback) {
console.log(`processing ${task.object_id}`);
let visionReq = {
"image": {
"source": {
"imageUri": `gs://gcs-storage-bucket/${task.id}.jpg`
}
},
"features": features
}
vision.annotate(visionReq, function(err, annotations, resp) {
if (err) {
return callback(new Error(err));
}
let imgMetadata = annotations[0];
if (!imgMetadata['imagePropertiesAnnotation']) {
return callback(new Error('no data'));
}
imgMetadata['object_id'] = task.name;
imageData.push(imgMetadata);
return callback();
});
}
// The queue sends the image ID to callVision() and writes the response to a local file
// once the entire queue has finished processing
let q = async.queue(callVision, 20);
q.push(imageIds, function (err) {
if (err) {
console.log(err)
}
});
function done() {
q.drain = null;
fs.writeFileSync('./responses.json', JSON.stringify(imageData));
}
// Will only be executed when the queue is drained
q.drain = done;
@moose56
Copy link

moose56 commented Jul 11, 2017

Hi, you can use async functions with the async library. As the vision api can return promises it can work easily with await.

I created a fork as an example https://gist.github.com/moose56/15b2172d95d5ded056383a3942145787

@grondanelli
Copy link

Nice job!
my modest version
https://stackblitz.com/edit/typescript-pqysu6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment