Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow Buffer as an input image for vision #1488

Merged
merged 3 commits into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/vision/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,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