Python 微信机器人:调用电脑摄像头时时监控功能实现演示,调用电脑摄像头进行拍照并保存。

时间:2022-07-25
本文章向大家介绍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."原因及解决办法