我需要将来自麦克风的音频从浏览器流式传输到后端服务器上基于Flask的应用程序,该应用程序需要将该流发送到Google Cloud API以转换为文本。我的项目在NodeJS中运行,但是无法在Python中运行。
步骤1:
在浏览器上,使用getUserMedia
启用麦克风访问。
步骤2: 在用户开始交谈时,会建立一个Websocket连接以将音频流发送到服务器(有效)。 ws不断将音频流传递到websocket服务器,该服务器调用python中的三个语音模块功能(见下文)
步骤3:
语音模块具有startSpeech
,endSpeech
和updateStream
功能。 (再次,这类似于我在NodeJS中所做的事情。)
语音模块如下:
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
# create client
client = speech.SpeechClient()
#variables
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US',
profanity_filter=False,
enable_word_time_offsets=False)
streaming_config = types.StreamingRecognitionConfig(config=config, interim_results=False)
responses = None
def startRecognitionStream(data):
print(data)
requests = types.StreamingRecognizeRequest(audio_content=data)
responses = client.streaming_recognize(streaming_config, requests)
for response in responses:
for result in response.results:
if result.is_final:
#socket.emit('speechData', result)
print(result)
stopRecognitionStream()
def stopRecognitionStream():
if responses:
responses.end()
responses = None
def writeStreamData(data):
if responses:
responses.write(data)
在上面的代码data
中是套接字在调用startRecognitionStream
和writeStreamData
函数时发送的流。我找不到如何将该流设置为requests
变量。出现的错误是:
message handler error
Traceback (most recent call last):
File "/Users/rr/Documents/GitHub/eda/edaenv/lib/python3.7/site-packages/google/protobuf/internal/python_message.py", line 526, in init
setattr(self, field_name, field_value)
File "/Users/rr/Documents/GitHub/eda/edaenv/lib/python3.7/site-packages/google/protobuf/internal/python_message.py", line 674, in setter
field_setter(self, new_value)
File "/Users/rr/Documents/GitHub/eda/edaenv/lib/python3.7/site-packages/google/protobuf/internal/python_message.py", line 662, in field_setter
new_value = type_checker.CheckValue(new_value)
File "/Users/rr/Documents/GitHub/eda/edaenv/lib/python3.7/site-packages/google/protobuf/internal/type_checkers.py", line 109, in CheckValue
raise TypeError(message)
TypeError: '' has type <class 'str'>, but expected one of: ((<class 'bytes'>,),)
感谢任何帮助或指导。我检查了Google提供的文档,但所有示例都显示了麦克风直接将数据发送到Python,这不是这种用例。谢谢