调用百度智能云API,实现身份证智能识别并转语音 | Python
一、百度云新建应用、获取权限和额度
1. 登录百度智能云,产品服务-->人工智能-->图像识别
2. 应用列表-->创建应用,用于身份证照的信息识别
3. 应用创建完成,得到APP_ID、API_KEY 、SECRET_KEY
4. 新建工单,获取数据访问权限
否则运行代码会报错:
5. 同样的步骤创建一个语音技术的应用,用于语音合成
但是语音合成需要领取免费额度
否则代码运行后,语音合成失败:
二、代码编写
1. 从百度aip包中导入AipOcr、AipSpeech模块,并配置对应的APP_ID、API_KEY 、SECRET_KEY
2. 从本地文件夹读取身份证图片
3. 调用AipOcr的basicGeneral()方法识别图片上的文字,拼接文字后调用AipSpeech的synthesis()方法转换成语音
三、执行程序
准备就绪,运行代码,通过调用百度云Ocr API和Speech API,对准备好的身份证照进行信息识别,再把识别信息的文字内容转为语音,并把保存语音文件,得到运行结果:
程序所在文件夹生成了语音文件result.mp3:
Python源码:
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 03 14:30:43 2022
"""
from aip import AipOcr,AipSpeech
clientAipOcr = AipOcr('23371074', 'POTXBZN7KLWfyCmrT53w3vlT', 'Fy0ykaHwKtL1pggSjp4m0dwfR68Gb6om')
clientAipSpeech = AipSpeech('23374670', 'znrQvqN4ZSqROVfmupthcieR', '2FYUADL7Nzj3foOWdoWguamGsTrSls5r')
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('身份证照.png')
"""
1.调用文字识别API识别图片上的文字
2.拼接文字后调用语音合成API转换成语音
"""
def convert_picture_words():
words=''
wordsResult=clientAipOcr.basicGeneral(image)
for item in wordsResult['words_result']:
words+=item['words']+','
if words=='':
return
words=words[:-1]
print('words: ',words)
speechResult=clientAipSpeech.synthesis(str(words), 'zh', 1, {
'vol': 5,
'per': 3
})
print('result: ',speechResult)
# 识别正确返回语音二进制 错误则返回dict
if not isinstance(speechResult, dict):
with open('result.mp3', 'wb') as f:
f.write(speechResult)
print('oook')
if __name__ == '__main__':
convert_picture_words()
注:测试所用身份证照片来自百度图片。
【如果你觉得文章有用,记得点赞、收藏、关注哦!你的点赞是我创作的无限动力】
————————————————————————————————————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_47068543/article/details/125117573