【开发需求】python开发一个智能公众号,实现以下功能:
1.根据用户输入的文本内容进行简单的人机对话;
2.调用百度人工智能接口,讲用户输入的图片的文字提取出来。
注意picocr为自定义编写的截图文字识别OCR模块,参考链接查看源代码。
import werobot
import picocr
import requests
import uuid
robot = werobot.WeRoBot(token='doctorwu')
@robot.subscribe
def hello(message):
return '欢迎来到无尘的小站,发送带文字的图片,我可以帮你转换成可以复制的文本'
@robot.text
def reply(message):
content = message.content
if content[0:3]=='###':
return 'I will check its pseudomeaning.'
elif content[-2:]=='吗?':
msg = content[:-2]+'!'
msg = msg.replace('你','我')
return msg
else:
return '发送带文字的图片,我可以帮你转换成可以复制的文本'
@robot.image
def image_ocr(message):
req = requests.get(message.img)
filename = f'{uuid.uuid4()}.jpg'
print(filename)
with open('./screenshots/'+filename,'wb') as f:
f.write(req.content)
return picocr.get_ocr_result(filename)
robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()