0X00
itchat 是一个开源的第三方微信Python库,提供了完整的微信接口。网上有人基于itchat创建了很多有趣的小程序。六月,我依葫芦画瓢,抄写了一个调用图灵机器人接口的微信聊天机器人。今天我看到知乎上有人在做 opensv-python的人脸识别,于是又捡起了我的微信机器人,抄加了这个功能。
用到的第三方库有:
- itchat ->(sudo) pip install itchat
- opencv-python ->(sudo) pip install opencv-python
0x01
代码如下: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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82import requests
import itchat
import time
from itchat.content import *
import cv2
import os
from PIL import Image
#构建图灵机器人消息回复
def tuling(msg):
url='http://www.tuling123.com/openapi/api'
data={
'key' :'0bda0c8b640f432d96d13396856c5a4a',
'info' :msg,
'userid':'hello-ffshine'
}
try:
r=requests.post(url,data=data).json()
return r.get('text')
except:
return
#png转jpg
def toJpg(msg):
img=Image.open('./'+msg['FileName'])
newImg= msg['FileName'][:-4] + '.jpg'
img.save(newImg,format="jpeg")
return os.getcwd() + '/' + newImg
#构建简单的人像识别
def humanFace(msg):
faceData = '/home/ss/Desktop/opencv/data/3.xml'
#photopath = os.getcwd() + msg['FileName']
photopath = msg
image = cv2.imread(photopath)
#gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
face_casacade = cv2.CascadeClassifier(faceData)
faces = face_casacade.detectMultiScale(image)
color = (0,0,225)
strokeWeight = 2
a = '您发来的图片'+'共识别到'+str(len(faces))+'个人'
for x,y,width,height in faces:
cv2.rectangle(image,(x,y),(x + width,y +height),color,strokeWeight)
cv2.imwrite(os.getcwd() + '/new.jpg',image)
return a
#回复好友
def frind_reply(msg):
defaultReply='不好意思,我出错了!!'
reply=tuling(msg['Text'])
return reply or defaultReply
#回复群聊
def group_reply(msg):
itchat.send(u'@'+msg['ActualNickName']+': '+tuling(msg['Content']),msg['FromUserName'])
#处理好友发来的图片等信息
def download_files(msg):
msg['Text'](msg['FileName'])
a = humanFace(toJpg(msg))
itchat.send_image('./new.jpg',msg['FromUserName'])
return a
#群友发来的图片
def download_group_files(msg):
msg['Text'](msg['FileName'])
a = humanFace(toJpg(msg))
itchat.send_image('./new.jpg',msg['FromUserName'])
return u'@'+msg['ActualNickName']+': '+a
#有新好友申请,则自动同意加好友并录入消息
def add_friend(msg):
itchat.add_friend(**msg['Text'])
itchat.send_msg('你好,Nice to meet you',msg['RecommendInfo']['UserName'])
#登录,并保持运行
#itchat.auto_login(hotReload=True)
itchat.auto_login(enableCmdQR=2)
itchat.run()
0x02
参考资料