Skip to content

Commit 21aa1f9

Browse files
authored
Updating video samples for v1 (GoogleCloudPlatform#485)
* Updating video samples for v1 * Testing improvements, copyright year fixes, printing style change * fixes constant names
1 parent 0d1906e commit 21aa1f9

10 files changed

+279
-190
lines changed

video/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"name": "google/translate-sample",
2+
"name": "google/video-sample",
33
"type": "project",
44
"require": {
55
"symfony/console": "^3.1",
6-
"google/cloud-videointelligence": "^0.3.0"
6+
"google/cloud-videointelligence": "^0.7.0"
77
},
88
"autoload": {
99
"files": [
1010
"src/analyze_faces.php",
1111
"src/analyze_labels.php",
1212
"src/analyze_labels_file.php",
13-
"src/analyze_safe_search.php",
13+
"src/analyze_explicit_content.php",
1414
"src/analyze_shots.php"
1515
]
1616
},

video/composer.lock

Lines changed: 106 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

video/quickstart.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
22
/**
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2016 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -19,33 +19,42 @@
1919
require __DIR__ . '/vendor/autoload.php';
2020

2121
# [START videointelligence_quickstart]
22-
use Google\Cloud\VideoIntelligence\V1beta1\VideoIntelligenceServiceClient;
23-
use Google\Cloud\Videointelligence\V1beta1\Feature;
22+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
23+
use Google\Cloud\Videointelligence\V1\Feature;
2424

2525
# Instantiate a client.
2626
$video = new VideoIntelligenceServiceClient();
2727

2828
# Execute a request.
29-
$operation = $video->annotateVideo(
30-
'gs://demomaker/cat.mp4',
31-
[Feature::LABEL_DETECTION]
32-
);
29+
$options = ['inputUri'=>'gs://demomaker/cat.mp4', 'features'=>[Feature::LABEL_DETECTION]];
30+
$operation = $video->annotateVideo($options);
31+
3332
# Wait for the request to complete.
3433
$operation->pollUntilComplete();
3534

3635
# Print the result.
37-
if (!$operation->operationSucceeded()) {
38-
print_r($operation->getError());
39-
die;
40-
}
41-
42-
$results = $operation->getResult()->getAnnotationResults()[0];
43-
foreach ($results->getLabelAnnotations() as $label) {
44-
printf($label->getDescription() . PHP_EOL);
45-
foreach ($label->getLocations() as $location) {
46-
printf(' %ss to %ss' . PHP_EOL,
47-
$location->getSegment()->getStartTimeOffset() / 1000000,
48-
$location->getSegment()->getEndTimeOffset() / 1000000);
36+
if ($operation->operationSucceeded()) {
37+
$results = $operation->getResult()->getAnnotationResults()[0];
38+
# Process video/segment level label annotations
39+
foreach ($results->getSegmentLabelAnnotations() as $label) {
40+
printf('Video label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
41+
foreach ($label->getCategoryEntities() as $categoryEntity) {
42+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
43+
}
44+
foreach ($label->getSegments() as $segment) {
45+
$startTimeOffset = $segment->getSegment()->getStartTimeOffset();
46+
$startSeconds = $startTimeOffset->getSeconds();
47+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
48+
$startTime = $startSeconds + $startNanoseconds;
49+
$endTimeOffset = $segment->getSegment()->getEndTimeOffset();
50+
$endSeconds = $endTimeOffset->getSeconds();
51+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
52+
$endTime = $endSeconds + $endNanoseconds;
53+
printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime);
54+
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
55+
}
4956
}
57+
} else {
58+
print_r($operation->getError());
5059
}
5160
# [END videointelligence_quickstart]
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
22

33
/**
4-
* Copyright 2016 Google Inc.
4+
* Copyright 2017 Google Inc.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -17,25 +17,24 @@
1717
*/
1818
namespace Google\Cloud\Samples\Video;
1919

20-
// [START analyze_safe_search]
21-
use Google\Cloud\VideoIntelligence\V1beta1\VideoIntelligenceServiceClient;
22-
use Google\Cloud\Videointelligence\V1beta1\Feature;
20+
// [START analyze_explicit_content]
21+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\Videointelligence\V1\Feature;
2323

2424
/**
25-
* Analyze safe search in the video.
25+
* Analyze explicit content in the video.
2626
*
2727
* @param string $uri The cloud storage object to analyze. Must be formatted
2828
* like gs://bucketname/objectname
2929
*/
30-
function analyze_safe_search($uri)
30+
function analyze_explicit_content($uri)
3131
{
3232
# Instantiate a client.
3333
$video = new VideoIntelligenceServiceClient();
3434

3535
# Execute a request.
36-
$operation = $video->annotateVideo(
37-
$uri,
38-
[Feature::SAFE_SEARCH_DETECTION]);
36+
$options = ['inputUri'=>$uri, 'features'=>[Feature::EXPLICIT_CONTENT_DETECTION]];
37+
$operation = $video->annotateVideo($options);
3938

4039
# Wait for the request to complete.
4140
$operation->pollUntilComplete();
@@ -45,16 +44,17 @@ function analyze_safe_search($uri)
4544
$likelihoods = ['Unknown', 'Very unlikely', 'Unlikely', 'Possible',
4645
'Likely', 'Very likely'];
4746
$results = $operation->getResult()->getAnnotationResults()[0];
48-
foreach ($results->getSafeSearchAnnotations() as $safeSearch) {
49-
printf('At %ss:' . PHP_EOL, $safeSearch->getTimeOffset() / 1000000);
50-
print(' adult: ' . $likelihoods[$safeSearch->getAdult()] . PHP_EOL);
51-
print(' spoof: ' . $likelihoods[$safeSearch->getSpoof()] . PHP_EOL);
52-
print(' medical: ' . $likelihoods[$safeSearch->getMedical()] . PHP_EOL);
53-
print(' racy: ' . $likelihoods[$safeSearch->getRacy()] . PHP_EOL);
54-
print(' violent: ' . $likelihoods[$safeSearch->getViolent()] . PHP_EOL);
47+
$explicitAnnotation = $results->getExplicitAnnotation();
48+
foreach ($explicitAnnotation->getFrames() as $frame) {
49+
$timeOffset = $frame->getTimeOffset();
50+
$seconds = $timeOffset->getSeconds();
51+
$nanoseconds = floatval($timeOffset->getNanos())/1000000000.00;
52+
$time = $seconds + $nanoseconds;
53+
printf('At %ss:' . PHP_EOL, $time);
54+
printf(' pornography: ' . $likelihoods[$frame->getPornographyLikelihood()] . PHP_EOL);
5555
}
5656
} else {
5757
print_r($operation->getError());
5858
}
5959
}
60-
// [END analyze_safe_search]
60+
// [END analyze_explicit_content]

video/src/analyze_labels.php

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
namespace Google\Cloud\Samples\Video;
1919

2020
// [START analyze_labels]
21-
use Google\Cloud\VideoIntelligence\V1beta1\VideoIntelligenceServiceClient;
22-
use Google\Cloud\Videointelligence\V1beta1\Feature;
21+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\Videointelligence\V1\Feature;
2323

2424
/**
2525
* Finds labels in the video.
@@ -33,24 +33,57 @@ function analyze_labels($uri)
3333
$video = new VideoIntelligenceServiceClient();
3434

3535
# Execute a request.
36-
$operation = $video->annotateVideo(
37-
$uri,
38-
[Feature::LABEL_DETECTION]);
36+
$options = ['inputUri'=>$uri, 'features'=>[Feature::LABEL_DETECTION]];
37+
$operation = $video->annotateVideo($options);
3938

4039
# Wait for the request to complete.
4140
$operation->pollUntilComplete();
4241

43-
# Print the result.
42+
# Print the results.
4443
if ($operation->operationSucceeded()) {
4544
$results = $operation->getResult()->getAnnotationResults()[0];
46-
foreach ($results->getLabelAnnotations() as $label) {
47-
printf($label->getDescription() . PHP_EOL);
48-
foreach ($label->getLocations() as $location) {
49-
printf(' %ss to %ss' . PHP_EOL,
50-
$location->getSegment()->getStartTimeOffset() / 1000000,
51-
$location->getSegment()->getEndTimeOffset() / 1000000);
45+
46+
# Process video/segment level label annotations
47+
foreach ($results->getSegmentLabelAnnotations() as $label) {
48+
printf('Video label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
49+
foreach ($label->getCategoryEntities() as $categoryEntity) {
50+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
51+
}
52+
foreach ($label->getSegments() as $segment) {
53+
$startTimeOffset = $segment->getSegment()->getStartTimeOffset();
54+
$startSeconds = $startTimeOffset->getSeconds();
55+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
56+
$startTime = $startSeconds + $startNanoseconds;
57+
$endTimeOffset = $segment->getSegment()->getEndTimeOffset();
58+
$endSeconds = $endTimeOffset->getSeconds();
59+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
60+
$endTime = $endSeconds + $endNanoseconds;
61+
printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime);
62+
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
63+
}
64+
}
65+
print(PHP_EOL);
66+
67+
# Process shot level label annotations
68+
foreach ($results->getShotLabelAnnotations() as $label) {
69+
printf('Shot label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
70+
foreach ($label->getCategoryEntities() as $categoryEntity) {
71+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
72+
}
73+
foreach ($label->getSegments() as $shot) {
74+
$startTimeOffset = $shot->getSegment()->getStartTimeOffset();
75+
$startSeconds = $startTimeOffset->getSeconds();
76+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
77+
$startTime = $startSeconds + $startNanoseconds;
78+
$endTimeOffset = $shot->getSegment()->getEndTimeOffset();
79+
$endSecondseconds = $endTimeOffset->getSeconds();
80+
$endNanos = floatval($endTimeOffset->getNanos())/1000000000.00;
81+
$endTime = $endSeconds + $endNanoseconds;
82+
printf(' Shot: %ss to %ss' . PHP_EOL, $startTime, $endTime);
83+
printf(' Confidence: %f' . PHP_EOL, $shot->getConfidence());
5284
}
5385
}
86+
print(PHP_EOL);
5487
} else {
5588
print_r($operation->getError());
5689
}

video/src/analyze_labels_file.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
namespace Google\Cloud\Samples\Video;
1919

2020
// [START analyze_labels_file]
21-
use Google\Cloud\VideoIntelligence\V1beta1\VideoIntelligenceServiceClient;
22-
use Google\Cloud\Videointelligence\V1beta1\Feature;
21+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\Videointelligence\V1\Feature;
2323

2424
/**
2525
* Finds labels in the video.
@@ -31,31 +31,61 @@ function analyze_labels_file($path)
3131
# Instantiate a client.
3232
$video = new VideoIntelligenceServiceClient();
3333

34-
# Read the local video file and convert it to base64
35-
$inputContent = base64_encode(
36-
file_get_contents($path)
37-
);
34+
# Read the local video file
35+
$inputContent = file_get_contents($path);
3836

3937
# Execute a request.
40-
$operation = $video->annotateVideo(
41-
'',
42-
[Feature::LABEL_DETECTION],
43-
['inputContent' => $inputContent]);
38+
$options = ['inputContent'=>$inputContent, 'features'=>[Feature::LABEL_DETECTION]];
39+
$operation = $video->annotateVideo($options);
4440

4541
# Wait for the request to complete.
4642
$operation->pollUntilComplete();
4743

48-
# Print the result.
44+
# Print the results.
4945
if ($operation->operationSucceeded()) {
5046
$results = $operation->getResult()->getAnnotationResults()[0];
51-
foreach ($results->getLabelAnnotations() as $label) {
52-
printf($label->getDescription() . PHP_EOL);
53-
foreach ($label->getLocations() as $location) {
54-
printf(' %ss to %ss' . PHP_EOL,
55-
$location->getSegment()->getStartTimeOffset() / 1000000,
56-
$location->getSegment()->getEndTimeOffset() / 1000000);
47+
48+
# Process video/segment level label annotations
49+
foreach ($results->getSegmentLabelAnnotations() as $label) {
50+
printf('Video label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
51+
foreach ($label->getCategoryEntities() as $categoryEntity) {
52+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
53+
}
54+
foreach ($label->getSegments() as $segment) {
55+
$startTimeOffset = $segment->getSegment()->getStartTimeOffset();
56+
$startSeconds = $startTimeOffset->getSeconds();
57+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
58+
$startTime = $startSeconds + $startNanoseconds;
59+
$endTimeOffset = $segment->getSegment()->getEndTimeOffset();
60+
$endSeconds = $endTimeOffset->getSeconds();
61+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
62+
$endTime = $endSeconds + $endNanoseconds;
63+
printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime);
64+
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
65+
}
66+
}
67+
print(PHP_EOL);
68+
69+
# Process shot level label annotations
70+
foreach ($results->getShotLabelAnnotations() as $label) {
71+
printf('Shot label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
72+
foreach ($label->getCategoryEntities() as $categoryEntity) {
73+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
74+
}
75+
foreach ($label->getSegments() as $shot) {
76+
$startTimeOffset = $shot->getSegment()->getStartTimeOffset();
77+
$startSeconds = $startTimeOffset->getSeconds();
78+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
79+
$startTime = $startSeconds + $startNanoseconds;
80+
$endTimeOffset = $shot->getSegment()->getEndTimeOffset();
81+
$endSeconds = $endTimeOffset->getSeconds();
82+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
83+
$endTime = $endSeconds + $endNanoseconds;
84+
printf(' Shot: %ss to %ss' . PHP_EOL, $startTime, $endTime);
85+
printf(' Confidence: %f' . PHP_EOL, $shot->getConfidence());
5786
}
5887
}
88+
print(PHP_EOL);
5989
} else {
6090
print_r($operation->getError());
6191
}

video/src/analyze_shots.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
namespace Google\Cloud\Samples\Video;
1919

2020
// [START analyze_shots]
21-
use Google\Cloud\VideoIntelligence\V1beta1\VideoIntelligenceServiceClient;
22-
use Google\Cloud\Videointelligence\V1beta1\Feature;
21+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\Videointelligence\V1\Feature;
2323

2424
/**
2525
* Finds shot changes in the video.
@@ -33,9 +33,8 @@ function analyze_shots($uri)
3333
$video = new VideoIntelligenceServiceClient();
3434

3535
# Execute a request.
36-
$operation = $video->annotateVideo(
37-
$uri,
38-
[Feature::SHOT_CHANGE_DETECTION]);
36+
$options = ['inputUri'=>$uri, 'features'=>[Feature::SHOT_CHANGE_DETECTION]];
37+
$operation = $video->annotateVideo($options);
3938

4039
# Wait for the request to complete.
4140
$operation->pollUntilComplete();
@@ -44,9 +43,15 @@ function analyze_shots($uri)
4443
if ($operation->operationSucceeded()) {
4544
$results = $operation->getResult()->getAnnotationResults()[0];
4645
foreach ($results->getShotAnnotations() as $shot) {
47-
printf('%ss to %ss' . PHP_EOL,
48-
$shot->getStartTimeOffset() / 1000000,
49-
$shot->getEndTimeOffset() / 1000000);
46+
$startTimeOffset = $shot->getStartTimeOffset();
47+
$startSeconds = $startTimeOffset->getSeconds();
48+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
49+
$startTime = $startSeconds + $startNanoseconds;
50+
$endTimeOffset = $shot->getEndTimeOffset();
51+
$endSeconds = $endTimeOffset->getSeconds();
52+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
53+
$endTime = $endSeconds + $endNanoseconds;
54+
printf('Shot: %ss to %ss' . PHP_EOL, $startTime, $endTime);
5055
}
5156
} else {
5257
print_r($operation->getError());

video/test/quickstartTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
22
/**
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2016 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)