Skip to content

Commit 1b8513e

Browse files
committed
Merge pull request #111 from GoogleCloudPlatform/label
Add Cloud Vision label detection sample.
2 parents bc87638 + e3bb52c commit 1b8513e

File tree

8 files changed

+118
-2
lines changed

8 files changed

+118
-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

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 path = require('path');
18+
19+
var labelDetectionSample = require('../../vision/labelDetection');
20+
var inputFile = path.resolve(path.join('../../vision/resources', 'cat.jpg'));
21+
22+
test.cb('should detect labels', function (t) {
23+
labelDetectionSample.main(
24+
inputFile,
25+
function (err, labels) {
26+
t.ifError(err);
27+
t.ok(labels.length > 0);
28+
t.end();
29+
}
30+
);
31+
});

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

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
console.log('result:', JSON.stringify(labels, null, 2));
47+
callback(null, labels);
48+
});
49+
}
50+
// [END construct_request]
51+
52+
// Run the example
53+
function main(inputFile, callback) {
54+
detectLabels(inputFile, function (err, labels) {
55+
if (err) {
56+
return callback(err);
57+
}
58+
59+
// [START parse_response]
60+
console.log('Found label: ' + labels[0].desc + ' for ' + inputFile);
61+
// [END parse_response]
62+
callback(null, labels);
63+
});
64+
}
65+
66+
// [START run_application]
67+
if (module === require.main) {
68+
if (process.argv.length < 3) {
69+
console.log('Usage: node labelDetection <inputFile>');
70+
process.exit(1);
71+
}
72+
var inputFile = process.argv[2];
73+
main(inputFile, console.log);
74+
}
75+
// [END run_application]
76+
// [END app]
77+
78+
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)