Python基于百度API 图片识别+文字转语音

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
    功能:利用百度官方api,读取图片中的文字,同时将文字转换成语音
    官方地址:http://ai.baidu.com/docs#/OCR-Python-SDK/top
'''


from aip import AipOcr,AipSpeech


""" 你的 APPID AK SK """
# 定义常量
# APP_ID = '你的appid' #你百度帐号上的APP_ID
# API_KEY = '你的apikey' #你百度帐号上的API_KEY
# SECRET_KEY = '你的secretkey'#你百度帐号上的SECRET_KEY



clientAipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
clientAipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

#测试图片
picture_url="http://image.bug2048.com/mongo20180906.jpg"


""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

"""
    1.调用文字识别API识别图片上的文字
    2.拼接文字后调用语音合成API转换成语音
"""

def convert_picture_words():
    words=''
    wordsResult=clientAipOcr.basicGeneralUrl(picture_url)
    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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知火猪

如果觉得有帮助,打赏鼓励,3Q

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值