python调用百度AI语音识别

1、进入百度AI官网,注册账号和语音识别服务,创建语音识别应用

获取百度AI应用的AppID,API Key,Secret Key
在这里插入图片描述

2、python代码实现百度AI语音平台的调用

2.1、python库的安装

(1)、pyAudio库
该库因为需要c++的依赖,所以无法直接使用pip直接安装,需要我们下载其whlee文件安装
github地址:https://github.com/intxcc/pyaudio_portaudio/releases

详见此处:https://blog.csdn.net/qq_36387683/article/details/91960141

打开该文件目录:

pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl

(2)\wave,baidu-aip库的安装

详见此处:https://blog.csdn.net/alice_tl/article/details/97434261

pip install wave
pip install baidu-aip

2.2、代码调用

详见此处:https://blog.csdn.net/qq_42145185/article/details/101209531

# -*- coding: UTF-8 -*-
from aip import AipSpeech
import pyaudio
import wave

input_filename = "input.wav"  # 麦克风采集的语音输入
input_filepath = ""  # 输入s文件的path
in_path = input_filepath + input_filename

""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

'''语音识别部分'''


def Speech():
    def get_file_content(filePath):
        with open(filePath, "rb") as fp:
            return fp.read()

    keyword = client.asr(get_file_content('input.wav'), 'pcm', 16000, {'dev_ped': 1536})

    print(keyword['err_no'])
    print(keyword['err_msg'])
    print(keyword['result'][0])


'''语音采集部分'''


def get_audio(filepath):
    aa = str(input("是否开始录音?   (是/否)"))
    if aa == str("是"):
        CHUNK = 256
        FORMAT = pyaudio.paInt16
        CHANNELS = 1  # 声道数
        RATE = 11025  # 采样率
        RECORD_SECONDS = 10  # 采集时间(s)
        WAVE_OUTPUT_FILENAME = filepath  # 输出文件名和路径
        p = pyaudio.PyAudio()

        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK)

        print("*" * 10, "开始录音:请在10秒内输入语音")
        frames = []
        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)
        print("*" * 10, "录音结束\n")

        stream.stop_stream()
        stream.close()
        p.terminate()

        wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b''.join(frames))
        wf.close()
    elif aa == str("否"):
        exit()
    else:
        print("无效输入,请重新选择")
        get_audio(in_path)


if __name__ == '__main__':
    for i in range(1):
        get_audio(in_path)
        Speech()

在这里插入图片描述
识别结果很棒!!!!

部分觉得不错的文章:

扫描二维码关注公众号,回复: 9447860 查看本文章

https://www.jianshu.com/p/915db160504b

发布了7 篇原创文章 · 获赞 2 · 访问量 541

猜你喜欢

转载自blog.csdn.net/qq_41744697/article/details/104090421