Skip to content

Commit 37a945c

Browse files
authored
chore: upgrade video to new sample format (GoogleCloudPlatform#1639)
1 parent 0616c3e commit 37a945c

10 files changed

+449
-431
lines changed

video/src/analyze_explicit_content.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,48 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/video/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) < 2 || count($argv) > 3) {
29-
return print("Usage: php analyze_explicit_content.php URI\n");
30-
}
31-
list($_, $uri) = $argv;
32-
$options = isset($argv[2]) ? ['pollingIntervalSeconds' => $argv[2]] : [];
25+
namespace Google\Cloud\Samples\VideoIntelligence;
3326

3427
// [START video_analyze_explicit_content]
3528
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
3629
use Google\Cloud\VideoIntelligence\V1\Feature;
3730
use Google\Cloud\VideoIntelligence\V1\Likelihood;
3831

39-
/** Uncomment and populate these variables in your code */
40-
// $uri = 'The cloud storage object to analyze (gs://your-bucket-name/your-object-name)';
41-
// $options = []; // Optional, can be used to increate "pollingIntervalSeconds"
42-
43-
$video = new VideoIntelligenceServiceClient();
32+
/**
33+
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
34+
* @param int $pollingIntervalSeconds
35+
*/
36+
function analyze_explicit_content(string $uri, int $pollingIntervalSeconds = 0)
37+
{
38+
$video = new VideoIntelligenceServiceClient();
4439

45-
# Execute a request.
46-
$features = [Feature::EXPLICIT_CONTENT_DETECTION];
47-
$operation = $video->annotateVideo([
48-
'inputUri' => $uri,
49-
'features' => $features,
50-
]);
40+
# Execute a request.
41+
$features = [Feature::EXPLICIT_CONTENT_DETECTION];
42+
$operation = $video->annotateVideo([
43+
'inputUri' => $uri,
44+
'features' => $features,
45+
]);
5146

52-
# Wait for the request to complete.
53-
$operation->pollUntilComplete($options);
47+
# Wait for the request to complete.
48+
$operation->pollUntilComplete([
49+
'pollingIntervalSeconds' => $pollingIntervalSeconds
50+
]);
5451

55-
# Print the result.
56-
if ($operation->operationSucceeded()) {
57-
$results = $operation->getResult()->getAnnotationResults()[0];
58-
$explicitAnnotation = $results->getExplicitAnnotation();
59-
foreach ($explicitAnnotation->getFrames() as $frame) {
60-
$time = $frame->getTimeOffset();
61-
printf('At %ss:' . PHP_EOL, $time->getSeconds() + $time->getNanos() / 1000000000.0);
62-
printf(' pornography: ' . Likelihood::name($frame->getPornographyLikelihood()) . PHP_EOL);
52+
# Print the result.
53+
if ($operation->operationSucceeded()) {
54+
$results = $operation->getResult()->getAnnotationResults()[0];
55+
$explicitAnnotation = $results->getExplicitAnnotation();
56+
foreach ($explicitAnnotation->getFrames() as $frame) {
57+
$time = $frame->getTimeOffset();
58+
printf('At %ss:' . PHP_EOL, $time->getSeconds() + $time->getNanos() / 1000000000.0);
59+
printf(' pornography: ' . Likelihood::name($frame->getPornographyLikelihood()) . PHP_EOL);
60+
}
61+
} else {
62+
print_r($operation->getError());
6363
}
64-
} else {
65-
print_r($operation->getError());
6664
}
6765
// [END video_analyze_explicit_content]
66+
67+
// The following 2 lines are only needed to run the samples
68+
require_once __DIR__ . '/../../testing/sample_helpers.php';
69+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

video/src/analyze_labels_file.php

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,77 +16,79 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if (count($argv) < 2 || count($argv) > 3) {
23-
return print("Usage: php analyze_labels_file.php PATH\n");
24-
}
25-
list($_, $path) = $argv;
26-
$options = isset($argv[2]) ? ['pollingIntervalSeconds' => $argv[2]] : [];
19+
namespace Google\Cloud\Samples\VideoIntelligence;
2720

2821
// [START video_analyze_labels]
2922
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
3023
use Google\Cloud\VideoIntelligence\V1\Feature;
3124

32-
/** Uncomment and populate these variables in your code */
33-
// $path = 'File path to a video file to analyze';
34-
// $options = [];
35-
36-
# Instantiate a client.
37-
$video = new VideoIntelligenceServiceClient();
25+
/**
26+
* @param string $path File path to a video file to analyze
27+
* @param int $pollingIntervalSeconds
28+
*/
29+
function analyze_labels_file(string $path, int $pollingIntervalSeconds = 0)
30+
{
31+
# Instantiate a client.
32+
$video = new VideoIntelligenceServiceClient();
3833

39-
# Read the local video file
40-
$inputContent = file_get_contents($path);
34+
# Read the local video file
35+
$inputContent = file_get_contents($path);
4136

42-
# Execute a request.
43-
$features = [Feature::LABEL_DETECTION];
44-
$operation = $video->annotateVideo([
45-
'inputContent' => $inputContent,
46-
'features' => $features,
47-
]);
37+
# Execute a request.
38+
$features = [Feature::LABEL_DETECTION];
39+
$operation = $video->annotateVideo([
40+
'inputContent' => $inputContent,
41+
'features' => $features,
42+
]);
4843

49-
# Wait for the request to complete.
50-
$operation->pollUntilComplete($options);
44+
# Wait for the request to complete.
45+
$operation->pollUntilComplete([
46+
'pollingIntervalSeconds' => $pollingIntervalSeconds
47+
]);
5148

52-
# Print the results.
53-
if ($operation->operationSucceeded()) {
54-
$results = $operation->getResult()->getAnnotationResults()[0];
49+
# Print the results.
50+
if ($operation->operationSucceeded()) {
51+
$results = $operation->getResult()->getAnnotationResults()[0];
5552

56-
# Process video/segment level label annotations
57-
foreach ($results->getSegmentLabelAnnotations() as $label) {
58-
printf('Video label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
59-
foreach ($label->getCategoryEntities() as $categoryEntity) {
60-
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
53+
# Process video/segment level label annotations
54+
foreach ($results->getSegmentLabelAnnotations() as $label) {
55+
printf('Video label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
56+
foreach ($label->getCategoryEntities() as $categoryEntity) {
57+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
58+
}
59+
foreach ($label->getSegments() as $segment) {
60+
$start = $segment->getSegment()->getStartTimeOffset();
61+
$end = $segment->getSegment()->getEndTimeOffset();
62+
printf(' Segment: %ss to %ss' . PHP_EOL,
63+
$start->getSeconds() + $start->getNanos() / 1000000000.0,
64+
$end->getSeconds() + $end->getNanos() / 1000000000.0);
65+
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
66+
}
6167
}
62-
foreach ($label->getSegments() as $segment) {
63-
$start = $segment->getSegment()->getStartTimeOffset();
64-
$end = $segment->getSegment()->getEndTimeOffset();
65-
printf(' Segment: %ss to %ss' . PHP_EOL,
66-
$start->getSeconds() + $start->getNanos() / 1000000000.0,
67-
$end->getSeconds() + $end->getNanos() / 1000000000.0);
68-
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
69-
}
70-
}
71-
print(PHP_EOL);
68+
print(PHP_EOL);
7269

73-
# Process shot level label annotations
74-
foreach ($results->getShotLabelAnnotations() as $label) {
75-
printf('Shot label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
76-
foreach ($label->getCategoryEntities() as $categoryEntity) {
77-
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
78-
}
79-
foreach ($label->getSegments() as $shot) {
80-
$start = $shot->getSegment()->getStartTimeOffset();
81-
$end = $shot->getSegment()->getEndTimeOffset();
82-
printf(' Shot: %ss to %ss' . PHP_EOL,
83-
$start->getSeconds() + $start->getNanos() / 1000000000.0,
84-
$end->getSeconds() + $end->getNanos() / 1000000000.0);
85-
printf(' Confidence: %f' . PHP_EOL, $shot->getConfidence());
70+
# Process shot level label annotations
71+
foreach ($results->getShotLabelAnnotations() as $label) {
72+
printf('Shot label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
73+
foreach ($label->getCategoryEntities() as $categoryEntity) {
74+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
75+
}
76+
foreach ($label->getSegments() as $shot) {
77+
$start = $shot->getSegment()->getStartTimeOffset();
78+
$end = $shot->getSegment()->getEndTimeOffset();
79+
printf(' Shot: %ss to %ss' . PHP_EOL,
80+
$start->getSeconds() + $start->getNanos() / 1000000000.0,
81+
$end->getSeconds() + $end->getNanos() / 1000000000.0);
82+
printf(' Confidence: %f' . PHP_EOL, $shot->getConfidence());
83+
}
8684
}
85+
print(PHP_EOL);
86+
} else {
87+
print_r($operation->getError());
8788
}
88-
print(PHP_EOL);
89-
} else {
90-
print_r($operation->getError());
9189
}
9290
// [END video_analyze_labels]
91+
92+
// The following 2 lines are only needed to run the samples
93+
require_once __DIR__ . '/../../testing/sample_helpers.php';
94+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

video/src/analyze_labels_gcs.php

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,76 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if (count($argv) < 2 || count($argv) > 3) {
23-
return print("Usage: php analyze_labels.php URI\n");
24-
}
25-
list($_, $uri) = $argv;
26-
$options = isset($argv[2]) ? ['pollingIntervalSeconds' => $argv[2]] : [];
19+
namespace Google\Cloud\Samples\VideoIntelligence;
2720

2821
// [START video_analyze_labels_gcs]
2922
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
3023
use Google\Cloud\VideoIntelligence\V1\Feature;
3124

32-
/** Uncomment and populate these variables in your code */
33-
// $uri = 'The cloud storage object to analyze (gs://your-bucket-name/your-object-name)';
34-
// $options = [];
35-
36-
# Instantiate a client.
37-
$video = new VideoIntelligenceServiceClient();
25+
/**
26+
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
27+
* @param int $pollingIntervalSeconds
28+
*/
29+
function analyze_labels_gcs(string $uri, int $pollingIntervalSeconds = 0)
30+
{
31+
# Instantiate a client.
32+
$video = new VideoIntelligenceServiceClient();
3833

39-
# Execute a request.
40-
$features = [Feature::LABEL_DETECTION];
41-
$operation = $video->annotateVideo([
42-
'inputUri' => $uri,
43-
'features' => $features,
44-
]);
34+
# Execute a request.
35+
$features = [Feature::LABEL_DETECTION];
36+
$operation = $video->annotateVideo([
37+
'inputUri' => $uri,
38+
'features' => $features,
39+
]);
4540

46-
# Wait for the request to complete.
47-
$operation->pollUntilComplete($options);
41+
# Wait for the request to complete.
42+
$operation->pollUntilComplete([
43+
'pollingIntervalSeconds' => $pollingIntervalSeconds
44+
]);
4845

49-
# Print the results.
50-
if ($operation->operationSucceeded()) {
51-
$results = $operation->getResult()->getAnnotationResults()[0];
46+
# Print the results.
47+
if ($operation->operationSucceeded()) {
48+
$results = $operation->getResult()->getAnnotationResults()[0];
5249

53-
# Process video/segment level label annotations
54-
foreach ($results->getSegmentLabelAnnotations() as $label) {
55-
printf('Video label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
56-
foreach ($label->getCategoryEntities() as $categoryEntity) {
57-
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
50+
# Process video/segment level label annotations
51+
foreach ($results->getSegmentLabelAnnotations() as $label) {
52+
printf('Video label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
53+
foreach ($label->getCategoryEntities() as $categoryEntity) {
54+
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
55+
}
56+
foreach ($label->getSegments() as $segment) {
57+
$start = $segment->getSegment()->getStartTimeOffset();
58+
$end = $segment->getSegment()->getEndTimeOffset();
59+
printf(' Segment: %ss to %ss' . PHP_EOL,
60+
$start->getSeconds() + $start->getNanos() / 1000000000.0,
61+
$end->getSeconds() + $end->getNanos() / 1000000000.0);
62+
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
63+
}
5864
}
59-
foreach ($label->getSegments() as $segment) {
60-
$start = $segment->getSegment()->getStartTimeOffset();
61-
$end = $segment->getSegment()->getEndTimeOffset();
62-
printf(' Segment: %ss to %ss' . PHP_EOL,
63-
$start->getSeconds() + $start->getNanos() / 1000000000.0,
64-
$end->getSeconds() + $end->getNanos() / 1000000000.0);
65-
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
66-
}
67-
}
68-
print(PHP_EOL);
65+
print(PHP_EOL);
6966

70-
# Process shot level label annotations
71-
foreach ($results->getShotLabelAnnotations() as $label) {
72-
printf('Shot label description: %s' . PHP_EOL, $label->getEntity()->getDescription());
73-
foreach ($label->getCategoryEntities() as $categoryEntity) {
74-
printf(' Category: %s' . PHP_EOL, $categoryEntity->getDescription());
75-
}
76-
foreach ($label->getSegments() as $shot) {
77-
$start = $shot->getSegment()->getStartTimeOffset();
78-
$end = $shot->getSegment()->getEndTimeOffset();
79-
printf(' Shot: %ss to %ss' . PHP_EOL,
80-
$start->getSeconds() + $start->getNanos() / 1000000000.0,
81-
$end->getSeconds() + $end->getNanos() / 1000000000.0);
82-
printf(' Confidence: %f' . PHP_EOL, $shot->getConfidence());
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+
$start = $shot->getSegment()->getStartTimeOffset();
75+
$end = $shot->getSegment()->getEndTimeOffset();
76+
printf(' Shot: %ss to %ss' . PHP_EOL,
77+
$start->getSeconds() + $start->getNanos() / 1000000000.0,
78+
$end->getSeconds() + $end->getNanos() / 1000000000.0);
79+
printf(' Confidence: %f' . PHP_EOL, $shot->getConfidence());
80+
}
8381
}
82+
print(PHP_EOL);
83+
} else {
84+
print_r($operation->getError());
8485
}
85-
print(PHP_EOL);
86-
} else {
87-
print_r($operation->getError());
8886
}
8987
// [END video_analyze_labels_gcs]
88+
89+
// The following 2 lines are only needed to run the samples
90+
require_once __DIR__ . '/../../testing/sample_helpers.php';
91+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)