From 48b6e381455e9943bc17274bdc72265a68f21a6b Mon Sep 17 00:00:00 2001 From: Praveen Kumar Singh Date: Wed, 14 Nov 2018 13:31:00 +0530 Subject: [PATCH 1/6] replace promise with async await for sample folder --- samples/analyze.js | 445 ++++++++++++++++------------------- samples/analyze.v1p2beta1.js | 83 ++++--- samples/quickstart.js | 109 +++++---- 3 files changed, 300 insertions(+), 337 deletions(-) diff --git a/samples/analyze.js b/samples/analyze.js index f9072451..0b2d86db 100644 --- a/samples/analyze.js +++ b/samples/analyze.js @@ -15,7 +15,7 @@ 'use strict'; -function analyzeLabelsGCS(gcsUri) { +async function analyzeLabelsGCS(gcsUri) { // [START video_analyze_labels_gcs] // Imports the Google Cloud Video Intelligence library const video = require('@google-cloud/video-intelligence').v1; @@ -34,57 +34,50 @@ function analyzeLabelsGCS(gcsUri) { }; // Detects labels in a video - client - .annotateVideo(request) - .then(results => { - const operation = results[0]; - console.log('Waiting for operation to complete...'); - return operation.promise(); - }) - .then(results => { - // Gets annotations for video - const annotations = results[0].annotationResults[0]; - - const labels = annotations.segmentLabelAnnotations; - labels.forEach(label => { - console.log(`Label ${label.entity.description} occurs at:`); - label.segments.forEach(segment => { - const time = segment.segment; - if (time.startTimeOffset.seconds === undefined) { - time.startTimeOffset.seconds = 0; - } - if (time.startTimeOffset.nanos === undefined) { - time.startTimeOffset.nanos = 0; - } - if (time.endTimeOffset.seconds === undefined) { - time.endTimeOffset.seconds = 0; - } - if (time.endTimeOffset.nanos === undefined) { - time.endTimeOffset.nanos = 0; - } - console.log( - `\tStart: ${time.startTimeOffset.seconds}` + - `.${(time.startTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log( - `\tEnd: ${time.endTimeOffset.seconds}.` + - `${(time.endTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log(`\tConfidence: ${segment.confidence}`); - }); - }); - }) - .catch(err => { - console.error('ERROR:', err); + const [operation] = await client.annotateVideo(request); + console.log('Waiting for operation to complete...'); + const [operationResult] = await operation.promise(); + + // Gets annotations for video + const annotations = operationResult.annotationResults[0]; + + const labels = annotations.segmentLabelAnnotations; + labels.forEach(label => { + console.log(`Label ${label.entity.description} occurs at:`); + label.segments.forEach(segment => { + const time = segment.segment; + if (time.startTimeOffset.seconds === undefined) { + time.startTimeOffset.seconds = 0; + } + if (time.startTimeOffset.nanos === undefined) { + time.startTimeOffset.nanos = 0; + } + if (time.endTimeOffset.seconds === undefined) { + time.endTimeOffset.seconds = 0; + } + if (time.endTimeOffset.nanos === undefined) { + time.endTimeOffset.nanos = 0; + } + console.log( + `\tStart: ${time.startTimeOffset.seconds}` + + `.${(time.startTimeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log( + `\tEnd: ${time.endTimeOffset.seconds}.` + + `${(time.endTimeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log(`\tConfidence: ${segment.confidence}`); }); + }); // [END video_analyze_labels_gcs] } -function analyzeLabelsLocal(path) { +async function analyzeLabelsLocal(path) { // [START video_analyze_labels] // Imports the Google Cloud Video Intelligence library + Node's fs library const video = require('@google-cloud/video-intelligence').v1; const fs = require('fs'); + const util = require('util'); // Creates a client const client = new video.VideoIntelligenceServiceClient(); @@ -95,7 +88,8 @@ function analyzeLabelsLocal(path) { // const path = 'Local file to analyze, e.g. ./my-file.mp4'; // Reads a local video file and converts it to base64 - const file = fs.readFileSync(path); + const readFile = util.promisify(fs.readFile); + const file = await readFile(path); const inputContent = file.toString('base64'); // Constructs request @@ -105,53 +99,45 @@ function analyzeLabelsLocal(path) { }; // Detects labels in a video - client - .annotateVideo(request) - .then(results => { - const operation = results[0]; - console.log('Waiting for operation to complete...'); - return operation.promise(); - }) - .then(results => { - // Gets annotations for video - const annotations = results[0].annotationResults[0]; - - const labels = annotations.segmentLabelAnnotations; - labels.forEach(label => { - console.log(`Label ${label.entity.description} occurs at:`); - label.segments.forEach(segment => { - const time = segment.segment; - if (time.startTimeOffset.seconds === undefined) { - time.startTimeOffset.seconds = 0; - } - if (time.startTimeOffset.nanos === undefined) { - time.startTimeOffset.nanos = 0; - } - if (time.endTimeOffset.seconds === undefined) { - time.endTimeOffset.seconds = 0; - } - if (time.endTimeOffset.nanos === undefined) { - time.endTimeOffset.nanos = 0; - } - console.log( - `\tStart: ${time.startTimeOffset.seconds}` + - `.${(time.startTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log( - `\tEnd: ${time.endTimeOffset.seconds}.` + - `${(time.endTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log(`\tConfidence: ${segment.confidence}`); - }); - }); - }) - .catch(err => { - console.error('ERROR:', err); + const [operation] = await client.annotateVideo(request); + console.log('Waiting for operation to complete...'); + const [operationResult] = await operation.promise(); + // Gets annotations for video + const annotations = operationResult.annotationResults[0]; + + const labels = annotations.segmentLabelAnnotations; + labels.forEach(label => { + console.log(`Label ${label.entity.description} occurs at:`); + label.segments.forEach(segment => { + const time = segment.segment; + if (time.startTimeOffset.seconds === undefined) { + time.startTimeOffset.seconds = 0; + } + if (time.startTimeOffset.nanos === undefined) { + time.startTimeOffset.nanos = 0; + } + if (time.endTimeOffset.seconds === undefined) { + time.endTimeOffset.seconds = 0; + } + if (time.endTimeOffset.nanos === undefined) { + time.endTimeOffset.nanos = 0; + } + console.log( + `\tStart: ${time.startTimeOffset.seconds}` + + `.${(time.startTimeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log( + `\tEnd: ${time.endTimeOffset.seconds}.` + + `${(time.endTimeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log(`\tConfidence: ${segment.confidence}`); }); + }); + // [END video_analyze_labels] } -function analyzeShots(gcsUri) { +async function analyzeShots(gcsUri) { // [START video_analyze_shots] // Imports the Google Cloud Video Intelligence library const video = require('@google-cloud/video-intelligence').v1; @@ -170,59 +156,51 @@ function analyzeShots(gcsUri) { }; // Detects camera shot changes - client - .annotateVideo(request) - .then(results => { - const operation = results[0]; - console.log('Waiting for operation to complete...'); - return operation.promise(); - }) - .then(results => { - // Gets shot changes - const shotChanges = results[0].annotationResults[0].shotAnnotations; - console.log('Shot changes:'); - - if (shotChanges.length === 1) { - console.log(`The entire video is one shot.`); - } else { - shotChanges.forEach((shot, shotIdx) => { - console.log(`Scene ${shotIdx} occurs from:`); - if (shot.startTimeOffset === undefined) { - shot.startTimeOffset = {}; - } - if (shot.endTimeOffset === undefined) { - shot.endTimeOffset = {}; - } - if (shot.startTimeOffset.seconds === undefined) { - shot.startTimeOffset.seconds = 0; - } - if (shot.startTimeOffset.nanos === undefined) { - shot.startTimeOffset.nanos = 0; - } - if (shot.endTimeOffset.seconds === undefined) { - shot.endTimeOffset.seconds = 0; - } - if (shot.endTimeOffset.nanos === undefined) { - shot.endTimeOffset.nanos = 0; - } - console.log( - `\tStart: ${shot.startTimeOffset.seconds}` + - `.${(shot.startTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log( - `\tEnd: ${shot.endTimeOffset.seconds}.` + - `${(shot.endTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - }); + const [operation] = await client.annotateVideo(request); + console.log('Waiting for operation to complete...'); + const [operationResult] = await operation.promise(); + // Gets shot changes + const shotChanges = operationResult.annotationResults[0].shotAnnotations; + console.log('Shot changes:'); + + if (shotChanges.length === 1) { + console.log(`The entire video is one shot.`); + } else { + shotChanges.forEach((shot, shotIdx) => { + console.log(`Scene ${shotIdx} occurs from:`); + if (shot.startTimeOffset === undefined) { + shot.startTimeOffset = {}; + } + if (shot.endTimeOffset === undefined) { + shot.endTimeOffset = {}; } - }) - .catch(err => { - console.error('ERROR:', err); + if (shot.startTimeOffset.seconds === undefined) { + shot.startTimeOffset.seconds = 0; + } + if (shot.startTimeOffset.nanos === undefined) { + shot.startTimeOffset.nanos = 0; + } + if (shot.endTimeOffset.seconds === undefined) { + shot.endTimeOffset.seconds = 0; + } + if (shot.endTimeOffset.nanos === undefined) { + shot.endTimeOffset.nanos = 0; + } + console.log( + `\tStart: ${shot.startTimeOffset.seconds}` + + `.${(shot.startTimeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log( + `\tEnd: ${shot.endTimeOffset.seconds}.` + + `${(shot.endTimeOffset.nanos / 1e6).toFixed(0)}s` + ); }); + } + // [END video_analyze_shots] } -function analyzeSafeSearch(gcsUri) { +async function analyzeSafeSearch(gcsUri) { // [START video_analyze_explicit_content] // Imports the Google Cloud Video Intelligence library const video = require('@google-cloud/video-intelligence').v1; @@ -251,46 +229,35 @@ function analyzeSafeSearch(gcsUri) { ]; // Detects unsafe content - client - .annotateVideo(request) - .then(results => { - const operation = results[0]; - console.log('Waiting for operation to complete...'); - return operation.promise(); - }) - .then(results => { - // Gets unsafe content - const explicitContentResults = - results[0].annotationResults[0].explicitAnnotation; - console.log('Explicit annotation results:'); - explicitContentResults.frames.forEach(result => { - if (result.timeOffset === undefined) { - result.timeOffset = {}; - } - if (result.timeOffset.seconds === undefined) { - result.timeOffset.seconds = 0; - } - if (result.timeOffset.nanos === undefined) { - result.timeOffset.nanos = 0; - } - console.log( - `\tTime: ${result.timeOffset.seconds}` + - `.${(result.timeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log( - `\t\tPornography liklihood: ${ - likelihoods[result.pornographyLikelihood] - }` - ); - }); - }) - .catch(err => { - console.error('ERROR:', err); - }); + const [opertaion] = await client.annotateVideo(request); + console.log('Waiting for operation to complete...'); + const [operationResult] = await opertaion.promise(); + // Gets unsafe content + const explicitContentResults = + operationResult.annotationResults[0].explicitAnnotation; + console.log('Explicit annotation results:'); + explicitContentResults.frames.forEach(result => { + if (result.timeOffset === undefined) { + result.timeOffset = {}; + } + if (result.timeOffset.seconds === undefined) { + result.timeOffset.seconds = 0; + } + if (result.timeOffset.nanos === undefined) { + result.timeOffset.nanos = 0; + } + console.log( + `\tTime: ${result.timeOffset.seconds}` + + `.${(result.timeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log( + `\t\tPornography liklihood: ${likelihoods[result.pornographyLikelihood]}` + ); + }); // [END video_analyze_explicit_content] } -function analyzeVideoTranscription(gcsUri) { +async function analyzeVideoTranscription(gcsUri) { // [START video_speech_transcription_gcs_beta] // Imports the Google Cloud Video Intelligence library const videoIntelligence = require('@google-cloud/video-intelligence') @@ -317,75 +284,69 @@ function analyzeVideoTranscription(gcsUri) { videoContext: videoContext, }; - client - .annotateVideo(request) - .then(results => { - const operation = results[0]; - console.log('Waiting for operation to complete...'); - return operation.promise(); - }) - .then(results => { - console.log('Word level information:'); - const alternative = - results[0].annotationResults[0].speechTranscriptions[0].alternatives[0]; - alternative.words.forEach(wordInfo => { - const start_time = - wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9; - const end_time = - wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9; - console.log( - '\t' + start_time + 's - ' + end_time + 's: ' + wordInfo.word - ); - }); - console.log('Transcription: ' + alternative.transcript); - }) - .catch(err => { - console.error('ERROR:', err); - }); + const [operation] = await client.annotateVideo(request); + console.log('Waiting for operation to complete...'); + const operationResult = await operation.promise(); + console.log('Word level information:'); + const alternative = + operationResult.annotationResults[0].speechTranscriptions[0] + .alternatives[0]; + alternative.words.forEach(wordInfo => { + const start_time = + wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9; + const end_time = wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9; + console.log('\t' + start_time + 's - ' + end_time + 's: ' + wordInfo.word); + }); + console.log('Transcription: ' + alternative.transcript); + // [END video_speech_transcription_gcs_beta] } -require(`yargs`) - .demand(1) - .command( - `shots `, - `Analyzes shot angles in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, - {}, - opts => analyzeShots(opts.gcsUri) - ) - .command( - `labels-gcs `, - `Labels objects in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, - {}, - opts => analyzeLabelsGCS(opts.gcsUri) - ) - .command( - `labels-file `, - `Labels objects in a video stored locally using the Cloud Video Intelligence API.`, - {}, - opts => analyzeLabelsLocal(opts.filePath) - ) - .command( - `safe-search `, - `Detects explicit content in a video stored in Google Cloud Storage.`, - {}, - opts => analyzeSafeSearch(opts.gcsUri) - ) - .command( - `transcription `, - `Extract the video transcription using the Cloud Video Intelligence API.`, - {}, - opts => analyzeVideoTranscription(opts.gcsUri) - ) - .example(`node $0 shots gs://demomaker/sushi.mp4`) - .example(`node $0 labels-gcs gs://demomaker/tomatoes.mp4`) - .example(`node $0 labels-file cat.mp4`) - .example(`node $0 safe-search gs://demomaker/tomatoes.mp4`) - .example(`node $0 transcription gs://demomaker/tomatoes.mp4`) - .wrap(120) - .recommendCommands() - .epilogue( - `For more information, see https://cloud.google.com/video-intelligence/docs` - ) - .help() - .strict().argv; +async function main() { + require(`yargs`) + .demand(1) + .command( + `shots `, + `Analyzes shot angles in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeShots(opts.gcsUri) + ) + .command( + `labels-gcs `, + `Labels objects in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeLabelsGCS(opts.gcsUri) + ) + .command( + `labels-file `, + `Labels objects in a video stored locally using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeLabelsLocal(opts.filePath) + ) + .command( + `safe-search `, + `Detects explicit content in a video stored in Google Cloud Storage.`, + {}, + async opts => await analyzeSafeSearch(opts.gcsUri) + ) + .command( + `transcription `, + `Extract the video transcription using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeVideoTranscription(opts.gcsUri) + ) + .example(`node $0 shots gs://demomaker/sushi.mp4`) + .example(`node $0 labels-gcs gs://demomaker/tomatoes.mp4`) + .example(`node $0 labels-file cat.mp4`) + .example(`node $0 safe-search gs://demomaker/tomatoes.mp4`) + .example(`node $0 transcription gs://demomaker/tomatoes.mp4`) + .wrap(120) + .recommendCommands() + .epilogue( + `For more information, see https://cloud.google.com/video-intelligence/docs` + ) + .help() + .strict().argv; +} + +main().catch(console.error); diff --git a/samples/analyze.v1p2beta1.js b/samples/analyze.v1p2beta1.js index a502d66f..5d1a7b19 100644 --- a/samples/analyze.v1p2beta1.js +++ b/samples/analyze.v1p2beta1.js @@ -304,42 +304,47 @@ async function analyzeObjectTracking(path) { }); // [END video_object_tracking_beta] } -require(`yargs`) - .demand(1) - .command( - `video-text-gcs `, - `Analyzes text in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, - {}, - opts => analyzeTextGCS(opts.gcsUri) - ) - .command( - `track-objects-gcs `, - `Analyzes objects in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, - {}, - opts => analyzeObjectTrackingGCS(opts.gcsUri) - ) - .command( - `video-text `, - `Analyzes text in a video stored in a local file using the Cloud Video Intelligence API.`, - {}, - opts => analyzeText(opts.path) - ) - .command( - `track-objects `, - `Analyzes objects in a video stored in a local file using the Cloud Video Intelligence API.`, - {}, - opts => analyzeObjectTracking(opts.path) - ) - .example(`node $0 video-text ./resources/googlework_short.mp4`) - .example( - `node $0 video-text-gcs gs://nodejs-docs-samples/videos/googlework_short.mp4` - ) - .example(`node $0 track-objects ./resources/cat.mp4`) - .example(`node $0 track-objects-gcs gs://nodejs-docs-samples/video/cat.mp4`) - .wrap(120) - .recommendCommands() - .epilogue( - `For more information, see https://cloud.google.com/video-intelligence/docs` - ) - .help() - .strict().argv; + +async function main() { + require(`yargs`) + .demand(1) + .command( + `video-text-gcs `, + `Analyzes text in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeTextGCS(opts.gcsUri) + ) + .command( + `track-objects-gcs `, + `Analyzes objects in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeObjectTrackingGCS(opts.gcsUri) + ) + .command( + `video-text `, + `Analyzes text in a video stored in a local file using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeText(opts.path) + ) + .command( + `track-objects `, + `Analyzes objects in a video stored in a local file using the Cloud Video Intelligence API.`, + {}, + async opts => await analyzeObjectTracking(opts.path) + ) + .example(`node $0 video-text ./resources/googlework_short.mp4`) + .example( + `node $0 video-text-gcs gs://nodejs-docs-samples/videos/googlework_short.mp4` + ) + .example(`node $0 track-objects ./resources/cat.mp4`) + .example(`node $0 track-objects-gcs gs://nodejs-docs-samples/video/cat.mp4`) + .wrap(120) + .recommendCommands() + .epilogue( + `For more information, see https://cloud.google.com/video-intelligence/docs` + ) + .help() + .strict().argv; +} + +main().catch(console.error); diff --git a/samples/quickstart.js b/samples/quickstart.js index d09c139c..5adc54b7 100644 --- a/samples/quickstart.js +++ b/samples/quickstart.js @@ -14,67 +14,64 @@ */ 'use strict'; +async function main() { + // [START video_quickstart] + // Imports the Google Cloud Video Intelligence library + const videoIntelligence = require('@google-cloud/video-intelligence'); -// [START video_quickstart] -// Imports the Google Cloud Video Intelligence library -const videoIntelligence = require('@google-cloud/video-intelligence'); + // Creates a client + const client = new videoIntelligence.VideoIntelligenceServiceClient(); -// Creates a client -const client = new videoIntelligence.VideoIntelligenceServiceClient(); + // The GCS uri of the video to analyze + const gcsUri = 'gs://nodejs-docs-samples-video/quickstart_short.mp4'; -// The GCS uri of the video to analyze -const gcsUri = 'gs://nodejs-docs-samples-video/quickstart_short.mp4'; + // Construct request + const request = { + inputUri: gcsUri, + features: ['LABEL_DETECTION'], + }; -// Construct request -const request = { - inputUri: gcsUri, - features: ['LABEL_DETECTION'], -}; + // Execute request + const [operation] = await client.annotateVideo(request); -// Execute request -client - .annotateVideo(request) - .then(results => { - const operation = results[0]; - console.log( - 'Waiting for operation to complete... (this may take a few minutes)' - ); - return operation.promise(); - }) - .then(results => { - // Gets annotations for video - const annotations = results[0].annotationResults[0]; + console.log( + 'Waiting for operation to complete... (this may take a few minutes)' + ); - // Gets labels for video from its annotations - const labels = annotations.segmentLabelAnnotations; - labels.forEach(label => { - console.log(`Label ${label.entity.description} occurs at:`); - label.segments.forEach(segment => { - segment = segment.segment; - if (segment.startTimeOffset.seconds === undefined) { - segment.startTimeOffset.seconds = 0; - } - if (segment.startTimeOffset.nanos === undefined) { - segment.startTimeOffset.nanos = 0; - } - if (segment.endTimeOffset.seconds === undefined) { - segment.endTimeOffset.seconds = 0; - } - if (segment.endTimeOffset.nanos === undefined) { - segment.endTimeOffset.nanos = 0; - } - console.log( - `\tStart: ${segment.startTimeOffset.seconds}` + - `.${(segment.startTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log( - `\tEnd: ${segment.endTimeOffset.seconds}.` + - `${(segment.endTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - }); + const [operationResult] = await operation.promise(); + + // Gets annotations for video + const annotations = operationResult.annotationResults[0]; + + // Gets labels for video from its annotations + const labels = annotations.segmentLabelAnnotations; + labels.forEach(label => { + console.log(`Label ${label.entity.description} occurs at:`); + label.segments.forEach(segment => { + segment = segment.segment; + if (segment.startTimeOffset.seconds === undefined) { + segment.startTimeOffset.seconds = 0; + } + if (segment.startTimeOffset.nanos === undefined) { + segment.startTimeOffset.nanos = 0; + } + if (segment.endTimeOffset.seconds === undefined) { + segment.endTimeOffset.seconds = 0; + } + if (segment.endTimeOffset.nanos === undefined) { + segment.endTimeOffset.nanos = 0; + } + console.log( + `\tStart: ${segment.startTimeOffset.seconds}` + + `.${(segment.startTimeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log( + `\tEnd: ${segment.endTimeOffset.seconds}.` + + `${(segment.endTimeOffset.nanos / 1e6).toFixed(0)}s` + ); }); - }) - .catch(err => { - console.error('ERROR:', err); }); -// [END video_quickstart] + // [END video_quickstart] +} + +main().catch(console.error); From 40c1f99996fd96d8766044b973a154b2075560af Mon Sep 17 00:00:00 2001 From: Praveen Kumar Singh Date: Thu, 15 Nov 2018 22:40:12 +0530 Subject: [PATCH 2/6] removed async await from yargs command --- samples/analyze.js | 10 +++++----- samples/analyze.v1p2beta1.js | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/samples/analyze.js b/samples/analyze.js index 0b2d86db..c3412687 100644 --- a/samples/analyze.js +++ b/samples/analyze.js @@ -309,31 +309,31 @@ async function main() { `shots `, `Analyzes shot angles in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeShots(opts.gcsUri) + opts => analyzeShots(opts.gcsUri) ) .command( `labels-gcs `, `Labels objects in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeLabelsGCS(opts.gcsUri) + opts => analyzeLabelsGCS(opts.gcsUri) ) .command( `labels-file `, `Labels objects in a video stored locally using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeLabelsLocal(opts.filePath) + opts => analyzeLabelsLocal(opts.filePath) ) .command( `safe-search `, `Detects explicit content in a video stored in Google Cloud Storage.`, {}, - async opts => await analyzeSafeSearch(opts.gcsUri) + opts => analyzeSafeSearch(opts.gcsUri) ) .command( `transcription `, `Extract the video transcription using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeVideoTranscription(opts.gcsUri) + opts => analyzeVideoTranscription(opts.gcsUri) ) .example(`node $0 shots gs://demomaker/sushi.mp4`) .example(`node $0 labels-gcs gs://demomaker/tomatoes.mp4`) diff --git a/samples/analyze.v1p2beta1.js b/samples/analyze.v1p2beta1.js index 5d1a7b19..1e70051a 100644 --- a/samples/analyze.v1p2beta1.js +++ b/samples/analyze.v1p2beta1.js @@ -312,25 +312,25 @@ async function main() { `video-text-gcs `, `Analyzes text in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeTextGCS(opts.gcsUri) + opts => analyzeTextGCS(opts.gcsUri) ) .command( `track-objects-gcs `, `Analyzes objects in a video stored in Google Cloud Storage using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeObjectTrackingGCS(opts.gcsUri) + opts => analyzeObjectTrackingGCS(opts.gcsUri) ) .command( `video-text `, `Analyzes text in a video stored in a local file using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeText(opts.path) + opts => analyzeText(opts.path) ) .command( `track-objects `, `Analyzes objects in a video stored in a local file using the Cloud Video Intelligence API.`, {}, - async opts => await analyzeObjectTracking(opts.path) + opts => analyzeObjectTracking(opts.path) ) .example(`node $0 video-text ./resources/googlework_short.mp4`) .example( From f96d1fa59aa7b92eda0a01eb632de7d04042dd81 Mon Sep 17 00:00:00 2001 From: nareshqlogic <44403913+nareshqlogic@users.noreply.github.com> Date: Fri, 16 Nov 2018 01:38:47 +0530 Subject: [PATCH 3/6] refactor(samples): convert sample tests from ava to mocha (#146) --- samples/.eslintrc.yml | 2 + samples/package.json | 3 +- samples/system-test/analyze.test.js | 89 +++++++++---------- samples/system-test/analyze.v1p2beta1.test.js | 42 ++++----- samples/system-test/quickstart.test.js | 17 ++-- 5 files changed, 77 insertions(+), 76 deletions(-) diff --git a/samples/.eslintrc.yml b/samples/.eslintrc.yml index 282535f5..f9605165 100644 --- a/samples/.eslintrc.yml +++ b/samples/.eslintrc.yml @@ -1,3 +1,5 @@ --- +env: + mocha: true rules: no-console: off diff --git a/samples/package.json b/samples/package.json index cd195763..fe00cfcc 100644 --- a/samples/package.json +++ b/samples/package.json @@ -13,8 +13,7 @@ "node": ">=8" }, "scripts": { - "cover": "nyc --reporter=lcov --cache ava -T 20s --verbose test/*.test.js ./system-test/*.test.js && nyc report", - "test": "ava -T 10m --serial --verbose system-test/*.test.js" + "test": "mocha system-test/*.test.js --timeout=600000" }, "dependencies": { "@google-cloud/video-intelligence": "^1.5.0", diff --git a/samples/system-test/analyze.test.js b/samples/system-test/analyze.test.js index 4c33b7d1..de974a0d 100644 --- a/samples/system-test/analyze.test.js +++ b/samples/system-test/analyze.test.js @@ -1,13 +1,13 @@ /** * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the `License`); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an `AS IS` BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -17,71 +17,68 @@ 'use strict'; -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const path = require('path'); +const assert = require('assert'); +const tools = require('@google-cloud/nodejs-repo-tools'); -const cmd = `node analyze.js`; -const cwd = path.join(__dirname, `..`); +const cmd = 'node analyze.js'; +const cwd = path.join(__dirname, '..'); -const url = `gs://nodejs-docs-samples-video/quickstart.mp4`; -const shortUrl = `gs://nodejs-docs-samples-video/quickstart_short.mp4`; -const file = `resources/cat.mp4`; +const url = 'gs://nodejs-docs-samples-video/quickstart.mp4'; +const shortUrl = 'gs://nodejs-docs-samples-video/quickstart_short.mp4'; +const file = 'resources/cat.mp4'; // analyze_labels_gcs (one scene) -test.serial(`should analyze labels in a GCS file with one scene`, async t => { +it('should analyze labels in a GCS file with one scene', async () => { const output = await tools.runAsync(`${cmd} labels-gcs ${shortUrl}`, cwd); - t.regex(output, /Label shirt occurs at:/); - t.regex(output, /Confidence: \d+\.\d+/); + assert.strictEqual(new RegExp(/Label shirt occurs at:/).test(output), true); + assert.strictEqual(new RegExp(/Confidence: \d+\.\d+/).test(output), true); }); // analyze_labels_gcs (multiple scenes) -test.serial( - `should analyze labels in a GCS file with multiple scenes`, - async t => { - const output = await tools.runAsync(`${cmd} labels-gcs ${url}`, cwd); - t.regex(output, /Label shirt occurs at:/); - t.regex(output, /Confidence: \d+\.\d+/); - } -); +it('should analyze labels in a GCS file with multiple scenes', async () => { + const output = await tools.runAsync(`${cmd} labels-gcs ${url}`, cwd); + assert.strictEqual(new RegExp(/Label shirt occurs at:/).test(output), true); + assert.strictEqual(new RegExp(/Confidence: \d+\.\d+/).test(output), true); +}); // analyze_labels_local -test.serial(`should analyze labels in a local file`, async t => { +it('should analyze labels in a local file', async () => { const output = await tools.runAsync(`${cmd} labels-file ${file}`, cwd); - t.regex(output, /Label whiskers occurs at:/); - t.regex(output, /Confidence: \d+\.\d+/); + assert.strictEqual( + new RegExp(/Label whiskers occurs at:/).test(output), + true + ); + assert.strictEqual(new RegExp(/Confidence: \d+\.\d+/).test(output), true); }); // analyze_shots (multiple shots) -test.serial( - `should analyze shots in a GCS file with multiple shots`, - async t => { - const output = await tools.runAsync(`${cmd} shots ${url}`, cwd); - t.regex(output, /Scene 0 occurs from:/); - } -); +it('should analyze shots in a GCS file with multiple shots', async () => { + const output = await tools.runAsync(`${cmd} shots ${url}`, cwd); + assert.strictEqual(new RegExp(/Scene 0 occurs from:/).test(output), true); +}); // analyze_shots (one shot) -test.serial(`should analyze shots in a GCS file with one shot`, async t => { +it('should analyze shots in a GCS file with one shot', async () => { const output = await tools.runAsync(`${cmd} shots ${shortUrl}`, cwd); - t.regex(output, /The entire video is one shot./); + assert.strictEqual( + new RegExp(/The entire video is one shot./).test(output), + true + ); }); // analyze_safe_search -test.serial(`should analyze safe search results in a GCS file`, async t => { +it('should analyze safe search results in a GCS file', async () => { const output = await tools.runAsync(`${cmd} safe-search ${url}`, cwd); - t.regex(output, /Time: \d+\.\d+s/); - t.regex(output, /Explicit annotation results:/); + assert.strictEqual(new RegExp(/Time: \d+\.\d+s/).test(output), true); + assert.strictEqual( + new RegExp(/Explicit annotation results:/).test(output), + true + ); }); // analyze_video_transcription -test.serial( - `should analyze video transcription results in a GCS file`, - async t => { - const output = await tools.runAsync( - `${cmd} transcription ${shortUrl}`, - cwd - ); - t.regex(output, /over the pass/); - } -); +it('should analyze video transcription results in a GCS file', async () => { + const output = await tools.runAsync(`${cmd} transcription ${shortUrl}`, cwd); + assert.strictEqual(new RegExp(/over the pass/).test(output), true); +}); diff --git a/samples/system-test/analyze.v1p2beta1.test.js b/samples/system-test/analyze.v1p2beta1.test.js index f8129fec..f011f7d9 100644 --- a/samples/system-test/analyze.v1p2beta1.test.js +++ b/samples/system-test/analyze.v1p2beta1.test.js @@ -1,13 +1,13 @@ /** * Copyright 2018, Google, LLC - * Licensed under the Apache License, Version 2.0 (the `License`); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an `AS IS` BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -17,37 +17,37 @@ 'use strict'; -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const path = require('path'); +const assert = require('assert'); +const tools = require('@google-cloud/nodejs-repo-tools'); -const cmd = `node analyze.v1p2beta1.js`; -const cwd = path.join(__dirname, `..`); +const cmd = 'node analyze.v1p2beta1.js'; +const cwd = path.join(__dirname, '..'); -const shortUrl = `gs://nodejs-docs-samples/video/googlework_short.mp4`; -const url = `gs://nodejs-docs-samples/video/cat.mp4`; -const file1 = `resources/cat.mp4`; -const file2 = `resources/googlework_short.mp4`; +const shortUrl = 'gs://nodejs-docs-samples/video/googlework_short.mp4'; +const url = 'gs://nodejs-docs-samples/video/cat.mp4'; +const file1 = 'resources/cat.mp4'; +const file2 = 'resources/googlework_short.mp4'; const possibleTexts = /Google|GOOGLE|SUR|OMAR|ROTO|Vice President|58oo9|LONDRES|PARIS|METRO|RUE|CARLO/; -test.serial(`should detect text in a GCS file`, async t => { +it('should detect text in a GCS file', async () => { const output = await tools.runAsync(`${cmd} video-text-gcs ${shortUrl}`, cwd); - t.regex(output, possibleTexts); + assert.strictEqual(new RegExp(possibleTexts).test(output), true); }); -test.serial(`should detect text in a local file`, async t => { +it('should detect text in a local file', async () => { const output = await tools.runAsync(`${cmd} video-text ${file2}`, cwd); - t.regex(output, possibleTexts); + assert.strictEqual(new RegExp(possibleTexts).test(output), true); }); -test.serial(`should track objects in a GCS file`, async t => { +it('should track objects in a GCS file', async () => { const output = await tools.runAsync(`${cmd} track-objects-gcs ${url}`, cwd); - t.regex(output, /cat/); - t.regex(output, /Confidence: \d+\.\d+/); + assert.strictEqual(new RegExp(/cat/).test(output), true); + assert.strictEqual(new RegExp(/Confidence: \d+\.\d+/).test(output), true); }); -test.serial(`should track objects in a local file`, async t => { +it('should track objects in a local file', async () => { const output = await tools.runAsync(`${cmd} track-objects ${file1}`, cwd); - t.regex(output, /cat/); - t.regex(output, /Confidence: \d+\.\d+/); + assert.strictEqual(new RegExp(/cat/).test(output), true); + assert.strictEqual(new RegExp(/Confidence: \d+\.\d+/).test(output), true); }); diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index befe0836..d5722e1e 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -15,14 +15,17 @@ 'use strict'; -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const path = require('path'); +const assert = require('assert'); +const tools = require('@google-cloud/nodejs-repo-tools'); -const cmd = `node quickstart.js`; -const cwd = path.join(__dirname, `..`); +const cmd = 'node quickstart.js'; +const cwd = path.join(__dirname, '..'); -test(`should analyze a hardcoded video`, async t => { +it('should analyze a hardcoded video', async () => { const output = await tools.runAsync(cmd, cwd); - t.regex(output, /Label standing occurs at:/); + assert.strictEqual( + new RegExp(/Label standing occurs at:/).test(output), + true + ); }); From 5ae85af872e5988456680a881e20fc345accf287 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" Date: Thu, 15 Nov 2018 12:09:06 -0800 Subject: [PATCH 4/6] fix(deps): update dependency google-gax to ^0.22.0 (#145) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 41586079..96fcf8e3 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "fix": "eslint --fix '**/*.js'" }, "dependencies": { - "google-gax": "^0.20.0", + "google-gax": "^0.22.0", "lodash.merge": "^4.6.1", "protobufjs": "^6.8.6" }, From 282dc4dab89232695e26bbf68622ea09237b9dd9 Mon Sep 17 00:00:00 2001 From: Yoshi Automation Bot <44816363+yoshi-automation@users.noreply.github.com> Date: Mon, 19 Nov 2018 09:18:40 -0800 Subject: [PATCH 5/6] chore: add a synth.metadata chore: add a synth.metadata --- synth.metadata | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 synth.metadata diff --git a/synth.metadata b/synth.metadata new file mode 100644 index 00000000..f26e69af --- /dev/null +++ b/synth.metadata @@ -0,0 +1,27 @@ +{ + "sources": [ + { + "git": { + "name": "googleapis", + "remote": "https://github.com/googleapis/googleapis.git", + "sha": "5a57f0c13a358b2b15452bf2d67453774a5f6d4f", + "internalRef": "221837528" + } + }, + { + "git": { + "name": "googleapis-private", + "remote": "https://github.com/googleapis/googleapis-private.git", + "sha": "6aa8e1a447bb8d0367150356a28cb4d3f2332641", + "internalRef": "221340946" + } + }, + { + "generator": { + "name": "artman", + "version": "0.16.0", + "dockerImage": "googleapis/artman@sha256:90f9d15e9bad675aeecd586725bce48f5667ffe7d5fc4d1e96d51ff34304815b" + } + } + ] +} \ No newline at end of file From 3a005a4e4c936acc2d94de00f030d3898f912fe3 Mon Sep 17 00:00:00 2001 From: Praveen Kumar Singh Date: Wed, 21 Nov 2018 13:47:27 +0530 Subject: [PATCH 6/6] error fix for sample test --- samples/analyze.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/analyze.js b/samples/analyze.js index c3412687..237bce64 100644 --- a/samples/analyze.js +++ b/samples/analyze.js @@ -286,7 +286,7 @@ async function analyzeVideoTranscription(gcsUri) { const [operation] = await client.annotateVideo(request); console.log('Waiting for operation to complete...'); - const operationResult = await operation.promise(); + const [operationResult] = await operation.promise(); console.log('Word level information:'); const alternative = operationResult.annotationResults[0].speechTranscriptions[0]