Skip to content

Commit 6113680

Browse files
committed
Add Cloud Vision label detection sample.
1 parent bc87638 commit 6113680

File tree

8 files changed

+120
-2
lines changed

8 files changed

+120
-2
lines changed

test/vision/faceDetection.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function MockCanvas () {
4646
MockCanvas.Image = function () {};
4747

4848
var faceDetectionExample = require('../../vision/faceDetection');
49-
var inputFile = path.resolve(path.join('../../vision', 'face.png'));
49+
var inputFile = path.resolve(path.join('../../vision/resources', 'face.png'));
5050
var outputFile = path.resolve(path.join('../../vision', 'out.png'));
5151

5252
test.cb('should detect faces', function (t) {

test/vision/labelDetection.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var test = require('ava');
17+
var fs = require('fs');
18+
var path = require('path');
19+
20+
var labelDetectionSample = require('../../vision/labelDetection');
21+
var inputFile = path.resolve(path.join('../../vision/resources', 'cat.jpg'));
22+
23+
test.cb('should detect labels', function (t) {
24+
labelDetectionSample.main(
25+
inputFile,
26+
function (err, labels) {
27+
t.ifError(err);
28+
t.ok(labels.length > 0);
29+
t.end();
30+
}
31+
);
32+
});

vision/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ details.
2828
Execute the sample:
2929

3030
node faceDetection "/path/to/image.jpg"
31+
32+
### Label detection sample
33+
34+
Execute the sample:
35+
36+
node labelDetection "/path/to/image.jpg"

vision/labelDetection.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START app]
17+
// [START import_libraries]
18+
var gcloud = require('gcloud');
19+
// [END import_libraries]
20+
21+
// [START authenticate]
22+
// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT
23+
// environment variables to run this sample. See:
24+
// https://github.com/GoogleCloudPlatform/gcloud-node/blob/master/docs/authentication.md
25+
var projectId = process.env.GCLOUD_PROJECT;
26+
27+
// Initialize gcloud
28+
gcloud = gcloud({
29+
projectId: projectId
30+
});
31+
32+
// Get a reference to the vision component
33+
var vision = gcloud.vision();
34+
// [END authenticate]
35+
36+
/**
37+
* Uses the Vision API to detect labels in the given file.
38+
*/
39+
// [START construct_request]
40+
function detectLabels(inputFile, callback) {
41+
// Make a call to the Vision API to detect the labels
42+
vision.detectLabels(inputFile, { verbose: true }, function (err, labels) {
43+
if (err) {
44+
return callback(err);
45+
}
46+
var numLabels = labels.length;
47+
console.log('Found ' + numLabels + (numLabels === 1 ? ' label' : ' labels'));
48+
callback(null, labels);
49+
});
50+
}
51+
// [END construct_request]
52+
53+
// Run the example
54+
function main(inputFile, callback) {
55+
detectLabels(inputFile, function (err, labels) {
56+
if (err) {
57+
return callback(err);
58+
}
59+
60+
// [START construct_request]
61+
console.log('result:', JSON.stringify(labels, null, 2));
62+
// [END construct_request]
63+
callback(null, labels);
64+
});
65+
}
66+
67+
// [START run_application]
68+
if (module === require.main) {
69+
if (process.argv.length < 3) {
70+
console.log('Usage: node labelDetection <inputFile>');
71+
process.exit(1);
72+
}
73+
var inputFile = process.argv[2];
74+
main(inputFile, console.log);
75+
}
76+
// [END run_application]
77+
// [END app]
78+
79+
exports.main = main;

vision/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"node": ">=0.10.x"
99
},
1010
"scripts": {
11-
"faceDetection": "node faceDetection.js"
11+
"faceDetection": "node faceDetection.js",
12+
"labelDetection": "node labelDetection.js"
1213
},
1314
"dependencies": {
1415
"gcloud": "^0.32.0",

vision/resources/cat.jpg

120 KB
Loading
File renamed without changes.

vision/resources/faulkner.jpg

163 KB
Loading

0 commit comments

Comments
 (0)