|
| 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; |
0 commit comments