简体   繁体   English

设置wit.ai语音API的录制时间

[英]Set recording time for wit.ai speech API

I've been working with speech recognition in python 3.7.3. 我一直在使用python 3.7.3中的语音识别。 As a basic I've used the sample code using Wit.ai, which I have got online. 作为一个基础,我使用了Wit.ai的示例代码,我已经在线了。 My problem is, I'm not able to increase the recoding time. 我的问题是,我无法增加重新编码时间。 By default it is 4sec. 默认情况下为4秒。 When I change this to 10 sec or more I get the below error 当我将其更改为10秒或更长时,我得到以下错误

Traceback (most recent call last): File "C:/Users/xyx/Downloads/Desktop/python STT/main.py", line 41, in text = RecognizeSpeech('myspeech.wav', 15) File "C:/Users/xyx/Downloads/Desktop/python STT/main.py", line 34, in RecognizeSpeech text = data['_text'] KeyError: '_text' 回溯(最近一次调用最后一次):文件“C:/ Users / xyx / Downloads / Desktop / python STT / main.py”,第41行,文本= RecognizeSpeech('myspeech.wav',15)文件“C:/ Users / xyx / Downloads / Desktop / python STT / main.py“,第34行,在RecognizeSpeech text = data ['_ text'] KeyError:'_ text'

Appreciate for any help. 感谢任何帮助。 Thank you. 谢谢。

My python code is as below:- 我的python代码如下: -

import requests
import json
from Recorder import record_audio, read_audio

# Wit speech API endpoint
API_ENDPOINT = 'https://api.wit.ai/speech'

# Wit.ai api access token
wit_access_token = 'MY WIT.AI KEY'


def RecognizeSpeech(AUDIO_FILENAME, num_seconds = 5):

    # record audio of specified length in specified audio file
    record_audio(num_seconds, AUDIO_FILENAME)

    # reading audio
    audio = read_audio(AUDIO_FILENAME)

    # defining headers for HTTP request
    headers = {'authorization': 'Bearer ' + wit_access_token,
               'Content-Type': 'audio/wav'}

    # making an HTTP post request
    resp = requests.post(API_ENDPOINT, headers = headers,
                         data = audio)

    # converting response content to JSON format
    data = json.loads(resp.content)

    # get text from data
    text = data['_text']

    # return the text
    return text

if __name__ == "__main__":
    text =  RecognizeSpeech('myspeech.wav', 15)
    print("\nYou said: {}".format(text))

This is a limitation of the wit.ai API. 这是wit.ai API的限制。 Use different API like the one from Google, they have a nice streaming version which provides fast response and can handle longer inputs. 使用与谷歌不同的API,他们有一个很好的流媒体版本,提供快速响应,可以处理更长的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM