Python 学习之旅1
2019年10月11日
来到你面前的是一个图像识别的小程序。它能做的是将写在纸上的文字,精准地识别出来。如下图:
import requests
from aip import AipOcr
image = requests.get(‘https://res.pandateacher.com/python_classic.png’).content
APP_ID = ‘16149264’
API_KEY = ‘yxYg9r4OuAs4fYvfcl8tqCYd’
SECRET_KEY = ‘yWg3KMds2muFsWs7MBSSFcgMQl8Wng4s’
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
res = client.basicGeneral(image)
if ‘words_result’ in res.keys():
for item in res[‘words_result’]:
print(item[‘words’])
else:
APP_ID = ‘11756541’
API_KEY = ‘2YhkLuyQGljPUYnmi1CFgxOP’
SECRET_KEY = ‘4rrHe2BF828bI8bQy6bLlx1MelXqa8Z7’
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
res = client.basicGeneral(image)
if ‘words_result’ in res.keys():
for item in res[‘words_result’]:
print(item[‘words’])
else:
print(res)
下面,我们再来一个更厉害的:真正地运行一个人工智能小程序,和计算机进行“对话”。它其实就是Python码出来的Siri。
【温馨提示】如果你要删除自己输入的内容,要按两次删除,才可以删掉一个汉字奥!
(因为在计算机世界里,中文是占两个字符的!)
import requests, json, time, random
feature_text = '''
大家好!我是你的聊天机器人吴小枫。
我有问必答,有人会问我“今天深圳天气怎么样?”,也有人问我“你喜欢我吗?”
快来问我问题呀,欢迎来撩!
user1 = input(feature_text)
time.sleep(1)
userid = str(random.randint(1, 1000000000000000000000))
apikey = 'd81c0b99c260440980a140440be200ec'
#超过1w有风险
tulingdata1 = json.dumps({ "perception": {
"inputText": {
"text": user1
},
},
"userInfo": {
"apiKey": apikey,
"userId": userid
}
})
robot1 = requests.post('http://openapi.tuling123.com/openapi/api/v2', tulingdata1)
jsrobot1 = json.loads(robot1.text)['results'][0]['values']['text']
print(jsrobot1)
time.sleep(2)
user2 = input('''
再来问我点啥吧!我把我知道的都告诉你,嘻嘻!
>''')
tulingdata1 = json.dumps({
"perception": {
"inputText": {
"text": user2
},
},
"userInfo": {
"apiKey": apikey,
"userId": userid
}
})
robot1 = requests.post('http://openapi.tuling123.com/openapi/api/v2', tulingdata1)
jsrobot1 = json.loads(robot1.text)['results'][0]['values']['text']
time.sleep(1)
print(jsrobot1)
user3 = input('''
我有点饿了,再和你聊完最后一句,我就要下线啦!你还有什么要问我的?
>''')
tulingdata1 = json.dumps({
"perception": {
"inputText": {
"text": user3
},
},
"userInfo": {
"apiKey": apikey,
"userId": userid
}
})
robot1 = requests.post('http://openapi.tuling123.com/openapi/api/v2', tulingdata1)
jsrobot1 = json.loads(robot1.text)['results'][0]['values']['text']
time.sleep(1)
print(jsrobot1)
time.sleep(1)
print('\n我走啦,下次见!')
转义字符的学习
变量的赋值
内容学习来自风变学习
https://shimo.im/docs/FFMiiTLcSPAYrvIg/read