0

我正在尝试使用 Python 实现 Microsoft Bing Speech REST API,并且我在网上找到了一些代码。

https://www.taygan.co/blog/2018/02/09/getting-started-with-speech-to-text

import json
import requests

    YOUR_API_KEY = 'ENTER_YOUR_KEY_HERE'
    YOUR_AUDIO_FILE = 'ENTER_PATH_TO_YOUR_AUDIO_FILE_HERE'
    MODE = 'interactive'
    LANG = 'en-US'
    FORMAT = 'simple'


    def handler():
        # 1. Get an Authorization Token
        token = get_token()
        # 2. Perform Speech Recognition
        results = get_text(token, YOUR_AUDIO_FILE)
        # 3. Print Results
        print(results)

    def get_token():
        # Return an Authorization Token by making a HTTP POST request to Cognitive Services with a valid API key.
        url = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken'
        headers = {
            'Ocp-Apim-Subscription-Key': YOUR_API_KEY
        }
        r = requests.post(url, headers=headers)
        token = r.content
        return(token)

    def get_text(token, audio):
        # Request that the Bing Speech API convert the audio to text
        url = 'https://speech.platform.bing.com/speech/recognition/{0}/cognitiveservices/v1?language={1}&format={2}'.format(MODE, LANG, FORMAT)
        headers = {
            'Accept': 'application/json',
            'Ocp-Apim-Subscription-Key': YOUR_API_KEY,
            'Transfer-Encoding': 'chunked',
            'Content-type': 'audio/wav; codec=audio/pcm; samplerate=16000',
            'Authorization': 'Bearer {0}'.format(token)
        }
        r = requests.post(url, headers=headers, data=stream_audio_file(audio))
        results = json.loads(r.content)
        return results

    def stream_audio_file(speech_file, chunk_size=1024):
        # Chunk audio file
        with open(speech_file, 'rb') as f:
            while 1:
                data = f.read(1024)
                if not data:
                    break
                yield data

    if __name__ == '__main__':
        handler()

我已经按照上面的代码(并更改了密钥和音频文件名),但不断得到这个:

Traceback (most recent call last):
  File "/Users/emi/Desktop/proj/taygan tutorial/handler.py", line 55, in <module>
    handler()
  File "/Users/emi/Desktop/proj/taygan tutorial/handler.py", line 15, in handler
    results = get_text(token, YOUR_AUDIO_FILE)
  File "/Users/emi/Desktop/proj/taygan tutorial/handler.py", line 40, in get_text
    results = json.loads(r.content)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

完全新手,所以真的不明白发生了什么。谢谢!

注意:我也尝试过这个 GitHub Repos中的代码, 但是我收到了很多错误。

4

0 回答 0