Skip to content

Commit 0471d46

Browse files
calibrcallmehiphop
calibr
authored andcommitted
allow Buffer as an input image for vision (#1488)
* allow Buffer as an input image for vision * fix linting issues * remove process.nextTick
1 parent b08f623 commit 0471d46

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

packages/vision/src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,12 @@ Vision.findImages_ = function(images, callback) {
12691269
images = arrify(images);
12701270

12711271
function findImage(image, callback) {
1272+
if (Buffer.isBuffer(image)) {
1273+
callback(null, {
1274+
content: image.toString('base64')
1275+
});
1276+
return;
1277+
}
12721278
if (image instanceof Storage.File) {
12731279
callback(null, {
12741280
source: {

packages/vision/system-test/vision.js

+14
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ describe('Vision', function() {
6666
});
6767
});
6868

69+
it('should detect from a Buffer', function(done) {
70+
var buffer = fs.readFileSync(IMAGES.logo);
71+
vision.detect(buffer, ['logos'], function(err, logos) {
72+
assert.ifError(err);
73+
74+
var expected = ['Google'];
75+
expected.errors = [];
76+
77+
assert.deepEqual(logos, expected);
78+
79+
done();
80+
});
81+
});
82+
6983
describe('single image', function() {
7084
var TYPES = ['faces', 'labels', 'safeSearch'];
7185

packages/vision/test/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,22 @@ describe('Vision', function() {
949949
});
950950
});
951951

952+
953+
it('should get content from a buffer', function(done) {
954+
var base64String = 'aGVsbG8gd29ybGQ=';
955+
var buffer = new Buffer(base64String, 'base64');
956+
957+
Vision.findImages_(buffer, function(err, images) {
958+
assert.ifError(err);
959+
assert.deepEqual(images, [
960+
{
961+
content: base64String
962+
}
963+
]);
964+
done();
965+
});
966+
});
967+
952968
it('should return an error when file cannot be found', function(done) {
953969
Vision.findImages_('./not-real-file.png', function(err) {
954970
assert.strictEqual(err.code, 'ENOENT');

0 commit comments

Comments
 (0)