Closed
Description
Example code to return all transcriptions
This entry provides an example for returning all speech transcriptions in a GCS stored video. The current example code for speech transcription only returns the first item:
[...]
# There is only one annotation_result since only
# one video is processed.
annotation_results = result.annotation_results[0]
speech_transcription = annotation_results.speech_transcriptions[0]
alternative = speech_transcription.alternatives[0]
print('Transcript: {}'.format(alternative.transcript))
[...]
In order to store and return all transcriptions (first alternative), here is a quick example for a loop:
def speech_transcription(path):
"""Transcribe speech from a video stored on GCS."""
video_client = videointelligence.VideoIntelligenceServiceClient()
features = [videointelligence.enums.Feature.SPEECH_TRANSCRIPTION]
config = videointelligence.types.SpeechTranscriptionConfig(
language_code='en-US',
enable_automatic_punctuation=True)
video_context = videointelligence.types.VideoContext(
speech_transcription_config=config)
operation = video_client.annotate_video(
path, features=features,
video_context=video_context)
result = operation.result()
# Make list, loop over entries, store each entry in list, return list.
all_transcriptions = []
entries = result.annotation_results[0].speech_transcriptions[0].alternatives
for i, entry in enumerate(entries):
all_transcriptions.append(entry.transcript)
return(all_transcriptions)
Hope this helps some of you!
Metadata
Metadata
Assignees
Labels
No labels
Activity
nnegrey commentedon Oct 24, 2018
Hi, thanks for sharing.
If you feel the sample should be updated, feel free to open a PR for review. :)
Since this isn't a explicit error with the sample or ask for help with the sample, I'm gonna close this. Feel free to re-open if needed.
Thanks