1
  1. 操作系统类型和版本:Windows 10, build 16199.1000

  2. Python版本和虚拟环境信息python --versionPython 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32

  3. 谷歌云蟒版本:google-cloud-speech==0.27.0

堆栈跟踪:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Python27\Lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Python27\Lib\threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "E:/Programming/Python/untitled1/main.py", line 109, in get_transcript
    print('. '.join(resp.alternative.transcript for resp in res), file=sys.stderr)
  File "E:/Programming/Python/untitled1/main.py", line 109, in <genexpr>
    print('. '.join(resp.alternative.transcript for resp in res), file=sys.stderr)
AttributeError: 'SpeechRecognitionResult' object has no attribute 'alternative'

重现步骤:

当我使用这个时:

alternatives = operation.result().results[0].alternatives
    for alternative in alternatives:
        print('Transcript: {}'.format(alternative.transcript))
        print('Confidence: {}'.format(alternative.confidence))

它按预期工作,但只打印第一个成绩单。当我使用这个时:

res = operation.result().results
print(res, file=sys.stderr)
print('. '.join(resp.alternative.transcript for resp in res), file=sys.stderr)

我得到了上面的例外。我也试过print('. '.join(resp.transcript for resp in res), file=sys.stderr)and print('. '.join(resp.alternative for resp in res), file=sys.stderr),就像打印调试一样。AttributeError两者都对任一属性抛出一个。

完整的工作示例:https ://gist.github.com/mxplusb/8f487a6ff3c781689799bb7ce1dec3f3 。它以正确的格式从视频文件中删除音频ffmpeg,将其上传到 GCS,然后执行异步语音到文本识别。我正在尝试将所有成绩单连接成一个大文本字符串。

4

1 回答 1

1

我认为你有一个轻微的错字,因为根据官方文档,该字段alternatives不是alternative

alternatives属性是一个包含对象的数组SpeechRecognitionAlternative,每个对象都有自己的transcript,在您的示例中,您正在遍历结果,但不是遍历每个替代项;相反,您假设只有一种选择,我认为这就是为什么您选择输入alternative而不是alternatives在整个过程中正确迭代的原因。

要解决此问题,只需更改您的resp.alternativetoresp.alternatives并正确迭代每个替代打印其成绩单。

于 2017-07-21T19:14:46.797 回答