最近,我试图创建一个程序,该程序可以将实时音频流转换为文本并在其中搜索关键字。然后,我希望它激活另一个监听功能。我当前的代码如下(没有第二个监听功能):
from __future__ import print_function
import json
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1
from watson_developer_cloud.websocket import RecognizeCallback, AudioSource
import threading
# If service instance provides API key authentication
# service = SpeechToTextV1(
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
url='https://stream.watsonplatform.net/speech-to-text/api',
# iam_apikey='your_apikey')
service = SpeechToTextV1(
username='MY USERNAME',
password='MY PASSWORD',
url='https://stream.watsonplatform.net/speech-to-text/api')
"""
models = service.list_models().get_result()
print(json.dumps(models, indent=2))
model = service.get_model('en-US_BroadbandModel').get_result()
print(json.dumps(model, indent=2))
with open(join(dirname(__file__), 'audio-file.flac'),
'rb') as audio_file:
print(json.dumps(
service.recognize(
audio=audio_file,
content_type='audio/flac',
timestamps=True,
word_confidence=True).get_result(),
indent=2))
`"""
# Example using websockets
class MyRecognizeCallback(RecognizeCallback):
def __init__(self):
RecognizeCallback.__init__(self)
def on_transcription(self, transcript):
print(transcript)
def on_connected(self):
print('Connection was successful')
def on_error(self, error):
print('Error received: {}'.format(error))
def on_inactivity_timeout(self, error):
print('Inactivity timeout: {}'.format(error))
def on_listening(self):
print('Service is listening')
def on_hypothesis(self, hypothesis):
print(hypothesis)
def on_data(self, data):
print(data)
# Example using threads in a non-blocking way
mycallback = MyRecognizeCallback()
audio_file = open(join(dirname(__file__), 'audio-file.flac'), 'rb')
audio_source = AudioSource(audio_file)
recognize_thread = threading.Thread(
target=service.recognize_using_websocket,
args=(audio_source, "audio/wav; rate=44100", mycallback))
recognize_thread.start()
此代码当前返回以下错误:
Error received: unable to transcode data stream audio/wav -> audio/x-float-array
Error received: 'NoneType' object has no attribute 'connected'
随附的是python返回的所有内容:
我当前正在使用python 3.4.2运行x64位Windows 10
编辑:
现在错误消息似乎已更改为以下内容:
Error received: unable to transcode data stream audio/wav -> audio/x-float-array
Error received: [WinError 10014] The system detected an invalid pointer address in attempting to use a pointer argument in a call
答案 0 :(得分:0)
编辑:您的脚本不起作用,因为您正在将音频/波形与音频/ flac混合在一起。它们是不同的格式,并且不能互换。
尽管我还没有测试过通过线程运行它,但是使用此代码仍然可以。
with open("audio-file.flac", "rb") as audio_file:
audio_source = AudioSource(audio_file)
print("Sending file to Watson...")
stt_auth.recognize_using_websocket(
audio=audio_source,
content_type="audio/flac",
recognize_callback=mycallback,
interim_results=True, # Print all attempts, including not final responses.
)
我会尝试使用文件名作为参数来设置线程。请注意,我自己尚未对此进行测试,但是应该可以。
def play_audiofile(name):
with open("audio-file.flac", "rb") as audio_file:
audio_source = AudioSource(audio_file)
print("Sending file to Watson...")
stt_auth.recognize_using_websocket(
audio=audio_source,
content_type="audio/flac",
recognize_callback=mycallback,
interim_results=True, # Print all attempts, including not final responses.
)
play_file_in_background = Thread(target=play_audiofile, args="audio-file.flac")
play_file_in_background.start()
while play_file_in_background.isAlive():
pass # Don't end until it has finished