From 9e1f616ca305c4b1d01ff542d30cc31d7a0e82a6 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Thu, 27 Apr 2017 18:50:10 -0700 Subject: [PATCH 1/2] Add video quickstart --- video/quickstart.js | 75 ++++++++++++++++++++++++++++ video/system-test/quickstart.test.js | 31 ++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 video/quickstart.js create mode 100644 video/system-test/quickstart.test.js diff --git a/video/quickstart.js b/video/quickstart.js new file mode 100644 index 0000000000..59a7d8540e --- /dev/null +++ b/video/quickstart.js @@ -0,0 +1,75 @@ +/** + * Copyright 2017, 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 videointelligence_quickstart] +// Imports the Google Cloud Video Intelligence library +const Video = require('@google-cloud/videointelligence').v1beta1(); + +// Instantiates a client +const video = Video.videoIntelligenceServiceClient(); + +// The GCS filepath of the video to analyze +const gcsUri = 'gs://nodejs-docs-samples/videointelligence_quickstart.mp4'; + +// Construct request +const request = { + inputUri: gcsUri, + features: ['FACE_DETECTION', 'LABEL_DETECTION', 'SHOT_CHANGE_DETECTION'] +}; + +// Execute request +video.annotateVideo(request) + .then((results) => { + const operation = results[0]; + console.log('Waiting for operation to complete... (this may take a few minutes)'); + return operation.promise(); + }) + .then((results) => { + // Gets annotations for video + const annotations = results[0].annotationResults[0]; + + // Gets faces for video from its annotations + const faces = annotations.faceAnnotations; + faces.forEach((face, faceIdx) => { + console.log('Thumbnail size:', face.thumbnail.buffer.length); + face.segments.forEach((segment, segmentIdx) => { + console.log(`Track ${segmentIdx} of face ${faceIdx}: frames ${segment.startTimeOffset} to ${segment.endTimeOffset}`); + }); + }); + + // Gets labels for video from its annotations + const labels = annotations.labelAnnotations; + labels.forEach((label) => { + console.log('Label description:', label.description); + console.log('Locations:'); + label.locations.forEach((location) => { + console.log(`\tFrames ${location.segment.startTimeOffset} to ${location.segment.endTimeOffset}`); + }); + }); + + // Gets shot changes for video from its annotations + const shotChanges = annotations.shotAnnotations; + shotChanges.forEach((shot, shotIdx) => { + console.log(`Scene ${shotIdx}:`); + console.log(`\tStart: ${shot.startTimeOffset}`); + console.log(`\tEnd: ${shot.endTimeOffset}`); + }); + }) + .catch((err) => { + console.error('ERROR:', err); + }); +// [END videointelligence_quickstart] diff --git a/video/system-test/quickstart.test.js b/video/system-test/quickstart.test.js new file mode 100644 index 0000000000..46e1dd7b2e --- /dev/null +++ b/video/system-test/quickstart.test.js @@ -0,0 +1,31 @@ +/** + * Copyright 2017, 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'; + +const path = require(`path`); +const test = require(`ava`); +const tools = require(`@google-cloud/nodejs-repo-tools`); + +const cmd = `node quickstart.js`; +const cwd = path.join(__dirname, `..`); + +test(`should analyze a hardcoded video`, async (t) => { + const output = await tools.runAsync(cmd, cwd); + t.regex(output, /Label description:/); + t.regex(output, /Frames \d+ to \d+/); + t.regex(output, /Track \d+ of face \d+: frames \d+ to \d+/); + t.regex(output, /Scene \d+/); +}); From 988876863800dbaa93bdce54c8e5cf248679ba71 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 28 Apr 2017 12:41:22 -0700 Subject: [PATCH 2/2] Explicitly specify project ID --- video/quickstart.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/video/quickstart.js b/video/quickstart.js index 59a7d8540e..72b9028ed8 100644 --- a/video/quickstart.js +++ b/video/quickstart.js @@ -20,7 +20,9 @@ const Video = require('@google-cloud/videointelligence').v1beta1(); // Instantiates a client -const video = Video.videoIntelligenceServiceClient(); +const video = Video.videoIntelligenceServiceClient({ + projectId: process.env.GCLOUD_PROJECT // Replace with your Google Cloud project ID +}); // The GCS filepath of the video to analyze const gcsUri = 'gs://nodejs-docs-samples/videointelligence_quickstart.mp4';