简体   繁体   中英

Set recording time for wit.ai speech API

I've been working with speech recognition in python 3.7.3. As a basic I've used the sample code using Wit.ai, which I have got online. My problem is, I'm not able to increase the recoding time. By default it is 4sec. When I change this to 10 sec or more I get the below error

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'

Appreciate for any help. Thank you.

My python code is as below:-

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. Use different API like the one from Google, they have a nice streaming version which provides fast response and can handle longer inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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