百度语音转文字 (Python)

感受好久没写中文技术文章了。说实话,学东西都是基于英文,或者 别人从英文翻译成中文 咱们再捡二手货学习。因此用中文写技术文章怎么都感受是在骗人,怎么都以为很别扭编程

可是这一次的主角是百度。框架

虽然认真来说,全部编程语言、框架、核心技术都是外国人写的(开源),但彷佛你拼凑一下,仍能够贴上国产的标签(这个就至关于零件不是我作的,但我用它拼出了一件产品,产品是个人)。编程语言

并且又加上这个 API 是免费的,因此我能够介绍一下。(最近几年百度为了拿钱换名气,在海外仍是作了很多工做,好比创建了 Twitter、Github 帐户,成立了 AI研究室,开源了一些项目)学习

我这我的不喜欢讲废话:翻译

# Author: yingshaoxo
#### For baidu voice
from aip import AipSpeech

APP_ID = '15311704'
API_KEY = 'yTzBl40WBlhFOo1GnKk0YQTN'
SECRET_KEY = 'xpWedO1u0ZLATHijhetFo7dE5ibMsI6Q'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

def get_text(wav_bytes):
    result = client.asr(wav_bytes, 'wav', 16000, {'dev_pid': 1536,})
    try:
        text = result['result'][0]
    except Exception as e:
        print(e)
        text = ""
    return text


#### For real time voice recording
import speech_recognition as sr

r = sr.Recognizer()
mic = sr.Microphone()

while 1:
    print("\nPlease try to speak something...")
    with mic as source:
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source)
        audio_data = audio.get_wav_data(convert_rate=16000)
        print("\nGot you, now I'm trying to recognize that...")
        text = get_text(audio_data)
        print(f"\n{text}")