Skip to content

Commit d3c5c1c

Browse files
jmdobryAhrar Monsur
authored andcommitted
Bring ML APIs up to standard. (#346)
1 parent 0cefcb0 commit d3c5c1c

File tree

3 files changed

+1079
-35
lines changed

3 files changed

+1079
-35
lines changed

video-intelligence/analyze.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16-
// https://cloud.google.com/video-intelligence/docs/
17-
1816
'use strict';
1917

20-
function analyzeFaces (gcsPath) {
18+
function analyzeFaces (gcsUri) {
2119
// [START analyze_faces]
2220
// Imports the Google Cloud Video Intelligence library
2321
const Video = require('@google-cloud/videointelligence').v1beta1();
@@ -26,34 +24,37 @@ function analyzeFaces (gcsPath) {
2624
const video = Video.videoIntelligenceServiceClient();
2725

2826
// The GCS filepath of the video to analyze
29-
// const gcsPath = 'gs://my-bucket/my-video.mp4'
27+
// const gcsUri = 'gs://my-bucket/my-video.mp4'
3028

3129
const request = {
32-
inputUri: gcsPath,
30+
inputUri: gcsUri,
3331
features: ['FACE_DETECTION']
3432
};
3533

3634
// Detect faces in a video
3735
video.annotateVideo(request)
38-
.then((startResponse) => {
39-
const operation = startResponse[0];
36+
.then((results) => {
37+
const operation = results[0];
4038
console.log('Waiting for operation to complete...');
4139
return operation.promise();
4240
})
43-
.then((doneResponse) => {
41+
.then((results) => {
4442
// Get faces for first video
45-
const faces = doneResponse[0].annotationResults[0].faceAnnotations;
43+
const faces = results[0].annotationResults[0].faceAnnotations;
4644
faces.forEach((face, faceIdx) => {
4745
console.log('Thumbnail size:', face.thumbnail.buffer.length);
4846
face.segments.forEach((segment, segmentIdx) => {
4947
console.log(`Track ${segmentIdx} of face ${faceIdx}: frames ${segment.startTimeOffset} to ${segment.endTimeOffset}`);
5048
});
5149
});
50+
})
51+
.catch((err) => {
52+
console.error('ERROR:', err);
5253
});
5354
// [END analyze_faces]
5455
}
5556

56-
function analyzeLabels (gcsPath) {
57+
function analyzeLabels (gcsUri) {
5758
// [START analyze_labels]
5859
// Imports the Google Cloud Video Intelligence library
5960
const Video = require('@google-cloud/videointelligence').v1beta1();
@@ -62,35 +63,38 @@ function analyzeLabels (gcsPath) {
6263
const video = Video.videoIntelligenceServiceClient();
6364

6465
// The GCS filepath of the video to analyze
65-
// const gcsPath = 'gs://my-bucket/my-video.mp4'
66+
// const gcsUri = 'gs://my-bucket/my-video.mp4'
6667

6768
const request = {
68-
inputUri: gcsPath,
69+
inputUri: gcsUri,
6970
features: ['LABEL_DETECTION']
7071
};
7172

7273
// Detect labels in a video
7374
video.annotateVideo(request)
74-
.then((startResponse) => {
75-
const operation = startResponse[0];
75+
.then((results) => {
76+
const operation = results[0];
7677
console.log('Waiting for operation to complete...');
7778
return operation.promise();
7879
})
79-
.then((doneResponse) => {
80+
.then((results) => {
8081
// Get labels for first video
81-
const labels = doneResponse[0].annotationResults[0].labelAnnotations;
82+
const labels = results[0].annotationResults[0].labelAnnotations;
8283
labels.forEach((label) => {
8384
console.log('Label description:', label.description);
8485
console.log('Locations:');
8586
label.locations.forEach((location) => {
8687
console.log(`\tFrames ${location.segment.startTimeOffset} to ${location.segment.endTimeOffset}`);
8788
});
8889
});
90+
})
91+
.catch((err) => {
92+
console.error('ERROR:', err);
8993
});
9094
// [END analyze_labels]
9195
}
9296

93-
function analyzeShots (gcsPath) {
97+
function analyzeShots (gcsUri) {
9498
// [START analyze_shots]
9599
// Imports the Google Cloud Video Intelligence library
96100
const Video = require('@google-cloud/videointelligence').v1beta1();
@@ -99,59 +103,61 @@ function analyzeShots (gcsPath) {
99103
const video = Video.videoIntelligenceServiceClient();
100104

101105
// The GCS filepath of the video to analyze
102-
// const gcsPath = 'gs://my-bucket/my-video.mp4'
106+
// const gcsUri = 'gs://my-bucket/my-video.mp4'
103107

104108
const request = {
105-
inputUri: gcsPath,
109+
inputUri: gcsUri,
106110
features: ['SHOT_CHANGE_DETECTION']
107111
};
108112

109113
// Detect camera shot changes
110114
video.annotateVideo(request)
111-
.then((startResponse) => {
112-
const operation = startResponse[0];
115+
.then((results) => {
116+
const operation = results[0];
113117
console.log('Waiting for operation to complete...');
114118
return operation.promise();
115119
})
116-
.then((doneResponse) => {
120+
.then((results) => {
117121
// Get shot changes for first video
118-
const shotChanges = doneResponse[0].annotationResults[0].shotAnnotations;
122+
const shotChanges = results[0].annotationResults[0].shotAnnotations;
119123
shotChanges.forEach((shot, shotIdx) => {
120124
console.log(`Scene ${shotIdx}:`);
121125
console.log(`\tStart: ${shot.startTimeOffset}`);
122126
console.log(`\tEnd: ${shot.endTimeOffset}`);
123127
});
128+
})
129+
.catch((err) => {
130+
console.error('ERROR:', err);
124131
});
125132
// [END analyze_shots]
126133
}
127134

128135
const cli = require(`yargs`)
129136
.demand(1)
130137
.command(
131-
`faces <gcsPath>`,
138+
`faces <gcsUri>`,
132139
`Analyzes faces in a video using the Cloud Video Intelligence API.`,
133140
{},
134-
(opts) => analyzeFaces(opts.gcsPath)
141+
(opts) => analyzeFaces(opts.gcsUri)
135142
)
136143
.command(
137-
`shots <gcsPath>`,
144+
`shots <gcsUri>`,
138145
`Analyzes shot angles in a video using the Cloud Video Intelligence API.`,
139146
{},
140-
(opts) => analyzeShots(opts.gcsPath)
147+
(opts) => analyzeShots(opts.gcsUri)
141148
)
142149
.command(
143-
`labels <gcsPath>`,
150+
`labels <gcsUri>`,
144151
`Labels objects in a video using the Cloud Video Intelligence API.`,
145152
{},
146-
(opts) => analyzeLabels(opts.gcsPath)
153+
(opts) => analyzeLabels(opts.gcsUri)
147154
)
148155
.example(`node $0 faces gs://my-bucket/my-video.mp4`)
149156
.example(`node $0 shots gs://my-bucket/my-video.mp4`)
150157
.example(`node $0 labels gs://my-bucket/my-video.mp4`)
151158
.wrap(120)
152159
.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-
}
160+
.epilogue(`For more information, see https://cloud.google.com/video-intelligence/docs`)
161+
.help()
162+
.strict()
163+
.argv;

video-intelligence/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@google-cloud/videointelligence": "https://storage.googleapis.com/videointelligence-alpha/videointelligence-nodejs.tar.gz",
12-
"yargs": "6.6.0"
12+
"yargs": "7.0.2"
1313
},
1414
"engines": {
1515
"node": ">=4.3.2"

0 commit comments

Comments
 (0)