百度云(https://cloud.baidu.com/)提供了许多人工智能的API接口,有一些是可以免费使用的。
环境配置
使用环境
windows7 x64
python3.7
vscode 编辑器,用于写代码
ffmpeg 用于将wav格式的音频文件转化成pcm格式,可在其官网(https://www.ffmpeg.org/)上下载
ffmpeg 下载后解压,不用安装,将bin中三个文件复制到system32目录下即可。
使用到的python包有aip 百度AI接口库 使用pip install baidu-aip安装
pyaudio python3.7使用pip安装会报错,可以下载whl文件(https://github.com/intxcc/pyaudio_portaudio/releases),离线安装pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl,pyaudio库的使用可参考https://people.csail.mit.edu/hubert/pyaudio/,上面有例子代码
wave 使用pip install wave安装
代码实现
工具函数
- #-*- coding:utf-8 -*-
- """音频处理"""
- import os
- import pyaudio
- import wave
-
- CHUNK = 1024
- FORMAT = pyaudio.paInt16
- CHANNELS = 2
- #RATE = 44100
- RATE = 16000
- RECORD_SECONDS = 5
-
-
- #用来播放声音,声音文件为wav格式
- def play_wav(filename):
- wf = wave.open(filename, 'rb')
-
- p = pyaudio.PyAudio()
-
- stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
- channels=wf.getnchannels(),
- rate=wf.getframerate(),
- output=True)
-
- data = wf.readframes(CHUNK)
-
-
- while len(data) != 0:
- stream.write(data)
- data = wf.readframes(CHUNK)
-
- stream.stop_stream()
- stream.close()
-
- p.terminate()
-
- #录音,将声音保存为wav格式
- def record(output_filename):
- p = pyaudio.PyAudio()
-
- stream = p.open(format=FORMAT,
- channels=CHANNELS,
- rate=RATE,
- input=True,
- frames_per_buffer=CHUNK)
-
- print("开始录音,最长为5s")
-
- frames = []
-
- over_flag = False
- for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
- data = stream.read(CHUNK)
- frames.append(data)
-
- print("录音结束")
-
- stream.stop_stream()
- stream.close()
- p.terminate()
-
- wf = wave.open(output_filename, 'wb')
- wf.setnchannels(CHANNELS)
- wf.setsampwidth(p.get_sample_size(FORMAT))
- wf.setframerate(RATE)
- wf.writeframes(b''.join(frames))
- wf.close()
-
- #调用ffmpeg将wav格式的音频文件转化成pcm格式,返回pcm格式文件名
- #可参考https://www.cnblogs.com/DragonFire/p/9212935.html
- #http://ai.baidu.com/docs#/ASR-Tool-convert/top
- def wav2pcm(wav_file_name):
- # 假设 wav_file = "音频文件.wav"
- # wav_file.split(".") 得到["音频文件","wav"] 拿出第一个结果"音频文件" 与 ".pcm" 拼接 等到结果 "音频文件.pcm"
- pcm_file = "%s.pcm" %(wav_file_name.split(".")[0])
-
- # 就是此前我们在cmd窗口中输入命令,这里面就是在让Python帮我们在cmd中执行命令
- os.system("ffmpeg -y -i %s -acodec pcm_s16le -f s16le -ac 1 -ar 16000 %s > ffmpeg.log"%(wav_file_name,pcm_file))
-
- return pcm_file
测试代码
- #-*- coding:utf-8 -*-
- from aip import AipSpeech
- import pyaudio
- import wave
- import sys
- from pyaudio_utils import play_wav,record,wav2pcm
-
- """ 你的 APPID AK SK """
- #如何获取这三个参数可参考官方文档
- APP_ID = 'xxxx'
- API_KEY = 'xxxx'
- SECRET_KEY = 'xxx'
-
- client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
-
- #语音合成
- #参考https://cloud.baidu.com/doc/SPEECH/TTS-Online-Python-SDK.html#.E8.AF.B7.E6.B1.82.E8.AF.B4.E6.98.8E
- def text2sound(msg):
- result = client.synthesis(msg, 'zh', 1, {
- 'vol': 5,'aue':6
- })
-
- # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
- if not isinstance(result, dict):
- with open('auido.wav', 'wb') as f:
- f.write(result)
- play_wav('auido.wav')
-
- #语音识别
- #参考https://cloud.baidu.com/doc/SPEECH/ASR-Online-Python-SDK.html#.E8.AF.B7.E6.B1.82.E8.AF.B4.E6.98.8E
- def sound2text(sound_filename,fmt):
- # 识别本地文件
- res = client.asr(open(sound_filename,'rb').read(), fmt, 16000, {
- 'dev_pid': 1536,
- })
- print(res)
- if 'result' in res.keys():
- return(res['result'][0])
- return ''
-
-
-
- def test():
- while True:
- #录音
- record('output.wav')
- #播放录音
- play_wav('output.wav')
- #将wav转化为pcm文件
- pcm_filename = wav2pcm('output.wav')
- #将声音转化为文字
- res = sound2text(pcm_filename,'pcm')
- if res:
- #将文字转化为声音并读出
- text2sound(res)
-
-
- test()