Я хочу создать код обнаружения лица, который будет отправлять электронное письмо при обнаружении лица в видеопотоке. Функция электронной почты должна работать в фоновом режиме, в то время как основная функция распознает лица.
Прежде чем заняться обнаружением лиц, я попытался запустить функцию электронной почты в фоновом режиме (после того, как функция сохранения изображения передает изображение в электронную почту). Когда я запустил код, я получил 2 ошибки подтверждения:
AssertionError: can only start a process object created by current process
AssertionError: cannot start a process twice
Вот мой код (минус логин gmail) ...
import face_recognition
import cv2
import numpy as np
import time
import datetime
import multiprocessing
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
def send_email():
email_user = 'xxxxx@gmail.com'
email_password = 'xxxxxxxxxxxx'
email_send = 'xxxxxxx@gmail.com'
subject = 'Surveillance Project'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Hi there, sending this email from Python!'
msg.attach(MIMEText(body,'plain'))
filename='joe.jpg'
print("[INFO] - " + str(datetime.datetime.now()) + " - Image name - " + filename)
attachment =open(filename,'rb')
# create the attachment
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
print("[INFO] - " + str(datetime.datetime.now()) + " - Attachment Created")
# set up connection
print("[INFO] - " + str(datetime.datetime.now()) + " - Creating Email to Send")
msg.attach(part)
text = msg.as_string()
print("[INFO] - " + str(datetime.datetime.now()) + " - Setting up connection")
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
# send email
print("[INFO] - " + str(datetime.datetime.now()) + " - Email SENDING" )
server.sendmail(email_user,email_send,text)
server.quit()
print("[INFO] - " + str(datetime.datetime.now()) + " - Email SENT" )
def save_image():
p1.start()
print("ID of process p1: {}".format(p1.pid))
p1.join()
send_email()
def Main():
# process IDs
for i in range(30):
print(i)
if i % 10 == 0:
print("[INFO] - " + str(datetime.datetime.now()) + " - Email initated...")
save_image()
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == "__main__":
# printing main program process id
print("ID of main process: {}".format(os.getpid()))
p1 = multiprocessing.Process(target=send_email)
Main()