diff --git a/packages/vision/src/index.js b/packages/vision/src/index.js index 8b6f4871d13..b7dcd4a0967 100644 --- a/packages/vision/src/index.js +++ b/packages/vision/src/index.js @@ -834,13 +834,13 @@ Vision.prototype.detect = function(images, options, callback) { * // } * // }, * // confidence: 56.748849, - * // blurry: false, - * // dark: false, - * // happy: false, - * // hat: false, - * // mad: false, - * // sad: false, - * // surprised: false + * // anger: false, + * // blurred: false, + * // headwear: false, + * // joy: false, + * // sorrow: false, + * // surprise: false, + * // underExposed: false * // } * // ] * }); @@ -1343,36 +1343,6 @@ Vision.prototype.detectText = function(images, options, callback) { this.detect(images, options, callback); }; -/** - * Convert an object with "likelihood" values to a boolean-representation, based - * on the lowest likelihood provided. - * - * @private - * - * @example - * Vision.convertToBoolean_(Vision.likelihood.VERY_LIKELY, { - * blurry: 'POSSIBLE' - * }); - * // { blurry: false } - * - * Vision.convertToBoolean_(Vision.likelihood.UNLIKELY, { - * blurry: 'POSSIBLE' - * }); - * // { blurry: true } - */ -Vision.convertToBoolean_ = function(baseLikelihood, object) { - var convertedObject = {}; - - for (var prop in object) { - if (object.hasOwnProperty(prop)) { - var value = Vision.likelihood[object[prop]]; - convertedObject[prop] = value >= baseLikelihood; - } - } - - return convertedObject; -}; - /** * Determine the type of image the user is asking to be annotated. If a * {module:storage/file}, convert to its "gs://{bucket}/{file}" URL. If a remote @@ -1586,15 +1556,16 @@ Vision.formatFaceAnnotation_ = function(faceAnnotation) { confidence: faceAnnotation.detectionConfidence * 100 }; - extend(formattedFaceAnnotation, Vision.convertToBoolean_(LIKELY, { - blurry: faceAnnotation.blurredLikelihood, - dark: faceAnnotation.underExposedLikelihood, - happy: faceAnnotation.joyLikelihood, - hat: faceAnnotation.headwearLikelihood, - mad: faceAnnotation.angerLikelihood, - sad: faceAnnotation.sorrowLikelihood, - surprised: faceAnnotation.surpriseLikelihood - })); + // Remove the `Likelihood` part from a property name. + // input: "joyLikelihood", output: "joy" + for (var prop in faceAnnotation) { + if (prop.indexOf('Likelihood') > -1) { + var shortenedProp = prop.replace('Likelihood', ''); + + formattedFaceAnnotation[shortenedProp] = + Vision.gteLikelihood_(LIKELY, faceAnnotation[prop]); + } + } return formattedFaceAnnotation; }; @@ -1644,12 +1615,33 @@ Vision.formatImagePropertiesAnnotation_ = function(imageAnnotation, options) { */ Vision.formatSafeSearchAnnotation_ = function(ssAnnotation, options) { if (!options.verbose) { - return Vision.convertToBoolean_(LIKELY, ssAnnotation); + for (var prop in ssAnnotation) { + var value = ssAnnotation[prop]; + ssAnnotation[prop] = Vision.gteLikelihood_(LIKELY, value); + } + return ssAnnotation; } return ssAnnotation; }; +/** + * Convert a "likelihood" value to a boolean representation, based on the lowest + * likelihood provided. + * + * @private + * + * @example + * Vision.gteLikelihood_(Vision.likelihood.VERY_LIKELY, 'POSSIBLE'); + * // false + * + * Vision.gteLikelihood_(Vision.likelihood.UNLIKELY, 'POSSIBLE'); + * // true + */ +Vision.gteLikelihood_ = function(baseLikelihood, likelihood) { + return Vision.likelihood[likelihood] >= baseLikelihood; +}; + /*! Developer Documentation * * All async methods (except for streams) will return a Promise in the event diff --git a/packages/vision/test/index.js b/packages/vision/test/index.js index 60dbf8bb905..c01e8f837b0 100644 --- a/packages/vision/test/index.js +++ b/packages/vision/test/index.js @@ -913,31 +913,6 @@ describe('Vision', function() { }); }); - describe('convertToBoolean_', function() { - it('should return booleans', function() { - var baseLikelihood = Vision.likelihood.LIKELY; - - var object = { - // f's should be false, t's should be true: - veryUnlikely: 'VERY_UNLIKELY', - unlikely: 'UNLIKELY', - possible: 'POSSIBLE', - likely: 'LIKELY', - veryLikely: 'VERY_LIKELY' - }; - - var convertedObject = Vision.convertToBoolean_(baseLikelihood, object); - - assert.deepEqual(convertedObject, { - veryUnlikely: false, - unlikely: false, - possible: false, - likely: true, - veryLikely: true - }); - }); - }); - describe('findImages_', function() { it('should convert a File object', function(done) { var file = { @@ -1289,7 +1264,9 @@ describe('Vision', function() { headwearLikelihood: 'LIKELY', angerLikelihood: 'LIKELY', sorrowLikelihood: 'LIKELY', - surpriseLikelihood: 'LIKELY' + surpriseLikelihood: 'LIKELY', + + nonExistentLikelihood: 'LIKELY' }; }); @@ -1378,13 +1355,16 @@ describe('Vision', function() { confidence: faceAnnotation.detectionConfidence * 100, - blurry: true, - dark: true, - happy: true, - hat: true, - mad: true, - sad: true, - surprised: true + anger: true, + blurred: true, + headwear: true, + joy: true, + sorrow: true, + surprise: true, + underExposed: true, + + // Checks that *any* property that ends in `Likelihood` is shortened. + nonExistent: true }; var formatted = Vision.formatFaceAnnotation_(faceAnnotation); @@ -1486,6 +1466,37 @@ describe('Vision', function() { }); }); + describe('gteLikelihood_', function() { + it('should return booleans', function() { + var baseLikelihood = Vision.likelihood.LIKELY; + + assert.strictEqual( + Vision.gteLikelihood_(baseLikelihood, 'VERY_UNLIKELY'), + false + ); + + assert.strictEqual( + Vision.gteLikelihood_(baseLikelihood, 'UNLIKELY'), + false + ); + + assert.strictEqual( + Vision.gteLikelihood_(baseLikelihood, 'POSSIBLE'), + false + ); + + assert.strictEqual( + Vision.gteLikelihood_(baseLikelihood, 'LIKELY'), + true + ); + + assert.strictEqual( + Vision.gteLikelihood_(baseLikelihood, 'VERY_LIKELY'), + true + ); + }); + }); + function testWithoutOptions(type) { return function(images, options, callback) { assert.strictEqual(images, IMAGE);