python百度语音实时识别成文字

python 实现百度AI语音实时识别成文字

百度AI接口https://console.bce.baidu.com/ai/?_=1559654571070&fromai=1#/ai/speech/app/list

安装:

pip install SpeechRecognition
pip install pyAudio
需要安装pyAudio,如果不能安装,可以去Python Extension Packages下载安装。

代码实现:

"""
实时语音识别测试
"""
import time
import speech_recognition as sr
import logging
logging.basicConfig(level=logging.DEBUG)
from aip import AipSpeech

# filename = './audio/test.wav'

BAIDU_APP_ID = 'xxx'
BAIDU_API_KEY = 'xxx'
BAIDU_SECRET_KEY = 'xxxx'
aip_speech = AipSpeech(BAIDU_APP_ID, BAIDU_API_KEY, BAIDU_SECRET_KEY)

r = sr.Recognizer()
# 麦克风
mic = sr.Microphone(sample_rate=16000)
while True:
    logging.info('录音中...')
    with mic as source:
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source)
    logging.info('录音结束,识别中...')

    start_time = time.time()
    print(type(audio))
    audio_data = audio.get_wav_data()
    print(type(audio_data))
    # 识别本地文件
    ret = aip_speech.asr(audio_data, 'wav', 16000, {'dev_pid': 1536, })
    print(ret)
    if ret and ret['err_no'] == 0:
        result = ret['result'][0]
        print(result)
        end_time = time.time()
        print(end_time - start_time)
    else:
        print(ret['err_msg'])
    logging.info('end')

 

 

你可能感兴趣的:(python基础)