Skip to content

Commit ac87e18

Browse files
stephenpluspluscallmehiphop
authored andcommitted
vision: revert friendly names to API names (#1858)
1 parent f839cc8 commit ac87e18

File tree

2 files changed

+83
-80
lines changed

2 files changed

+83
-80
lines changed

packages/vision/src/index.js

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -834,13 +834,13 @@ Vision.prototype.detect = function(images, options, callback) {
834834
* // }
835835
* // },
836836
* // confidence: 56.748849,
837-
* // blurry: false,
838-
* // dark: false,
839-
* // happy: false,
840-
* // hat: false,
841-
* // mad: false,
842-
* // sad: false,
843-
* // surprised: false
837+
* // anger: false,
838+
* // blurred: false,
839+
* // headwear: false,
840+
* // joy: false,
841+
* // sorrow: false,
842+
* // surprise: false,
843+
* // underExposed: false
844844
* // }
845845
* // ]
846846
* });
@@ -1343,36 +1343,6 @@ Vision.prototype.detectText = function(images, options, callback) {
13431343
this.detect(images, options, callback);
13441344
};
13451345

1346-
/**
1347-
* Convert an object with "likelihood" values to a boolean-representation, based
1348-
* on the lowest likelihood provided.
1349-
*
1350-
* @private
1351-
*
1352-
* @example
1353-
* Vision.convertToBoolean_(Vision.likelihood.VERY_LIKELY, {
1354-
* blurry: 'POSSIBLE'
1355-
* });
1356-
* // { blurry: false }
1357-
*
1358-
* Vision.convertToBoolean_(Vision.likelihood.UNLIKELY, {
1359-
* blurry: 'POSSIBLE'
1360-
* });
1361-
* // { blurry: true }
1362-
*/
1363-
Vision.convertToBoolean_ = function(baseLikelihood, object) {
1364-
var convertedObject = {};
1365-
1366-
for (var prop in object) {
1367-
if (object.hasOwnProperty(prop)) {
1368-
var value = Vision.likelihood[object[prop]];
1369-
convertedObject[prop] = value >= baseLikelihood;
1370-
}
1371-
}
1372-
1373-
return convertedObject;
1374-
};
1375-
13761346
/**
13771347
* Determine the type of image the user is asking to be annotated. If a
13781348
* {module:storage/file}, convert to its "gs://{bucket}/{file}" URL. If a remote
@@ -1586,15 +1556,16 @@ Vision.formatFaceAnnotation_ = function(faceAnnotation) {
15861556
confidence: faceAnnotation.detectionConfidence * 100
15871557
};
15881558

1589-
extend(formattedFaceAnnotation, Vision.convertToBoolean_(LIKELY, {
1590-
blurry: faceAnnotation.blurredLikelihood,
1591-
dark: faceAnnotation.underExposedLikelihood,
1592-
happy: faceAnnotation.joyLikelihood,
1593-
hat: faceAnnotation.headwearLikelihood,
1594-
mad: faceAnnotation.angerLikelihood,
1595-
sad: faceAnnotation.sorrowLikelihood,
1596-
surprised: faceAnnotation.surpriseLikelihood
1597-
}));
1559+
// Remove the `Likelihood` part from a property name.
1560+
// input: "joyLikelihood", output: "joy"
1561+
for (var prop in faceAnnotation) {
1562+
if (prop.indexOf('Likelihood') > -1) {
1563+
var shortenedProp = prop.replace('Likelihood', '');
1564+
1565+
formattedFaceAnnotation[shortenedProp] =
1566+
Vision.gteLikelihood_(LIKELY, faceAnnotation[prop]);
1567+
}
1568+
}
15981569

15991570
return formattedFaceAnnotation;
16001571
};
@@ -1644,12 +1615,33 @@ Vision.formatImagePropertiesAnnotation_ = function(imageAnnotation, options) {
16441615
*/
16451616
Vision.formatSafeSearchAnnotation_ = function(ssAnnotation, options) {
16461617
if (!options.verbose) {
1647-
return Vision.convertToBoolean_(LIKELY, ssAnnotation);
1618+
for (var prop in ssAnnotation) {
1619+
var value = ssAnnotation[prop];
1620+
ssAnnotation[prop] = Vision.gteLikelihood_(LIKELY, value);
1621+
}
1622+
return ssAnnotation;
16481623
}
16491624

16501625
return ssAnnotation;
16511626
};
16521627

1628+
/**
1629+
* Convert a "likelihood" value to a boolean representation, based on the lowest
1630+
* likelihood provided.
1631+
*
1632+
* @private
1633+
*
1634+
* @example
1635+
* Vision.gteLikelihood_(Vision.likelihood.VERY_LIKELY, 'POSSIBLE');
1636+
* // false
1637+
*
1638+
* Vision.gteLikelihood_(Vision.likelihood.UNLIKELY, 'POSSIBLE');
1639+
* // true
1640+
*/
1641+
Vision.gteLikelihood_ = function(baseLikelihood, likelihood) {
1642+
return Vision.likelihood[likelihood] >= baseLikelihood;
1643+
};
1644+
16531645
/*! Developer Documentation
16541646
*
16551647
* All async methods (except for streams) will return a Promise in the event

packages/vision/test/index.js

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -913,31 +913,6 @@ describe('Vision', function() {
913913
});
914914
});
915915

916-
describe('convertToBoolean_', function() {
917-
it('should return booleans', function() {
918-
var baseLikelihood = Vision.likelihood.LIKELY;
919-
920-
var object = {
921-
// f's should be false, t's should be true:
922-
veryUnlikely: 'VERY_UNLIKELY',
923-
unlikely: 'UNLIKELY',
924-
possible: 'POSSIBLE',
925-
likely: 'LIKELY',
926-
veryLikely: 'VERY_LIKELY'
927-
};
928-
929-
var convertedObject = Vision.convertToBoolean_(baseLikelihood, object);
930-
931-
assert.deepEqual(convertedObject, {
932-
veryUnlikely: false,
933-
unlikely: false,
934-
possible: false,
935-
likely: true,
936-
veryLikely: true
937-
});
938-
});
939-
});
940-
941916
describe('findImages_', function() {
942917
it('should convert a File object', function(done) {
943918
var file = {
@@ -1289,7 +1264,9 @@ describe('Vision', function() {
12891264
headwearLikelihood: 'LIKELY',
12901265
angerLikelihood: 'LIKELY',
12911266
sorrowLikelihood: 'LIKELY',
1292-
surpriseLikelihood: 'LIKELY'
1267+
surpriseLikelihood: 'LIKELY',
1268+
1269+
nonExistentLikelihood: 'LIKELY'
12931270
};
12941271
});
12951272

@@ -1378,13 +1355,16 @@ describe('Vision', function() {
13781355

13791356
confidence: faceAnnotation.detectionConfidence * 100,
13801357

1381-
blurry: true,
1382-
dark: true,
1383-
happy: true,
1384-
hat: true,
1385-
mad: true,
1386-
sad: true,
1387-
surprised: true
1358+
anger: true,
1359+
blurred: true,
1360+
headwear: true,
1361+
joy: true,
1362+
sorrow: true,
1363+
surprise: true,
1364+
underExposed: true,
1365+
1366+
// Checks that *any* property that ends in `Likelihood` is shortened.
1367+
nonExistent: true
13881368
};
13891369

13901370
var formatted = Vision.formatFaceAnnotation_(faceAnnotation);
@@ -1486,6 +1466,37 @@ describe('Vision', function() {
14861466
});
14871467
});
14881468

1469+
describe('gteLikelihood_', function() {
1470+
it('should return booleans', function() {
1471+
var baseLikelihood = Vision.likelihood.LIKELY;
1472+
1473+
assert.strictEqual(
1474+
Vision.gteLikelihood_(baseLikelihood, 'VERY_UNLIKELY'),
1475+
false
1476+
);
1477+
1478+
assert.strictEqual(
1479+
Vision.gteLikelihood_(baseLikelihood, 'UNLIKELY'),
1480+
false
1481+
);
1482+
1483+
assert.strictEqual(
1484+
Vision.gteLikelihood_(baseLikelihood, 'POSSIBLE'),
1485+
false
1486+
);
1487+
1488+
assert.strictEqual(
1489+
Vision.gteLikelihood_(baseLikelihood, 'LIKELY'),
1490+
true
1491+
);
1492+
1493+
assert.strictEqual(
1494+
Vision.gteLikelihood_(baseLikelihood, 'VERY_LIKELY'),
1495+
true
1496+
);
1497+
});
1498+
});
1499+
14891500
function testWithoutOptions(type) {
14901501
return function(images, options, callback) {
14911502
assert.strictEqual(images, IMAGE);

0 commit comments

Comments
 (0)