Python 学习之旅1

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值