Python 微信机器人:调用电脑摄像头时时监控功能实现演示,调用电脑摄像头进行拍照并保存。
我想要做的就是用微信遥控电脑,电脑拍照后用微信进行接收。这样我就能时时监测到电脑摄像头范围内的景象了。
我们需要的是VideoCapture库,获取方式如下:
lfd.uci.edu下载
然后直接pip install把下载的库拖进来就好了。
调用摄像头功能实现
这3行代码就可以实现调用摄像头并保存照片的功能。
from VideoCapture import Device
cam = Device()
cam.saveSnapshot('camera.jpg')
微信端调用实现监控功能
我们设定的口令是“拍照”,当接收到这个消息后,机器人就会执行命令,调用摄像头,拍照保存,然后把照片传给发送消息的人。
from VideoCapture import Device
import itchat
# 执行拍照功能
def cameraRecord():
cam = Device()
cam.saveSnapshot('camera.jpg')
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def camera_itchat(msg):
msg_from=msg['FromUserName']
if '拍照'==msg['Text']:
cameraRecord()
itchat.send_image(fileDir='camera.jpg', toUserName=msg_from)
itchat.auto_login(hotReload=True)
itchat.run()
运行效果图:

喜欢的点个赞❤吧!
如果运行时出现"fromstring() has been removed. Please call frombytes() instead."错误,请看:
Python的VideoCapture库-运行时报错"fromstring() has been removed. Please call frombytes() instead."原因及解决办法
各位好,我是csdn的小蓝枣,现在我的博客已经同步到博客园啦,欢迎大家关注哈!也欢迎大家关注我的公众号Py_JSQY!每天会更新有趣的科技文!哈哈!
浙公网安备 33010602011771号