import os import time模块 from aip import AipOcr,AipSpeech """ 你的 APPID AK SK错误在哪?


python版本:3.7.2aip 库版本 0.1
买了一本书,照着上面的代码写,结果第一句就显示红色from aip import AipOcr
这个aip库,在cmd中直接pip安装,报错,显示没有这个库。之后在pycharm中下载第三方库的镜像源中添加了所有的国内源,像豆瓣,清华,阿里等等,最后在豆瓣源下载的aip,版本为0.1百度了一番,很多网友说需要安装的是 baidu-aip 这个库,不是 aip库于是安装了 baidu-aip 库,版本为 2.2.18但是安装了 baidu-aip 库之后仍然报错联想到网友们说的 baidu-aip 库可以,于是我把这个最新版的baidu-aip卸载了,找了一个旧版的,2.0.0版本的baidu-aip运行正常!
运行之前的代码报错,很有可能就是因为库的更新
如果遇到了这种运行别人的代码报错,有两种解决方法如果是某一个函数报错,一般是该函数过时了,出现了新的函数,这种情况可以查看源码,鼠标放在函数名中,ctrl+鼠标左键 查看源码,一般都会在源码中注释的有新的函数名,或者其他相应的函数如果像这种导入出错,或者源码中没有用注释写上解决办法,就可以考虑降低库的版本}
python引入from aip import报错Traceback (most recent call last):File "E:\LearnByMyself\HTML\sublimetext3\sublimetext3pjb_47542\Sublime Text 3.3126x86\识别车牌号.py", line 4, infrom aip import AipOcrFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aip\__init__.py", line 6, infrom .ocr import AipOcrFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aip\ocr.py", line 12, infrom .base import AipBaseFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aip\base.py", line 13, inimport requestsFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\__init__.py", line 43, inimport urllib3File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-3
您愿意向朋友推荐“博客详情页”吗?
强烈不推荐
不推荐
一般般
推荐
强烈推荐
}
试图调百度 谷歌 科大讯飞的语音识别API进行语音转文字1.谷歌(谷歌好像需要梯子)需要下载ffmpeg.exe和ffprobe.exe下载连接https://ffmpeg.zeranoe.com/builds/MP3的音频文件不能直接用谷歌的语音识别去识别,可以看下这篇文章(https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/79832700(删掉号)这里面介绍了有关音频的一些理论知识,什么频率赫兹啥的),包括谷歌语音识别接口的使用;所#以,在识别语音之前,我们要先对保存到本地的语音消息转化格式,这里用到了pydub的#ffmpeg和ffprobe(https://ffmpeg.zeranoe.com/builds/下载完解压,ffmpeg.exe和#ffprobe.exe复制到你想放的地方,然后再代码中引用一下就可以了,windows平台可以,#不止linux是否可以)将mp3转化成wavfrom pydub import AudioSegment
import os
from os import path
# os.getcwd() 是获取当前路径,这里可以写绝对路径
AudioSegment.ffmpeg = os.getcwd()+'\\ffmpeg.exe'
AudioSegment.ffprobe = os.getcwd()+'\\ffprobe.exe'
def dealMp3(filePath,fileName):
sound = AudioSegment.from_mp3(filePath)
#获取原始pcm数据
data=sound._data
sound_wav = AudioSegment(
#指定原始pcm文件
# raw audio data (bytes)
data = data,
#指定采样深度,可选值1,2,3,4
# 2 byte (16 bit) samples
sample_width = 2,
#指定采样频率
# 44.1 kHz frame rate
# 16kHz frame rate
frame_rate = 16000,
#指定声道数量
# stereo or mono
channels = 1
)
#导出wav文件到当前目录下
sound_wav.export(fileName,format='wav')
# 判断生成wav格式的文件成功没
isDeal = os.path.exists(os.getcwd()+'\\'+fileName)
#如果wav文件生成了就删除mp3文件 - -这个可以不参考
if isDeal:
#删除mp3文件
os.remove(filePath)
return isDeal
将wav识别成文字
from pydub import AudioSegment
import os
from os import path
# os.getcwd() 是获取当前路径,这里可以写绝对路径
AudioSegment.ffmpeg = os.getcwd()+'\\ffmpeg.exe'
AudioSegment.ffprobe = os.getcwd()+'\\ffprobe.exe'
def voice2Text(file_name):
voice_file = path.join(path.dirname(path.realpath(__file__)), file_name)
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(voice_file) as source:
audio = r.record(source)
try:
content = r.recognize_google(audio, language='zh-CN')
print("Google Speech Recognition:" + content)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Google Speech Recognition error; {0}".format(e))
return content or '无法翻译'
2.百度加载需要的库,其中from aip import AipSpeech是百度的
from pydub import AudioSegment
import os
from os import path
import speech_recognition as sr
from aip import AipSpeech
import time
下面这个函数是切割mp3的,因为百度语音识别的API只能60秒# =============================================================================
# #切割完的存放地址
# filePath = r'C:\Users\Administrator\Desktop\python工程\语音识别系统\block'
#
# # 操作函数
# def get_wav_make(dataDir):
#
sound= AudioSegment.from_mp3(dataDir)
#
duration = sound.duration_seconds * 1000
# 音频时长(ms)
#
for i in range(0,int(duration),59000):
#
begin = i
#
end = i + ( 1000 * 59 )
#
cut_wav = sound[begin:end]
#以毫秒为单位截取[begin, end]区间的音频
#
cut_wav.export(filePath+ 'test'+ str(int(i/( 1000 * 59 ))) + '.mp3', format='mp3')
#存储新的wav文件
#
# get_wav_make(r'C:\Users\Administrator\Desktop\python工程\语音识别系统\22.mp3')
#切割完的存放地址
#filePath = r'C:\Users\Administrator\Desktop\python工程\语音识别系统\block'
# =============================================================================
进行语音转文字get_wav_make(r'C:\Users\Administrator\Desktop\python工程\语音识别系统\22.mp3')
#下面输入你们在百度只智能注册号后给出的id key啥的
APP_ID = 'APP_ID '
API_KEY = 'API_KEY '
SECRET_KEY = 'SECRET_KEY '
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
#定义函数
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 开始识别
start_time = time.time()
ret = client.asr(get_file_content(r'C:\Users\Administrator\Desktop\python工程\语音识别系统\blocktest2.mp3'), 'pcm', 16000, {
'dev_pid': 1537,
})
used_time = time.time() - start_time
print( "used time: {}s".format( round( time.time() - start_time, 2 ) ) )
print('ret:{}'.format(ret))
3.科大讯飞忘了}

我要回帖

更多关于 import time模块 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信