From c26583fdb4af6148b44db4ea5b90e9b589e3697a Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Fri, 13 May 2016 10:27:21 -0700 Subject: [PATCH] Add Cloud Vision landmark detection sample. --- README.md | 6 ++ test/vision/landmarkDetection.test.js | 29 ++++++++++ vision/README.md | 6 ++ vision/landmarkDetection.js | 80 +++++++++++++++++++++++++++ vision/package.json | 3 +- 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 test/vision/landmarkDetection.test.js create mode 100644 vision/landmarkDetection.js diff --git a/README.md b/README.md index 1d130345e9..69f3d00091 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,8 @@ __Other Examples__ ## Google Cloud Vision - Face detection - [Source code][vision_1] | [Documentation][vision_2] +- Label detection - [Source code][vision_3] | [Documentation][vision_4] +- Landmark detection - [Source code][vision_5] | [Documentation][vision_6] ## Google Prediction API @@ -322,6 +324,10 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma [vision_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/vision/faceDetection.js [vision_2]: https://cloud.google.com/vision/docs/face-tutorial +[vision_3]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/vision/labelDetection.js +[vision_4]: https://cloud.google.com/vision/docs/label-tutorial +[vision_5]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/vision/landmarkDetection.js +[vision_6]: https://cloud.google.com/vision/docs/landmark-tutorial [predictionapi_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/prediction/hostedmodels.js [predictionapi_2]: https://cloud.google.com/prediction/docs/developer-guide#predictionfromappengine diff --git a/test/vision/landmarkDetection.test.js b/test/vision/landmarkDetection.test.js new file mode 100644 index 0000000000..e917b4417b --- /dev/null +++ b/test/vision/landmarkDetection.test.js @@ -0,0 +1,29 @@ +// Copyright 2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +var test = require('ava'); + +var landmarkDetectionSample = require('../../vision/landmarkDetection'); + +test.cb('should detect landmarks', function (t) { + landmarkDetectionSample.main( + 'https://cloud-samples-tests.storage.googleapis.com/vision/water.jpg', + function (err, landmarks) { + t.ifError(err); + t.ok(landmarks.length > 0); + t.end(); + } + ); +}); diff --git a/vision/README.md b/vision/README.md index 988500da17..22780ccea7 100644 --- a/vision/README.md +++ b/vision/README.md @@ -34,3 +34,9 @@ Execute the sample: Execute the sample: node labelDetection "/path/to/image.jpg" + +### Landmark detection sample + +Execute the sample: + + node landmarkDetection "https://cloud-samples-tests.storage.googleapis.com/vision/water.jpg" diff --git a/vision/landmarkDetection.js b/vision/landmarkDetection.js new file mode 100644 index 0000000000..3f6a4a378a --- /dev/null +++ b/vision/landmarkDetection.js @@ -0,0 +1,80 @@ +// Copyright 2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +// [START app] +// [START import_libraries] +var gcloud = require('gcloud'); +// [END import_libraries] + +// [START authenticate] +// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT +// environment variables to run this sample. See: +// https://github.com/GoogleCloudPlatform/gcloud-node/blob/master/docs/authentication.md +var projectId = process.env.GCLOUD_PROJECT; + +// Initialize gcloud +gcloud = gcloud({ + projectId: projectId +}); + +// Get a reference to the vision component +var vision = gcloud.vision(); +// [END authenticate] + +/** + * Uses the Vision API to detect landmarks in the given file. + */ +// [START construct_request] +function detectLandmarks(inputFile, callback) { + var options = { verbose: true }; + + // Make a call to the Vision API to detect the landmarks + vision.detectLandmarks(inputFile, options, function (err, landmarks) { + if (err) { + return callback(err); + } + console.log('result:', JSON.stringify(landmarks, null, 2)); + callback(null, landmarks); + }); +} +// [END construct_request] + +// Run the example +function main(inputFile, callback) { + detectLandmarks(inputFile, function (err, landmarks) { + if (err) { + return callback(err); + } + + // [START parse_response] + console.log('Found landmark: ' + landmarks[0].desc + ' for ' + inputFile); + // [END parse_response] + callback(null, landmarks); + }); +} + +// [START run_application] +if (module === require.main) { + if (process.argv.length < 3) { + console.log('Usage: node landmarkDetection '); + process.exit(1); + } + var inputFile = process.argv[2]; + main(inputFile, console.log); +} +// [END run_application] +// [END app] + +exports.main = main; diff --git a/vision/package.json b/vision/package.json index 3ea0bfb737..dcc8f52ae4 100644 --- a/vision/package.json +++ b/vision/package.json @@ -9,7 +9,8 @@ }, "scripts": { "faceDetection": "node faceDetection.js", - "labelDetection": "node labelDetection.js" + "labelDetection": "node labelDetection.js", + "landmarkDetection": "node landmarkDetection.js" }, "dependencies": { "gcloud": "^0.32.0",