|
| 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 | +} |
0 commit comments