Skip to content

Commit c48fe47

Browse files
jmdobrypull[bot]
authored andcommitted
Add Cloud Vision landmark detection sample.
1 parent b01fdf2 commit c48fe47

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 landmarks in the given file.
38+
*/
39+
// [START construct_request]
40+
function detectLandmarks(inputFile, callback) {
41+
var options = { verbose: true };
42+
43+
// Make a call to the Vision API to detect the landmarks
44+
vision.detectLandmarks(inputFile, options, function (err, landmarks) {
45+
if (err) {
46+
return callback(err);
47+
}
48+
console.log('result:', JSON.stringify(landmarks, null, 2));
49+
callback(null, landmarks);
50+
});
51+
}
52+
// [END construct_request]
53+
54+
// Run the example
55+
function main(inputFile, callback) {
56+
detectLandmarks(inputFile, function (err, landmarks) {
57+
if (err) {
58+
return callback(err);
59+
}
60+
61+
// [START parse_response]
62+
console.log('Found landmark: ' + landmarks[0].desc + ' for ' + inputFile);
63+
// [END parse_response]
64+
callback(null, landmarks);
65+
});
66+
}
67+
68+
// [START run_application]
69+
if (module === require.main) {
70+
if (process.argv.length < 3) {
71+
console.log('Usage: node landmarkDetection <inputFile>');
72+
process.exit(1);
73+
}
74+
var inputFile = process.argv[2];
75+
main(inputFile, console.log);
76+
}
77+
// [END run_application]
78+
// [END app]
79+
80+
exports.main = main;

vision/samples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"scripts": {
1111
"faceDetection": "node faceDetection.js",
12-
"labelDetection": "node labelDetection.js"
12+
"labelDetection": "node labelDetection.js",
13+
"landmarkDetection": "node landmarkDetection.js"
1314
},
1415
"dependencies": {
1516
"gcloud": "^0.32.0",

0 commit comments

Comments
 (0)