"""Description: 录入语音流程:采样-》写入文件"""import pyaudioimport wavedef record(seconds,filename):RATE=8000#采样率CHANNELS=2#采样管道数FORMAT=pyaudio.paInt16#量化位数SECONDS=seconds#录音时长#第一步:创建PyAudio的实例对象p = pyaudio.PyAudio()#第二步:调用PyAudio实例对象的open方法创建流Streamstream=p.open(rate=RATE,channels=CHANNELS,format=FORMAT,input=True)frames=[]#存储所有读取到的数据print("录音开始,还有",seconds,"秒")#第三步:根据需求,调用Stream的write或者read方法data=stream.read(RATE*SECONDS)frames.append(data)#第四步:调用Stream的stop方法停止播放音频或者是录制音频stream.stop_stream()print("录音结束!!!")#第五步:调用Stream的close方法,关闭流stream.close()#第六步:调用pyaudio.PyAudio.terminate() 关闭会话p.terminate()#写入到wav文件里面wf=wave.open(filename,"wb")wf.setnchannels(CHANNELS)wf.setframerate(RATE)wf.setsampwidth(p.get_sample_size(FORMAT))wf.writeframes(b''.join(frames))wf.close()return filename
"""Description: 语音识别"""from aip import AipSpeechfrom aip import AipOcrimport osfrom baidu_ai import record""" 你的 APPID AK SK """APP_ID = '14654414'API_KEY = 'avXA70ZkquBA83UMVSwUoeen'SECRET_KEY = 'kvf6CaTgLi7URiwAllGqcChQUsGyA58v '# client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)#语音识别#filename=record.record(2,'test.wav')# # 读取文件# def get_file_content(filePath):#with open(filePath, 'rb') as fp:# return fp.read()## # 识别本地文件# result=client.asr(get_file_content(filename), 'wav', 16000)# print(result)#语音合成# result = client.synthesis('床前明月光,疑是地上霜。举头望明月,低头思故乡。', 'zh', 1, {#'vol': 15,'per':0# })# filepath='test1.mp3'# with open(filepath, 'wb') as f:#f.write(result)# os.system("start "+filepath)# 图片的文字提取client = AipOcr(APP_ID, API_KEY, SECRET_KEY)""" 读取图片 """def get_file_content(filePath):with open(filePath, 'rb') as fp:return fp.read()image = get_file_content('4.png')""" 调用通用文字识别, 图片参数为本地图片 """resutl=client.basicGeneral(image)print(resutl)
如果觉得《利用百度ai接口做语音识别》对你有帮助,请点赞、收藏,并留下你的观点哦!