Python+百度API实现语音识别的方法

1、语言录制

#file PyAudio_Record.py


import pyaudio
import wave

def wwav():
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
   #RATE = 44100
    RATE =16000
    RECORD_SECONDS = 5
    WAVE_OUTPUT_FILENAME = "E:\\test\\test.wav"

    p = pyaudio.PyAudio()

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

    print("* recording")

    frames = []

    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    print("* done recording")

    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()


2、语言识别

#file yuyinshibie.py
'''
利用百度API实现语言识别
'''
from aip import AipSpeech
import datetime
def shibie():
    """ 你的 APPID AK SK """
    APP_ID = '你的ID'
    API_KEY = '你的AK'
    SECRET_KEY = '你的SK'
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    file_handle = open('E:\\test\\test.wav', 'rb') 
    file_content = file_handle.read() 
    result=client.asr(file_content, 'pcm', 16000, {
        'dev_pid': '1536',
    })
    if result['err_no'] == 0: 
        print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " >> " + result['result'][0]) 
    else: 
        print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " >> " + "err_no:" + str(result['err_no'])) 


3、运行上述文件的程序:

#file main.py

import PyAudio_Record
import yuyinshibie as yysb
import time

while True:
    PyAudio_Record.wwav()
    time.sleep(1)
    yysb.shibie()
    time.sleep(1)


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值