Skip to content

Commit

Permalink
vision: revert friendly names to API names (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and callmehiphop committed Dec 5, 2016
1 parent f839cc8 commit ac87e18
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 80 deletions.
86 changes: 39 additions & 47 deletions packages/vision/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* // }
* // ]
* });
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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
Expand Down
77 changes: 44 additions & 33 deletions packages/vision/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -1289,7 +1264,9 @@ describe('Vision', function() {
headwearLikelihood: 'LIKELY',
angerLikelihood: 'LIKELY',
sorrowLikelihood: 'LIKELY',
surpriseLikelihood: 'LIKELY'
surpriseLikelihood: 'LIKELY',

nonExistentLikelihood: 'LIKELY'
};
});

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ac87e18

Please sign in to comment.