【发布时间】:2021-05-04 03:12:54
【问题描述】:
我正在构建这个脚本来通过网络摄像头检测我的脸,并让模型检测我是否戴着口罩。如果检测到的人脸没有戴口罩,则会向管理员发送一封电子邮件以提醒他们。 除电子邮件部分外,一切正常。它不断给出 SMTPAuthenticationError 说我的用户名和密码不正确,但它们是正确的。我可能做错了什么,我该如何解决这个问题? 我已通过设置授权 Google 授权不太安全的应用程序。我也禁用了两步验证,但我仍然收到此错误。
这是产生错误的代码的一部分。
if (label == "No Mask"):
# Throw a Warning Message to tell user to wear a mask if not wearing one. This will stay
#open and No Access will be given He/She wears the mask
messagebox.showwarning("Warning","Access Denied. Please wear a Face Mask")
# Send an email alert to the administrator if access denied/user not wearing face mask
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('kwerondaa@gmail.com','#####')
mail.sendmail('kwerondaa@gmail.com','kwerondaa@gmail.com',message)
mail.close
else:
pass
这是错误: SMTPAuthenticationError: (535, b'5.7.8 用户名和密码不被接受。了解详情\n5.7.8 https://support.google.com/mail/?p=BadCredentials v25sm14100155wmh.4 - gsmtp')
这是整个脚本文件:
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from imutils.video import VideoStream
import numpy as np
import imutils
import time
import cv2
import os
import tkinter
from tkinter import messagebox
import smtplib
# Initialize Tkinter
root = tkinter.Tk()
root.withdraw()
SUBJECT = "Subject"
TEXT = "One Visitor violated Face Mask Policy. See in the camera to recognize user. A Person has been detected without a face mask in the Hotel Lobby Area 9. Please Alert the authorities."
def find_face_and_mask(frame, detectFace, detectMask):
#get face dimensions and construct blob
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (224, 224),
(104.0, 177.0, 123.0))
#obtaining the face
detectFace.setInput(blob)
detections = detectFace.forward()
print(detections.shape)
faces = [] # faces
locs = [] #locations
preds = [] #predictions
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5: #setting min confidence
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
(startX, startY) = (max(0, startX), max(0, startY))
(endX, endY) = (min(w - 1, endX), min(h - 1, endY))
# extracting the face ROI and preprocessing
face = frame[startY:endY, startX:endX]
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
face = cv2.resize(face, (224, 224))
face = img_to_array(face)
face = preprocess_input(face)
#append faces and locations
faces.append(face)
locs.append((startX, startY, endX, endY))
# only make a predictions if at least one face was detected
if len(faces) > 0:
# making batch predictions on *all* faces at the same time rather than one-by-one predictions
faces = np.array(faces, dtype="float32")
preds = detectMask.predict(faces, batch_size=100)
return (locs, preds) #2-tuple of the face locations and their corresponding locations
# the serialized face detector model from disk
prototxtPath = r"face_detector\deploy.prototxt"
weightsPath = r"face_detector\res10_300x300_ssd_iter_140000.caffemodel"
detectFace = cv2.dnn.readNet(prototxtPath, weightsPath)
# face mask detector model from disk
detectMask = load_model("mask_det_final_model.h5")
vid_stream = VideoStream(src=0).start()
# loop over the frames from the video stream
while True:
frame = vid_stream.read()
frame = imutils.resize(frame, width=600)
# detect faces in the frame and determine if the detected face is wearing a face mask or not
(locs, preds) = find_face_and_mask(frame, detectFace, detectMask)
for (box, pred) in zip(locs, preds):
# unpack the bounding box and predictions
(startX, startY, endX, endY) = box
(mask, withoutMask) = pred
label = "Mask" if mask > withoutMask else "No Mask" #labels of the box
color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
if (label == "No Mask"):
# Throw a Warning Message to tell user to wear a mask if not wearing one. This will stay
#open and No Access will be given He/She wears the mask
messagebox.showwarning("Warning","Access Denied. Please wear a Face Mask")
# Send an email alert to the administrator if access denied/user not wearing face mask
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('kwerondaa@gmail.com','######')
mail.sendmail('kwerondaa@gmail.com','kwerondaa@gmail.com',message)
mail.close
else:
pass
# include the probability in the label
label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
# display the label and bounding box rectangle on the output frame
cv2.putText(frame, label, (startX, startY - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
cv2.imshow("Face mask detector Live webcam", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
vid_stream.stop()
【问题讨论】:
-
如果应用或网站不符合我们的安全标准,Google 可能会阻止任何试图通过它登录您帐户的人,stackoverflow.com/questions/16512592/…
-
那我现在该怎么办?
-
您可以在登录
kwerondaa@gmail.com帐户后在google.com/settings/security/lesssecureapps打开allow less secure apps选项,它应该可以工作。 (默认情况下这是禁用的,所以请您自担风险)
标签: python opencv smtp gmail smtp-auth