Skip to content

Commit 3dcbf19

Browse files
author
Ace Nassri
committed
Add video quickstart (#367)
* Add video quickstart * Explicitly specify project ID
1 parent bd5acb9 commit 3dcbf19

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

video-intelligence/quickstart.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START videointelligence_quickstart]
19+
// Imports the Google Cloud Video Intelligence library
20+
const Video = require('@google-cloud/videointelligence').v1beta1();
21+
22+
// Instantiates a client
23+
const video = Video.videoIntelligenceServiceClient({
24+
projectId: process.env.GCLOUD_PROJECT // Replace with your Google Cloud project ID
25+
});
26+
27+
// The GCS filepath of the video to analyze
28+
const gcsUri = 'gs://nodejs-docs-samples/videointelligence_quickstart.mp4';
29+
30+
// Construct request
31+
const request = {
32+
inputUri: gcsUri,
33+
features: ['FACE_DETECTION', 'LABEL_DETECTION', 'SHOT_CHANGE_DETECTION']
34+
};
35+
36+
// Execute request
37+
video.annotateVideo(request)
38+
.then((results) => {
39+
const operation = results[0];
40+
console.log('Waiting for operation to complete... (this may take a few minutes)');
41+
return operation.promise();
42+
})
43+
.then((results) => {
44+
// Gets annotations for video
45+
const annotations = results[0].annotationResults[0];
46+
47+
// Gets faces for video from its annotations
48+
const faces = annotations.faceAnnotations;
49+
faces.forEach((face, faceIdx) => {
50+
console.log('Thumbnail size:', face.thumbnail.buffer.length);
51+
face.segments.forEach((segment, segmentIdx) => {
52+
console.log(`Track ${segmentIdx} of face ${faceIdx}: frames ${segment.startTimeOffset} to ${segment.endTimeOffset}`);
53+
});
54+
});
55+
56+
// Gets labels for video from its annotations
57+
const labels = annotations.labelAnnotations;
58+
labels.forEach((label) => {
59+
console.log('Label description:', label.description);
60+
console.log('Locations:');
61+
label.locations.forEach((location) => {
62+
console.log(`\tFrames ${location.segment.startTimeOffset} to ${location.segment.endTimeOffset}`);
63+
});
64+
});
65+
66+
// Gets shot changes for video from its annotations
67+
const shotChanges = annotations.shotAnnotations;
68+
shotChanges.forEach((shot, shotIdx) => {
69+
console.log(`Scene ${shotIdx}:`);
70+
console.log(`\tStart: ${shot.startTimeOffset}`);
71+
console.log(`\tEnd: ${shot.endTimeOffset}`);
72+
});
73+
})
74+
.catch((err) => {
75+
console.error('ERROR:', err);
76+
});
77+
// [END videointelligence_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const path = require(`path`);
19+
const test = require(`ava`);
20+
const tools = require(`@google-cloud/nodejs-repo-tools`);
21+
22+
const cmd = `node quickstart.js`;
23+
const cwd = path.join(__dirname, `..`);
24+
25+
test(`should analyze a hardcoded video`, async (t) => {
26+
const output = await tools.runAsync(cmd, cwd);
27+
t.regex(output, /Label description:/);
28+
t.regex(output, /Frames \d+ to \d+/);
29+
t.regex(output, /Track \d+ of face \d+: frames \d+ to \d+/);
30+
t.regex(output, /Scene \d+/);
31+
});

0 commit comments

Comments
 (0)