diff --git a/test/vision/faceDetection.test.js b/test/vision/faceDetection.test.js index e09c7df5f5..d9defb4f10 100644 --- a/test/vision/faceDetection.test.js +++ b/test/vision/faceDetection.test.js @@ -46,7 +46,7 @@ function MockCanvas () { MockCanvas.Image = function () {}; var faceDetectionExample = require('../../vision/faceDetection'); -var inputFile = path.resolve(path.join('../../vision', 'face.png')); +var inputFile = path.resolve(path.join('../../vision/resources', 'face.png')); var outputFile = path.resolve(path.join('../../vision', 'out.png')); test.cb('should detect faces', function (t) { diff --git a/test/vision/labelDetection.test.js b/test/vision/labelDetection.test.js new file mode 100644 index 0000000000..330d775e30 --- /dev/null +++ b/test/vision/labelDetection.test.js @@ -0,0 +1,31 @@ +// 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 path = require('path'); + +var labelDetectionSample = require('../../vision/labelDetection'); +var inputFile = path.resolve(path.join('../../vision/resources', 'cat.jpg')); + +test.cb('should detect labels', function (t) { + labelDetectionSample.main( + inputFile, + function (err, labels) { + t.ifError(err); + t.ok(labels.length > 0); + t.end(); + } + ); +}); diff --git a/vision/README.md b/vision/README.md index 860b81b347..988500da17 100644 --- a/vision/README.md +++ b/vision/README.md @@ -28,3 +28,9 @@ details. Execute the sample: node faceDetection "/path/to/image.jpg" + +### Label detection sample + +Execute the sample: + + node labelDetection "/path/to/image.jpg" diff --git a/vision/labelDetection.js b/vision/labelDetection.js new file mode 100644 index 0000000000..79eb13e422 --- /dev/null +++ b/vision/labelDetection.js @@ -0,0 +1,78 @@ +// 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 labels in the given file. + */ +// [START construct_request] +function detectLabels(inputFile, callback) { + // Make a call to the Vision API to detect the labels + vision.detectLabels(inputFile, { verbose: true }, function (err, labels) { + if (err) { + return callback(err); + } + console.log('result:', JSON.stringify(labels, null, 2)); + callback(null, labels); + }); +} +// [END construct_request] + +// Run the example +function main(inputFile, callback) { + detectLabels(inputFile, function (err, labels) { + if (err) { + return callback(err); + } + + // [START parse_response] + console.log('Found label: ' + labels[0].desc + ' for ' + inputFile); + // [END parse_response] + callback(null, labels); + }); +} + +// [START run_application] +if (module === require.main) { + if (process.argv.length < 3) { + console.log('Usage: node labelDetection '); + 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 989ac3c8e9..3ea0bfb737 100644 --- a/vision/package.json +++ b/vision/package.json @@ -8,7 +8,8 @@ "node": ">=0.10.x" }, "scripts": { - "faceDetection": "node faceDetection.js" + "faceDetection": "node faceDetection.js", + "labelDetection": "node labelDetection.js" }, "dependencies": { "gcloud": "^0.32.0", diff --git a/vision/resources/cat.jpg b/vision/resources/cat.jpg new file mode 100644 index 0000000000..76af906f0a Binary files /dev/null and b/vision/resources/cat.jpg differ diff --git a/vision/face.png b/vision/resources/face.png similarity index 100% rename from vision/face.png rename to vision/resources/face.png diff --git a/vision/resources/faulkner.jpg b/vision/resources/faulkner.jpg new file mode 100644 index 0000000000..93b8ac3ad2 Binary files /dev/null and b/vision/resources/faulkner.jpg differ