Advertisement
FlyFar

face.py

Feb 26th, 2023
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | Cybersecurity | 0 0
  1. # !/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. import smtplib
  5. from email.mime.multipart import MIMEMultipart
  6. from email.mime.application import MIMEApplication
  7. from email.mime.text import MIMEText
  8. from email.header import Header
  9. from email.mime.image import MIMEImage
  10. import zipfile
  11. import cv2
  12. import time
  13. image_list=[]#图片列表
  14. # 第三方 SMTP 服务
  15. mail_host = "smtp.163.com"  # 设置服务器
  16. mail_user = "XXXXX@163.com"  # 用户名
  17. mail_pass = "XXXXXXXXXXXXX"  # 口令
  18. receiver = 'XXXXX@163.com'  #接收邮件
  19. def SendEamil(zip_file,image_file):
  20.     # 邮件内容
  21.     email_box = MIMEMultipart()#创建容器
  22.     # message = MIMEText("警戒警戒!莎莎检测到有人入侵!数据以保存喵~", 'plain', 'utf-8')#邮箱文字
  23.     # email_box.attach(message)#存入
  24.     email_box['From'] =  "Salsa<"+mail_user+ ">"#发送人
  25.     email_box ['To'] =  receiver#发给谁
  26.     email_box ['Subject'] = Header("入侵警报", 'utf-8')#标题
  27.  
  28.     #发送压缩文件
  29.     zip_apart = MIMEApplication(open(zip_file, 'rb').read())
  30.     zip_apart.add_header('Content-Disposition', 'attachment', filename=zip_file)
  31.     email_box.attach(zip_apart)
  32.     #添加表情包图片
  33.  
  34.     msgAlternative = MIMEMultipart('alternative')
  35.     email_box.attach(msgAlternative)
  36.  
  37.     mail_msg = """
  38.    <p>警戒警戒!莎莎检测到有人入侵!数据以保存喵~</p>
  39.    <p><img src="cid:dns_config"></p>
  40.    """
  41.     msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
  42.     # 指定图片为当前目录
  43.     file = open(image_file, "rb")
  44.     img_data = file.read()
  45.     file.close()
  46.     img = MIMEImage(img_data)
  47.     img.add_header('Content-ID', 'dns_config')
  48.     email_box.attach(img)
  49.     try:
  50.         smtpObj = smtplib.SMTP()
  51.         smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
  52.         smtpObj.login(mail_user, mail_pass)
  53.         smtpObj.sendmail(mail_user, receiver, email_box.as_string())
  54.         print("发送成功")
  55.         smtpObj.quit()
  56.         smtpObj.close()
  57.     except smtplib.SMTPException as e:
  58.         print(e)
  59.  
  60. def ZIP(time_name):
  61.     zip_file = zipfile.ZipFile(time_name+'.zip', 'w', zipfile.ZIP_DEFLATED)
  62.     # 把zfile整个目录下所有内容,压缩为new.zip文件
  63.     zip_file.write(time_name+'.jpg')
  64.     # 把c.txt文件压缩成一个压缩文件
  65.     # zip_file.write('c.txt',compress_type=zipfile.ZIP_DEFLATED)
  66.     zip_file.close()
  67.     return time_name+'.zip'
  68.  
  69.  
  70. def FaceDetection():
  71.     FaceCascadePath = "haarcascade_frontalface.xml"
  72.     FaceCascade = cv2.CascadeClassifier(FaceCascadePath)
  73.     # FaceCascade = cv2.CascadeClassifier(config.FaceCascadePath)
  74.  
  75.     # 打开视频捕获设备
  76.     video_capture = cv2.VideoCapture(0)
  77.  
  78.     while True:
  79.         if not video_capture.isOpened():  # 检查它是否被初始化
  80.             time.sleep(5)
  81.             pass
  82.  
  83.         # 读视频帧
  84.         ret, frame = video_capture.read()  # 如果帧被正确读取,则为True
  85.  
  86.         # 转为灰度图像
  87.         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  88.  
  89.         # 调用分类器进行检测,如果有人脸返回一个列表
  90.         faces = FaceCascade.detectMultiScale(
  91.             gray,
  92.             scaleFactor=1.1,
  93.             minNeighbors=5,
  94.             minSize=(30, 30),
  95.             # flags=cv2.cv.CV_HAAR_SCALE_IMAGE
  96.         )
  97.         if len(faces) > 0:
  98.             time_name=str(int(time.time()))
  99.             image_list.append(time_name)
  100.             time.sleep(1)
  101.             cv2.imwrite(time_name + '.jpg', frame)  # 存储为图像
  102.             if len(image_list)>4:#如果容器里面图片超过4张发送邮件,防止疯狂发送
  103.                 zip_file=ZIP(time_name)
  104.                 SendEamil(zip_file, "test.gif")
  105.                 image_list.clear()
  106.  
  107.         # 画矩形框
  108.         #for (x, y, w, h) in faces:
  109.          #   cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  110.  
  111.         # 显示视频
  112.         #cv2.imshow('Video', frame)
  113.  
  114.         #if cv2.waitKey(1) & 0xFF == ord('q'):
  115.          #   break
  116.  
  117.     # 关闭摄像头设备
  118.     video_capture.release()
  119.  
  120.     # 关闭所有窗口
  121.     #cv2.destroyAllWindows()
  122.  
  123.  
  124. if __name__ == '__main__':
  125.     FaceDetection()
Tags: face
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement