-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbaidu-aip-demo.py
More file actions
59 lines (44 loc) · 1.42 KB
/
baidu-aip-demo.py
File metadata and controls
59 lines (44 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Cavin Cao'
'''
功能:利用百度官方api,读取图片中的文字,同时将文字转换成语音
官方地址:http://ai.baidu.com/docs#/OCR-Python-SDK/top
demo对应博文:
'''
import config
from aip import AipOcr,AipSpeech
""" 你的 APPID AK SK """
APP_ID = config.baidu_app_id
API_KEY = config.baidu_api_key
SECRET_KEY = config.baidu_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)
speechResult=clientAipSpeech.synthesis(words, 'zh', 1, {
'vol': 5,
'per': 3
})
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(speechResult, dict):
with open('result.mp3', 'wb') as f:
f.write(speechResult)
if __name__ == '__main__':
convert_picture_words()