调用百度语音AI实现语音的识别和合成
#coding:utf-8
## 先去ffmpeg官网下载(https://ffmpeg.zeranoe.com/builds/),好了之后解压缩,配一下环境变量
## 打开cmd,运行命令,安装如下的包
## pip install baidu-aip
## pip install pydub
## pip install PyAudio
## pip install Wave
""" 调用百度语音api """
from aip import AipSpeech
APP_ID = " "
API_KEY = " "
SECRET_KEY = " "
client = AipSpeech(APP_ID,API_KEY,SECRET_KEY)
def speech_synthesis(text, filepath):
""" 语音合成:文字转语音 """
result = client.synthesis(text, 'zh', 1, {
'vol': 5,
'spd': 5,
'pit': 5,
'per': 0,
})
if not isinstance(result, dict):
with open (filepath , 'wb') as file: file.write(result)
def play_speech(filepath):
import os
os.system("ffplay %s"%(filepath))
# def play_speech(filepath):
# """ 播放语音 """
# import pyaudio
# import wave
# wf = wave.open(filepath, 'rb') #二进制只读方式打开wav文件
# p = pyaudio.PyAudio()
# stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True)
# stream = p.open(format=pyaudio.paInt16,
# channels=1,
# rate=16000,
# output=True) #打开数据流
# data = wf.readframes(1024) #读取数据
# while data != '': #播放
# stream.write(data)
# data = wf.readframes(1024)
# stream.stop_stream()
# stream.close()
# p.terminate()
# def Conversion_sampling_rate(filepath, newfilepath):
# """ 转换采样率 """
# from pydub import AudioSegment
# setframefp = AudioSegment.from_file(filepath)
# setframefp.set_frame_rate(16000)
# setframefp.export(newfilepath, format='wav')
def wav_to_pcm(wav_file):
""" wav文件转为16k pcm文件 """
import os
pcm_file = "%s.pcm" %(wav_file.split(".")[0])
os.system("ffmpeg -y -i %s -acodec pcm_s16le -f s16le -ac 1 -ar 16000 %s"%(wav_file,pcm_file))
return pcm_file
def sound_record(file_name):
""" 录音 """
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 3
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("开始录音,请说话......")
frames = []
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(file_name, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def speech_recognition(filepath):
""" 语音识别:语音转文字 """
with open(filepath, 'rb') as fp:speechfile = fp.read()
result = client.asr(speechfile, 'pcm', 16000, {
'dev_pid': 1536,
})
try:
res_str = result.get("result")[0]
print(res_str)
except:
res_str = "error"
print("识别没有成功")
return res_str
# 测试
# text = "世界很复杂百度更懂你"
# synthesisfilepath = "synthesisspeech.pcm"
# synthesisfilepath = "16k.pcm"
# speech_synthesis(text, synthesisfilepath)
# wav_file = pcm_to_wav(synthesisfilepath)
# play_speech(wav_file)
# recordfilepath = "recordspeech.wav"
# sound_record(recordfilepath)
# pcm_file = wav_to_pcm(recordfilepath)
# speech_recognition(pcm_file)
""" 控制面板 """
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
class App:
def __init__(self, master):
self.master = master
self.master.title("调用百度AI识别语音")
self.master.geometry("500x400")
self.buttonimg = PhotoImage(file= os.path.join(os.path.dirname(os.path.abspath(__file__)), 'luyin - small.gif'))
self.initWidgets()
def initWidgets(self):
self.button = Button(self.master, text='开始录音', image=self.buttonimg, command=self.open_sound_record, height=100,width=100)
self.button.pack(ipadx=5, ipady=5, pady = 20)
self.label = Label(self.master, text="语音识别结果:")
self.label.place(x=100,y=400,anchor='nw')
self.label.pack()
self.text = Text(self.master, height=3, width=200)
self.text.place(x=150,y=400,anchor='nw')
self.text.pack()
def open_sound_record(self):
recordfilepath = "recordspeech.wav"
sound_record(recordfilepath)
pcm_file = wav_to_pcm(recordfilepath)
res_str = speech_recognition(pcm_file)
if res_str == "error":
print(messagebox.showinfo("出错","没有成功识别语音!"))
else:
self.text.insert("insert", res_str)
# text = "语音识别的结果是"+res_str
# synthesisfilepath = "synthesisspeech.pcm"
# speech_synthesis(text, synthesisfilepath)
# play_speech(synthesisfilepath)
root = Tk()
App(root)
root.mainloop()
调用百度语音AI实现语音的识别和合成的更多相关文章
- QT调用百度语音REST API实现语音合成
QT调用百度语音REST API实现语音合成 1.首先点击点击链接http://yuyin.baidu.com/docs/tts 点击access_token,获取access_token,里面有详细 ...
- 初探机器学习之使用百度AI服务实现图片识别与相似图片
一.百度云AI服务 最近在调研一些云服务平台的AI(人工智能)服务,了解了一下阿里云.腾讯云和百度云.其中,百度云提供了图像识别及图像搜索,而且还细分地提供了相似图片这项服务,比较符合我的需求,且百度 ...
- c# 利用AForge和百度AI开发实时人脸识别
baiduAIFaceIdentify项目是C#语言,集成百度AI的SDK利用AForge开发的实时人脸识别的小demo,里边包含了人脸检测识别,人脸注册,人脸登录等功能 人脸实时检测识别功能 思路是 ...
- 【第1篇】人工智能(AI)语音测试原理和实践---宣传
前言 本文主要介绍作者关于人工智能(AI)语音测试的各方面知识点和实战技术. 本书共分为9章,第1.2章详细介绍人工智能(AI)语音测试各种知识点和人工智能(AI)语音交互原理:第3.4章介绍人工智 ...
- 基于HTK语音工具包进行孤立词识别的使用教程
选自:http://my.oschina.net/jamesju/blog/116151 1前言 最近一直在研究HTK语音识别工具包,前几天完成了工具包的安装编译和测试,这几天又按耐不住好奇,决定自己 ...
- 三星语音AI助理背后的华人身影—73岁科技人三度创业成功(孙子兵法:道、天、地、将、法)
我绝对不当老二,也不当老大,我要当霸主!”说这句话的是富迪科技董事长黄炎松.他还把“独霸”当作公司愿景宣言,大剌剌的放在美国总公司进门最显眼的墙上. 集微网消息,据台湾商业周刊报道,黄炎松,是台湾 ...
- Excel催化剂开源第25波-Excel调用百度AI,返回AI结果
现成的这些轮子,无需调用网页,直接本地离线即可生成). 当然在AI时代,少不了各种AI接口的使用场景,普通开发者只需聚焦在自己的业务场景上,这些AI底层技术,只需类似水煤电一般去BAT这些大厂那里去消 ...
- Java文字识别软件-调用百度ocr实现文字识别
java_baidu_ocr Java调用百度OCR文字识别API实现图片文字识别软件 这是一款小巧方便,强大的文字识别软件,由Java编写,配上了窗口界面 调用了百度ocr文字识别API 识别精度高 ...
- 百度语音合成AI
注意:不要使用Dw编辑PHP代码,会因为编码问题出错!!<?php require_once 'AipSpeech.php'; // 你的 APPID AK SK const APP_ID = ...
随机推荐
- Centos 下安装 PHP (新)
今天重新实践了下 CentOS 7.6 下安装 PHP7 并完成配置,总结了一条可以照其实现的套路. 安装 PHP 所需扩展 # yum install libxml2 libxml2-devel o ...
- C# -- 优先获取电脑C盘之外的磁盘来保存数据
C# -- 优先获取电脑C盘之外的磁盘来保存数据 1. 优先获取电脑C盘之外的磁盘来保存数据.没有其他盘则使用C盘. public string GetSaveDataDiskPath() { str ...
- VS 2017 代码报错编译正常
今天遇到一个奇葩的错误,代码报红波浪线错误,但编译正常,程序能正常运行; 解决方法 在项目引用中把报错的代码所在项目先移除,再重新引用,然后编译一下就好了
- Java生鲜电商平台-生鲜系统中微服务架构设计与分析实战
Java生鲜电商平台-生鲜系统中微服务架构设计与分析实战 说明: Java生鲜系统中微服务的拆分应该如何架构设计与分析呢?以下是我的实战中的设计与经验分析. 目录 1. 微服务简介2. 当前现状3. ...
- 搭建ES集群
服务版本选择 TEG的ctsdb当前最高版本采用的是es的6.4.3版本,为了日后与ctsdb衔接方便,部署开源版es时也采用该版本.6.4.3版本的es依赖的jdk版本要求在8u181以上,测试环境 ...
- [转]UiPath Keyboard Shortcuts
本文转自:https://docs.uipath.com/studio/docs/keyboard-shortcuts The complete list of keyboard shortcuts ...
- SAP FI 问题汇总
记录工作中遇到的问题汇总 1.固定资产折旧码的设置 2.与资产有关的日期 3.如何添加固定资产分类
- Linux中crontab定时任务
crontab安装(centOS) yum -y install vixie-cron crontab语法(计划任务) crontab [-u user] file crontab [-u user] ...
- MySQL数据库:group分组
group by:分组 GroupBy语句从英文的字面意义上理解就是"根据(by)一定的规则进行分组(Group)".它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后 ...
- 06-Node.js学习笔记-创建web服务器
创建web服务器 //引用系统模块 const http = require('http'); //创建web服务器 //用于处理url地址 const url = require('url'); c ...