-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from GoogleCloudPlatform/label
Add Cloud Vision label detection sample.
- Loading branch information
Showing
8 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <inputFile>'); | ||
process.exit(1); | ||
} | ||
var inputFile = process.argv[2]; | ||
main(inputFile, console.log); | ||
} | ||
// [END run_application] | ||
// [END app] | ||
|
||
exports.main = main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.