【问题标题】:Google cloud transcription API谷歌云转录 API
【发布时间】:2022-06-15 13:40:13
【问题描述】:

我想用说话者标签、转录、说话者持续时间的时间戳和置信度来计算每个说话者在双向对话呼叫中的持续时间。

例如:我有 2 个扬声器数量的客户服务支持的 mp3 文件。我想知道说话者的持续时间以及说话者标签、转录和转录的置信度。

我面临着结束时间和转录信心的问题。我越来越有信心,因为转录为 0,结束时间与实际结束时间不符。

音频链接:https://drive.google.com/file/d/1OhwQ-xI7Rd-iKNj_dKP2unNxQzMIYlNW/view?usp=sharing

  **strong text**
  #!pip install --upgrade google-cloud-speech
from google.cloud import speech_v1p1beta1 as speech

import datetime     

tag=1

speaker=""

transcript = ''

client = speech.SpeechClient.from_service_account_file('#cloud_credentials')


audio = speech.types.RecognitionAudio(uri=gs_uri)

config = speech.types.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US',
enable_speaker_diarization=True,
enable_automatic_punctuation=True,
enable_word_time_offsets=True,
diarization_speaker_count=2,
use_enhanced=True,
model='phone_call',
profanity_filter=False,
enable_word_confidence=True)

print('Waiting for operation to complete…')

operation = client.long_running_recognize(config=config, audio=audio)

response = operation.result(timeout=100000)

with open('output_file.txt', "w") as text_file:

    for result in response.results:
        alternative = result.alternatives[0]
            confidence = result.alternatives[0].confidence
            current_speaker_tag=-1
            transcript = ""
            time = 0
            for word in alternative.words:
                if word.speaker_tag != current_speaker_tag:
                   if (transcript != ""):
                      print(u"Speaker {} - {} - {} - {}".format(current_speaker_tag, str(datetime.timedelta(seconds=time)), transcript, confidence), file=text_file)
                   transcript = ""
                   current_speaker_tag = word.speaker_tag
                   time = word.start_time.seconds

                transcript = transcript + " " + word.word
     if transcript != "":
         print(u"Speaker {} - {} - {} - {}".format(current_speaker_tag, str(datetime.timedelta(seconds=time)), transcript, confidence), file=text_file)
     print(u"Speech to text operation is completed, output file is created: {}".format('output_file.txt'))

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: machine-learning artificial-intelligence speech-to-text google-speech-to-text-api google-cloud-translate


【解决方案1】:

您在问题中的代码和屏幕截图彼此不同。但是,从屏幕截图中可以理解,您正在使用语音到文本说话者分类方法创建单个说话者的语音。

您无法在此处计算每个发言者的不同置信度,因为 response 包含每个转录本和单个单词的 confidence 值。根据音频,单个转录本可能包含也可能不包含多个说话者的话。
同样根据document response 在最后一个结果列表中包含所有wordsspeaker_tag。来自文档

每个结果中的成绩单是独立的,每个结果是连续的。但是,替代项中的单词列表包括所有单词 从迄今为止的所有结果来看。因此,要使用扬声器获取所有单词 标签,你只需要从最后一个结果中取出单词列表。

对于最后一个结果列表的置信度为 0。您可以在控制台或任何文件中编写响应并自行调试。

# Detects speech in the audio file
operation = client.long_running_recognize(config=config, audio=audio)
response = operation.result(timeout=10000)
 
# check the whole response
with open('output_file.txt', "w") as text_file:
   print(response,file=text_file)

或者您也可以打印个人成绩单和信心以便更好地理解。例如:

#confidence for each transcript
for result in response.results:
   alternative = result.alternatives[0]
   print("Transcript: {}".format(alternative.transcript))
   print("Confidence: {}".format(alternative.confidence))

对于每个发言者的持续时间问题,您计算的是每个单词的开始时间和结束时间,而不是每个发言者。 这个想法应该是这样的:-

  1. 获取说话者第一个单词的开始时间作为时长开始时间。
  2. 始终将每个单词的结束时间设置为持续时间结束时间,因为我们不知道下一个单词是否有不同的说话者。
  3. 注意说话人的变化,如果说话人相同,则只需在修改后的成绩单中添加单词,否则执行相同操作并重置新说话人的开始时间。 例如:
tag=1
speaker=""
transcript = ''
start_time=""
end_time=""
 
for word_info in words_info:
   end_time = word_info.end_time.seconds   #tracking the end time of speech
   if start_time=='' :
       start_time = word_info.start_time.seconds #setting the value only for first time
   if word_info.speaker_tag==tag:
       speaker=speaker+" "+word_info.word
   else:
       transcript += "speaker {}: {}-{} - {}".format(tag,str(datetime.timedelta(seconds=start_time)),str(datetime.timedelta(seconds=end_time)),speaker) + '\n'
       tag=word_info.speaker_tag
       speaker=""+word_info.word
       start_time = word_info.start_time.seconds #resetting the starttime as we found a new speaker
 
transcript += "speaker {}: {}-{} - {}".format(tag,str(datetime.timedelta(seconds=start_time)),str(datetime.timedelta(seconds=end_time)),speaker) + '\n'


我删除了修改后的成绩单中的置信度部分,因为它始终为 0。另外请记住,Speaker diarization 仍处于 beta 开发阶段,您可能无法获得所需的确切输出。

【讨论】:

    猜你喜欢
    • 2018-07-26
    • 2018-07-08
    • 2018-03-23
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多