我基本上有2个功能。一种是捕获在我的笔记本电脑上检测到的面部,我想收到一封电子邮件,上面有坐在我椅子上的用户的面部。我的问题是代码正在拍照,但未运行电子邮件提交。有人可以帮忙吗?
from imutils.video import VideoStream
import imutils, time, cv2, sys
import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.message import Message
from email.mime.audio import MIMEAudio
from email import encoders
def take_picture():
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
vs = VideoStream(src=0).start()
time.sleep(2.0)
total = 0
while True:
frame = vs.read()
orig = frame.copy()
frame = imutils.resize(frame, width=600)
rects = detector.detectMultiScale(
cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if rects is not None and total <5:
p="/Users/Desktop/atentie/mail/"+str(total)+".png"
cv2.imwrite(p, orig)
total+=1
else:
break
print("Initiez procedura de update root!")
def send_email():
files = "/Users/Desktop/atentie/mail/"
filenames = [os.path.join(files, f) for f in os.listdir(files)]
fromaddr = “mail”
toaddr = “mail”
Cc=“mail”
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Cc']=Cc
msg['Subject'] = "Atentionare Cineva Va Foloseste Laptopul"
body = "Cineva se joaca la laptopul tau"
msg.attach(MIMEText(body, 'plain'))
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, “pass”)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
print("Initiez procedura de trimitere email catre root!")
take_picture()
# time.sleep(2)
send_email()
# raise SystemExit
答案 0 :(得分:0)
您需要实现以下逻辑:
def send_picture(p):
# *** SEND_PICTURE_LOGIC ***
# Take the input picture p passed as argument
# Send this picture to email
def take_picture():
# *** TAKE_PICTURE_LOGIC ***
# If somebody is sitting on my chair
# take a picture and call it currentPicture
# Call send_picture function with the currentPicture acquired
send_picture(currentPicture)
这将导致为每次收购发送一封电子邮件
这应该可以解决您的问题