|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2018 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_speech_transcription_gcs] |
| 21 | +use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient; |
| 22 | +use Google\Cloud\VideoIntelligence\V1\Feature; |
| 23 | +use Google\Cloud\VideoIntelligence\V1\VideoContext; |
| 24 | +use Google\Cloud\VideoIntelligence\V1\SpeechTranscriptionConfig; |
| 25 | + |
| 26 | +/** |
| 27 | + * Transcribe speech from a video stored on GCS. |
| 28 | + * |
| 29 | + * @param string $path The cloud storage object to analyze. |
| 30 | + */ |
| 31 | +function analyze_transcription($uri, array $options = []) |
| 32 | +{ |
| 33 | + # set configs |
| 34 | + $features = [Feature::SPEECH_TRANSCRIPTION]; |
| 35 | + $speechTranscriptionConfig = (new SpeechTranscriptionConfig()) |
| 36 | + ->setLanguageCode('en-US') |
| 37 | + ->setEnableAutomaticPunctuation(true); |
| 38 | + $videoContext = (new VideoContext()) |
| 39 | + ->setSpeechTranscriptionConfig($speechTranscriptionConfig); |
| 40 | + |
| 41 | + # instantiate a client |
| 42 | + $client = new VideoIntelligenceServiceClient(); |
| 43 | + |
| 44 | + # execute a request. |
| 45 | + $operation = $client->annotateVideo([ |
| 46 | + 'inputUri' => $uri, |
| 47 | + 'features' => $features, |
| 48 | + 'videoContext' => $videoContext |
| 49 | + ]); |
| 50 | + |
| 51 | + print('Processing video for speech transcription...' . PHP_EOL); |
| 52 | + # Wait for the request to complete. |
| 53 | + $operation->pollUntilComplete($options); |
| 54 | + |
| 55 | + # Print the result. |
| 56 | + if ($operation->operationSucceeded()) { |
| 57 | + $result = $operation->getResult(); |
| 58 | + # there is only one annotation_result since only |
| 59 | + # one video is processed. |
| 60 | + $annotationResults = $result->getAnnotationResults()[0]; |
| 61 | + $speechTranscriptions = $annotationResults ->getSpeechTranscriptions(); |
| 62 | + |
| 63 | + foreach ($speechTranscriptions as $transcription) { |
| 64 | + # the number of alternatives for each transcription is limited by |
| 65 | + # $max_alternatives in SpeechTranscriptionConfig |
| 66 | + # each alternative is a different possible transcription |
| 67 | + # and has its own confidence score. |
| 68 | + foreach ($transcription->getAlternatives() as $alternative) { |
| 69 | + print('Alternative level information' . PHP_EOL); |
| 70 | + |
| 71 | + printf('Transcript: %s' . PHP_EOL, $alternative->getTranscript()); |
| 72 | + printf('Confidence: %s' . PHP_EOL, $alternative->getConfidence()); |
| 73 | + |
| 74 | + print('Word level information:'); |
| 75 | + foreach ($alternative->getWords() as $wordInfo) { |
| 76 | + printf('%s s - %s s: %s' . PHP_EOL, |
| 77 | + $wordInfo->getStartTime()->getSeconds(), |
| 78 | + $wordInfo->getEndTime()->getSeconds(), |
| 79 | + $wordInfo->getWord()); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + $client->close(); |
| 85 | +} |
| 86 | +// [END video_speech_transcription_gcs] |
0 commit comments