Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 49c99eb

Browse files
committed
no message
1 parent 4a927af commit 49c99eb

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

‎moumoubaimifan/siri/siri.py‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
from PIL import ImageGrab
2+
import requests
3+
import base64
4+
from urllib.parse import quote_plus
5+
6+
7+
def tuling(text):
8+
"""
9+
图灵机器人
10+
:return:
11+
"""
12+
13+
host = 'http://openapi.tuling123.com/openapi/api/v2'
14+
data = {
15+
"reqType":0, # 输入类型:0-文本(默认)、1-图片、2-音频
16+
"perception": { # 输入信息
17+
"inputText": { # 文本信息
18+
"text": text
19+
}
20+
},
21+
"userInfo": { # 用户信息
22+
"apiKey": "8d78a28535c947e69c2ddbcc5efeed51",
23+
"userId": "1234567"
24+
}
25+
}
26+
response = requests.post(host, json=data)
27+
if response:
28+
return response.json()['results'][0]['values']['text']
29+
else:
30+
return '错误了!'
31+
32+
def fetch_token():
33+
"""
34+
获取语音合成 token
35+
:return: token
36+
"""
37+
38+
access_token = ""
39+
40+
api_key = 'Pd4uoyvt1cwD7n2sHtLd5Ov0'
41+
secret_key = '8BnaPRcv3tTNa94eaFVfFy1pW2UkmvrO'
42+
43+
token_url = 'http://openapi.baidu.com/oauth/2.0/token'
44+
params = {'grant_type': 'client_credentials',
45+
'client_id': api_key,
46+
'client_secret': secret_key}
47+
response = requests.post(token_url, params)
48+
if response:
49+
access_token = response.json()['access_token']
50+
return access_token
51+
52+
53+
def synthesized_speech(text, token):
54+
"""
55+
合成语音
56+
"""
57+
58+
mp3_path = "/Users/xx/Desktop/siri/result.mp3"
59+
60+
tts_url = 'http://tsn.baidu.com/text2audio'
61+
62+
tex = quote_plus(text) # 此处TEXT需要两次urlencode
63+
# tok:token,tex:文本,per:发音人,spd:语速(0-15)默认 5,pit:音调(0-15)默认 5,
64+
# vol:音量(0-9)默认 5,aue:下载的文件格式, 3 mp3(default)、4 pcm-16k、5 pcm-8k、6 wav,
65+
# cuid:用户唯一标识,lan ctp 固定参数
66+
params = {'tok': token, 'tex': tex, 'per': 4, 'spd': 5, 'pit': 5, 'vol': 5, 'aue': 3, 'cuid': "pythonjishu", 'lan': 'zh', 'ctp': 1}
67+
68+
response = requests.post(tts_url, params)
69+
if response:
70+
with open(mp3_path, 'wb') as of:
71+
of.write(response.content)
72+
73+
74+
def grab_img():
75+
"""
76+
截图
77+
:return:
78+
"""
79+
80+
img_path = "/Users/xx/Desktop/siri/grab.png"
81+
# bbbox 的参数为截取屏幕的一部分,距离左边像素,上边像素,右边像素,下边像素
82+
83+
img = ImageGrab.grab(bbox=(2630,80,3330,1290))
84+
img.save(img_path)
85+
return img_path
86+
87+
def fench_ocr_token():
88+
"""
89+
获取ocr token
90+
:return: token
91+
"""
92+
93+
api_key = 'ioE84jDQmGNLG7heN6rovF9Q'
94+
secret_key = 'qGVyAobVtCGKdD1Ncz60IvGMdf3dP1ct'
95+
96+
access_token = ""
97+
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+api_key+'&client_secret='+secret_key
98+
response = requests.get(url)
99+
if response:
100+
access_token = response.json()['access_token']
101+
return access_token
102+
103+
def ocr(img_path, access_token):
104+
"""
105+
通用文字识别
106+
:return: 识别后的文字
107+
"""
108+
109+
text = ""
110+
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
111+
# 二进制方式打开图片文件
112+
f = open(img_path, 'rb')
113+
img = base64.b64encode(f.read())
114+
115+
params = {"image":img}
116+
request_url = request_url + "?access_token=" + access_token
117+
headers = {'content-type': 'application/x-www-form-urlencoded'}
118+
response = requests.post(request_url, data=params, headers=headers)
119+
if response:
120+
words_result = response.json()['words_result']
121+
print(words_result)
122+
text = words_result[-1]['words']
123+
return text
124+
125+
126+
if __name__ == '__main__':
127+
token = fetch_token()
128+
ocr_token = fench_ocr_token()
129+
siri_speak_text = "嗨,机器人!"
130+
while 1:
131+
text = tuling(siri_speak_text)
132+
synthesized_speech(text, token)
133+
img_path = grab_img()
134+
siri_speak_text = ocr(img_path, ocr_token)
135+
136+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /