Skip to content

Commit

Permalink
allow Buffer as an input image for vision (googleapis#1488)
Browse files Browse the repository at this point in the history
* allow Buffer as an input image for vision

* fix linting issues

* remove process.nextTick
  • Loading branch information
calibr authored and stephenplusplus committed Aug 23, 2016
1 parent af29b46 commit 3dc18e1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/vision/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,12 @@ Vision.findImages_ = function(images, callback) {
images = arrify(images);

function findImage(image, callback) {
if (Buffer.isBuffer(image)) {
callback(null, {
content: image.toString('base64')
});
return;
}
if (image instanceof Storage.File) {
callback(null, {
source: {
Expand Down
14 changes: 14 additions & 0 deletions packages/vision/system-test/vision.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ describe('Vision', function() {
});
});

it('should detect from a Buffer', function(done) {
var buffer = fs.readFileSync(IMAGES.logo);
vision.detect(buffer, ['logos'], function(err, logos) {
assert.ifError(err);

var expected = ['Google'];
expected.errors = [];

assert.deepEqual(logos, expected);

done();
});
});

describe('single image', function() {
var TYPES = ['faces', 'labels', 'safeSearch'];

Expand Down
16 changes: 16 additions & 0 deletions packages/vision/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,22 @@ describe('Vision', function() {
});
});


it('should get content from a buffer', function(done) {
var base64String = 'aGVsbG8gd29ybGQ=';
var buffer = new Buffer(base64String, 'base64');

Vision.findImages_(buffer, function(err, images) {
assert.ifError(err);
assert.deepEqual(images, [
{
content: base64String
}
]);
done();
});
});

it('should return an error when file cannot be found', function(done) {
Vision.findImages_('./not-real-file.png', function(err) {
assert.strictEqual(err.code, 'ENOENT');
Expand Down

0 comments on commit 3dc18e1

Please sign in to comment.