Azure-Samples / SpeechToText-REST

REST Samples of Speech To Text API
MIT License
22 stars 28 forks source link

Are there Python implementation of this? #10

Open Onotseike opened 6 years ago

Onotseike commented 6 years ago

Please is there a python implementation of STT similar to the TTS sample?

buaahsh commented 6 years ago

@Onotseike

Hi Onotseike, you try this python3 script. It works for me.

# -*- coding: utf-8 -*-

def read_in_chunks(file_object, blocksize=1024, chunks=-1):
    """Lazy function (generator) to read a file piece by piece. Default chunk size: 1k."""
    while chunks:
        data = file_object.read(blocksize)
        if not data:
            break
        yield data
        chunks -= 1

import requests

YOUR_SUBSCRIPTION_KEY = ''
audiofile = 'whatstheweatherlike.wav'

headers = {
    'Transfer-Encoding': 'chunked',
    'Ocp-Apim-Subscription-Key': YOUR_SUBSCRIPTION_KEY,
    'Content-type': 'audio/wav; codec=audio/pcm; samplerate=16000'
}

params = (
    ('language', 'en-us'),
    ('format', 'detailed'),
)

f =  open(audiofile, 'rb')
response = requests.post('https://speech.platform.bing.com/speech/recognition/dictation/cognitiveservices/v1', headers=headers, params=params, data=read_in_chunks(f))
print(response.content)