Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 来自客户端微流的Google语音API:数据格式_Python_Socket.io_Google Speech Api_Flask Socketio - Fatal编程技术网

Python 来自客户端微流的Google语音API:数据格式

Python 来自客户端微流的Google语音API:数据格式,python,socket.io,google-speech-api,flask-socketio,Python,Socket.io,Google Speech Api,Flask Socketio,我正在尝试将其改编为Python/SocketIO(服务器端的Flask套接字IO)。 我的套接字正在工作,我能够传递采样率(并在服务器端检索)和音频数据 问题是我的音频数据格式不正确。我这样发: // Create a node that sends raw bytes across the websocket var scriptNode = context.createScriptProcessor(4096, 1, 1); // Need the maximum value for 1

我正在尝试将其改编为Python/SocketIO(服务器端的Flask套接字IO)。 我的套接字正在工作,我能够传递采样率(并在服务器端检索)和音频数据

问题是我的音频数据格式不正确。我这样发:

// Create a node that sends raw bytes across the websocket
var scriptNode = context.createScriptProcessor(4096, 1, 1);

// Need the maximum value for 16-bit signed samples, to convert from float.
const MAX_INT = Math.pow(2, 16 - 1) - 1;

scriptNode.addEventListener('audioprocess', function(e) {
  var floatSamples = e.inputBuffer.getChannelData(0);
  // The samples are floats in range [-1, 1]. Convert to 16-bit signed
  // integer.
  socket.emit('audiodata', Int16Array.from(floatSamples.map(function(n) {
    return n * MAX_INT;
})));
这是codeLabs中的代码,但我使用的是
socket.emit
而不是
socket.send

处理并发送到语音API的python代码如下:

credentials = service_account.Credentials.from_service_account_file(CONFIG[u"service_account"])
client = speech.SpeechClient(credentials=credentials)
config = types.RecognitionConfig(
  encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
  sample_rate_hertz=44100,
  language_code=u"fr"
)
streaming_config = types.StreamingRecognitionConfig(
  config=config,
  interim_results=True,
  single_utterance=False)

@socketio.on(u'audiodata')
def handle_audio_data_msg(audio_data_msg):
    print u'Received raw audio.'
    print audio_data_msg
    for response in client.streaming_recognize(streaming_config, [audio_data_msg]):
        print response
    emit(u'audiodatareceived', {u"AudioData": u"Acquired"})
我得到
TypeError:描述符“SerializeToString”需要一个“google.protobuf.pyext.\u message.CMessage”对象,但收到一个“dict”

所以我尝试在前端传递
Int16Array.from(…).buffer
,但我得到
TypeError:描述符“SerializeToString”需要一个“google.protobuf.pyext.\u message.CMessage”对象,但收到一个“str”

所以我不确定我应该如何传递数据。。。感谢您的帮助

[编辑] 我重新编写了我的处理程序,因为我认为我没有使用正确的类型,现在是这样的:

@socketio.on(u'audiodata')
def handle_audio_data_msg(audio_data_msg):
    print u'Received raw audio.'
    request = types.StreamingRecognizeRequest(audio_content=audio_data_msg)
    response = client.streaming_recognize(streaming_config, [request])
    for item in response:
        print u"error", item.get(u"error")
        print u"results", item.get(u"results")
        print u"resultIndex", item.get(u"resultIndex")
        print u"endpointerType", item.get(u"endpointerType")

    emit(u'audiodatareceived', {u"AudioData": u"Acquired"})
我没有收到任何错误,但我没有收到任何响应(空列表),因此没有打印任何内容

[edit2]:我意识到我输入的数据类型从来不是二进制数据,而是str。问题可能来自这里吗?我尝试如下实例化我的Flask Socket IO应用程序,但类型仍然是str.

socketio=socketio(app,binary=True)

能否添加完整的错误,包括堆栈跟踪?如果只是提供错误消息,则不清楚失败的原因。谢谢。你知道这一点吗?我不记得服务器端出了什么问题,但在前端,将
.buffer
添加到
Int16Array
是正确的做法