脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python实现调用摄像头并拍照发邮箱

python实现调用摄像头并拍照发邮箱

2021-10-21 09:39flygaga Python

这篇文章主要介绍了python实现调用摄像头并拍照发邮箱的程序,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下

项目地址:

https://github.com/flygaga/camera

思路

1、通过opencv调用摄像头拍照保存图像到本地

2、用email库构造邮件内容,保存图片以附件形式插入邮件内容

3、用smtplib库发送邮件到指定邮箱

4、生成 .exe 文件

5、设置开机自启(每次开机自动运行,启动相机,拍下照片发送到指定邮箱)

导入工具

?
1
2
3
4
5
6
7
8
9
import cv2 # pip install opencv-python -i {指定镜像源} 控制摄像头
 
from email.mime.image imort MIMEImage #用来构造邮件内容的库
 
from email.mime.text import MIMEText
 
from email.mime.multipart import MIMEMultipart
 
import smtplib #发送邮件

编译环境

系统:Windows10

软件:Miniconda3-latest-Windows-x86_64

模块:opencv-python smtplib numpy email pyinstaller

生成exe文件

pyinstaller -F -w path/camera.py

设置开机自启

1.右击exe 创建快捷方式

2.win+r 输入以下命令 shell:startup 点击确定打开一个文件夹

3.将生成的快捷文件复制到打开的文件中,下次开机exe程序就会自动启动

python代码实现调用摄像头,并拍照发送邮件

主要代码

camera.py

?
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
82
import cv2
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# import smtplib #发送邮件
import smtplib
from smtplib import SMTP
import time
 
host = 'smtp.qq.com' #邮箱的接口
port = '25' #端口
pwd = 'neelrhh88******ch' #授权码
sender = '邮箱地址' #发送方
receiver = "邮箱地址" #接收方
 
path = r'./' #图像保存路径
images = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime())
 
def GetPicture():
    """
    拍照保存图像
    """
    #创建一个窗口camera
    cv2.namedWindow('camera',1) #'1' 表示窗口不能随意拖动
    #调用摄像头
    cap = cv2.VideoCapture(0)
    ret,frame = cap.read() #读取摄像头内容
    cv2.imwrite(path+images+".jpg",frame)  #保存到磁盘
 
 
    #释放摄像头
    cap.release()
    #关闭窗口
    cv2.destroyWindow("camera")
 
def SetMsg():
    '''
    设置邮件格式
    :return:
    '''
    msg = MIMEMultipart('mixed')
    #标题
    msg['Subject'] = '电脑已开机'
    msg['From'] = sender
    msg['To'] = receiver
    #邮件正文内容
    text = '电脑已开机,请查收图片确认是否为本人'
    text_plain = MIMEText(text,'plain','utf-8') #正文转码
    msg.attach(text_plain)
 
    #图片
    SendImageFile = open(path+images+'.jpg','rb').read()
    image = MIMEImage(SendImageFile)
    image['Content-Disposition'] = 'attachment;filename="people.jpg"'
    msg.attach(image)
    return msg.as_string()
 
def SendEmail(msg):
    '''
    发送邮件
    :msg :邮件内容
    :return
    '''
    try:
        smtp = smtplib.SMTP_SSL(host,port) #创建一个邮件服务
        # smtp.connect(host)
        smtp.login(sender,pwd)
        smtp.sendmail(sender,receiver,msg)
        time.sleep(3)
        smtp.quit() #退出邮件服务
    except smtplib.SMTPException as e:
        print("e")
#实现开机自启动
#打包实现启动  例:exe
 
if __name__ == '__main__':
    # 1.拍照保存
    GetPicture()
    # 2. 设置邮件格式
    msg = SetMsg()
    # 3. 发送邮件
    SendEmail(msg)

以上就是python实现调用摄像头并拍照发邮箱的详细内容,更多关于python 调用摄像头的资料请关注服务器之家其它相关文章!

原文链接:https://github.com/flygaga/camera

延伸 · 阅读

精彩推荐