Skip to content

Commit 82f607c

Browse files
author
Ace Nassri
committed
Video samples (#319)
* First + second drafts of the Video API Change-Id: I9bac19d0bf64065c93c6d84ec1edd0cece78af41 * Fix test failures * Change video lib to Cloud Storage link * Use videos that pass licensing requirements
1 parent e7c19e7 commit 82f607c

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

video-intelligence/analyze.js

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
// https://cloud.google.com/video-intelligence/docs/
17+
18+
'use strict';
19+
20+
function analyzeFaces (gcsPath) {
21+
// [START analyze_faces]
22+
// Imports the Google Cloud Video Intelligence library
23+
const Video = require('@google-cloud/videointelligence').v1beta1();
24+
25+
// Instantiates a client
26+
const video = Video.videoIntelligenceServiceClient();
27+
28+
// The GCS filepath of the video to analyze
29+
// const gcsPath = 'gs://my-bucket/my-video.mp4'
30+
31+
const request = {
32+
inputUri: gcsPath,
33+
features: ['FACE_DETECTION']
34+
};
35+
36+
// Detect faces in a video
37+
video.annotateVideo(request)
38+
.then((startResponse) => {
39+
const operation = startResponse[0];
40+
console.log('Waiting for operation to complete...');
41+
return operation.promise();
42+
})
43+
.then((doneResponse) => {
44+
// Get faces for first video
45+
const faces = doneResponse[0].annotationResults[0].faceAnnotations;
46+
faces.forEach((face, faceIdx) => {
47+
console.log('Thumbnail size:', face.thumbnail.buffer.length);
48+
face.segments.forEach((segment, segmentIdx) => {
49+
console.log(`Track ${segmentIdx} of face ${faceIdx}: frames ${segment.startTimeOffset} to ${segment.endTimeOffset}`);
50+
});
51+
});
52+
});
53+
// [END analyze_faces]
54+
}
55+
56+
function analyzeLabels (gcsPath) {
57+
// [START analyze_labels]
58+
// Imports the Google Cloud Video Intelligence library
59+
const Video = require('@google-cloud/videointelligence').v1beta1();
60+
61+
// Instantiates a client
62+
const video = Video.videoIntelligenceServiceClient();
63+
64+
// The GCS filepath of the video to analyze
65+
// const gcsPath = 'gs://my-bucket/my-video.mp4'
66+
67+
const request = {
68+
inputUri: gcsPath,
69+
features: ['LABEL_DETECTION']
70+
};
71+
72+
// Detect labels in a video
73+
video.annotateVideo(request)
74+
.then((startResponse) => {
75+
const operation = startResponse[0];
76+
console.log('Waiting for operation to complete...');
77+
return operation.promise();
78+
})
79+
.then((doneResponse) => {
80+
// Get labels for first video
81+
const labels = doneResponse[0].annotationResults[0].labelAnnotations;
82+
labels.forEach((label) => {
83+
console.log('Label description:', label.description);
84+
console.log('Locations:');
85+
label.locations.forEach((location) => {
86+
console.log(`\tFrames ${location.segment.startTimeOffset} to ${location.segment.endTimeOffset}`);
87+
});
88+
});
89+
});
90+
// [END analyze_labels]
91+
}
92+
93+
function analyzeShots (gcsPath) {
94+
// [START analyze_shots]
95+
// Imports the Google Cloud Video Intelligence library
96+
const Video = require('@google-cloud/videointelligence').v1beta1();
97+
98+
// Instantiates a client
99+
const video = Video.videoIntelligenceServiceClient();
100+
101+
// The GCS filepath of the video to analyze
102+
// const gcsPath = 'gs://my-bucket/my-video.mp4'
103+
104+
const request = {
105+
inputUri: gcsPath,
106+
features: ['SHOT_CHANGE_DETECTION']
107+
};
108+
109+
// Detect camera shot changes
110+
video.annotateVideo(request)
111+
.then((startResponse) => {
112+
const operation = startResponse[0];
113+
console.log('Waiting for operation to complete...');
114+
return operation.promise();
115+
})
116+
.then((doneResponse) => {
117+
// Get shot changes for first video
118+
const shotChanges = doneResponse[0].annotationResults[0].shotAnnotations;
119+
shotChanges.forEach((shot, shotIdx) => {
120+
console.log(`Scene ${shotIdx}:`);
121+
console.log(`\tStart: ${shot.startTimeOffset}`);
122+
console.log(`\tEnd: ${shot.endTimeOffset}`);
123+
});
124+
});
125+
// [END analyze_shots]
126+
}
127+
128+
const cli = require(`yargs`)
129+
.demand(1)
130+
.command(
131+
`faces <gcsPath>`,
132+
`Analyzes faces in a video using the Cloud Video Intelligence API.`,
133+
{},
134+
(opts) => analyzeFaces(opts.gcsPath)
135+
)
136+
.command(
137+
`shots <gcsPath>`,
138+
`Analyzes shot angles in a video using the Cloud Video Intelligence API.`,
139+
{},
140+
(opts) => analyzeShots(opts.gcsPath)
141+
)
142+
.command(
143+
`labels <gcsPath>`,
144+
`Labels objects in a video using the Cloud Video Intelligence API.`,
145+
{},
146+
(opts) => analyzeLabels(opts.gcsPath)
147+
)
148+
.example(`node $0 faces gs://my-bucket/my-video.mp4`)
149+
.example(`node $0 shots gs://my-bucket/my-video.mp4`)
150+
.example(`node $0 labels gs://my-bucket/my-video.mp4`)
151+
.wrap(120)
152+
.recommendCommands()
153+
.epilogue(`For more information, see https://cloud.google.com/video-intelligence/docs`);
154+
155+
if (module === require.main) {
156+
cli.help().strict().argv;
157+
}

video-intelligence/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "nodejs-docs-samples-videointelligence",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache Version 2.0",
6+
"author": "Google Inc.",
7+
"scripts": {
8+
"test": "cd ..; npm run st -- --verbose video/system-test/*.test.js"
9+
},
10+
"dependencies": {
11+
"@google-cloud/videointelligence": "https://storage.googleapis.com/videointelligence-alpha/videointelligence-nodejs.tar.gz",
12+
"yargs": "6.6.0"
13+
},
14+
"engines": {
15+
"node": ">=4.3.2"
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// https://cloud.google.com/video-intelligence/docs/
17+
18+
'use strict';
19+
20+
require(`../../system-test/_setup`);
21+
22+
const cmd = `node analyze.js`;
23+
24+
// analyze_faces
25+
test(`should analyze faces`, async (t) => {
26+
const output = console.log(`${cmd} faces gs://nodejs-docs-samples/video/google_gmail.mp4`);
27+
t.regex(output, 'Thumbnail size: \d+');
28+
});
29+
30+
// analyze_labels
31+
test(`should analyze labels`, async (t) => {
32+
const output = await runAsync(`${cmd} labels gs://nodejs-docs-samples/video/cat.mp4`);
33+
t.regex(output, /Label description: Whiskers/);
34+
});
35+
36+
// analyze_shots
37+
test(`should analyze shots`, async (t) => {
38+
const output = await runAsync(`${cmd} shots gs://nodejs-docs-samples/video/gbike_dinosaur.mp4`);
39+
t.regex(output, /Scene 0:/);
40+
});

0 commit comments

Comments
 (0)