|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2019 Google Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +namespace Google\Cloud\Samples\Video; |
| 19 | + |
| 20 | +// [START video_analyze_text_detection_file] |
| 21 | +use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient; |
| 22 | +use Google\Cloud\VideoIntelligence\V1\Feature; |
| 23 | + |
| 24 | +/** |
| 25 | + * Finds labels in the video. |
| 26 | + * |
| 27 | + * @param string $path File path to a video file to analyze. |
| 28 | + * @param array $options optional Array of options to pass to |
| 29 | + * OperationResponse::pollUntilComplete. This is useful |
| 30 | + * for increasing the "pollingIntervalSeconds" option. |
| 31 | + */ |
| 32 | +function analyze_text_detection_file($path, array $options = []) |
| 33 | +{ |
| 34 | + # Instantiate a client. |
| 35 | + $video = new VideoIntelligenceServiceClient(); |
| 36 | + |
| 37 | + # Read the local video file |
| 38 | + $inputContent = file_get_contents($path); |
| 39 | + |
| 40 | + # Execute a request. |
| 41 | + $operation = $video->annotateVideo([ |
| 42 | + 'inputContent' => $inputContent, |
| 43 | + 'features' => [Feature::TEXT_DETECTION] |
| 44 | + ]); |
| 45 | + |
| 46 | + # Wait for the request to complete. |
| 47 | + $operation->pollUntilComplete($options); |
| 48 | + |
| 49 | + # Print the results. |
| 50 | + if ($operation->operationSucceeded()) { |
| 51 | + $results = $operation->getResult()->getAnnotationResults()[0]; |
| 52 | + |
| 53 | + # Process video/segment level label annotations |
| 54 | + foreach ($results->getTextAnnotations() as $text) { |
| 55 | + printf('Video text description: %s' . PHP_EOL, $text->getText()); |
| 56 | + foreach ($text->getSegments() as $segment) { |
| 57 | + $startTimeOffset = $segment->getSegment()->getStartTimeOffset(); |
| 58 | + $startSeconds = $startTimeOffset->getSeconds(); |
| 59 | + $startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00; |
| 60 | + $startTime = $startSeconds + $startNanoseconds; |
| 61 | + $endTimeOffset = $segment->getSegment()->getEndTimeOffset(); |
| 62 | + $endSeconds = $endTimeOffset->getSeconds(); |
| 63 | + $endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00; |
| 64 | + $endTime = $endSeconds + $endNanoseconds; |
| 65 | + printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime); |
| 66 | + printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence()); |
| 67 | + } |
| 68 | + } |
| 69 | + print(PHP_EOL); |
| 70 | + } else { |
| 71 | + print_r($operation->getError()); |
| 72 | + } |
| 73 | +} |
| 74 | +// [END video_analyze_text_detection_file] |
0 commit comments