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