|
16 | 16 | * limitations under the License. |
17 | 17 | */ |
18 | 18 |
|
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; |
27 | 20 |
|
28 | 21 | // [START video_analyze_labels] |
29 | 22 | use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient; |
30 | 23 | use Google\Cloud\VideoIntelligence\V1\Feature; |
31 | 24 |
|
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(); |
38 | 33 |
|
39 | | -# Read the local video file |
40 | | -$inputContent = file_get_contents($path); |
| 34 | + # Read the local video file |
| 35 | + $inputContent = file_get_contents($path); |
41 | 36 |
|
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 | + ]); |
48 | 43 |
|
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 | + ]); |
51 | 48 |
|
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]; |
55 | 52 |
|
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 | + } |
61 | 67 | } |
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); |
72 | 69 |
|
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 | + } |
86 | 84 | } |
| 85 | + print(PHP_EOL); |
| 86 | + } else { |
| 87 | + print_r($operation->getError()); |
87 | 88 | } |
88 | | - print(PHP_EOL); |
89 | | -} else { |
90 | | - print_r($operation->getError()); |
91 | 89 | } |
92 | 90 | // [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); |
0 commit comments