import time import imaplib import email import os from aip import AipFace import base64 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage i = 0 print("\033[31m这里是Fionne的基于QQ邮箱通讯协议SMTP第三方服务器实现的AI人脸识别项目\033[0m\n") print("\033[33m实现原理是利用Pycharm的email库实现smtp协议连接 time实现每延迟10s执行一次本代码\033[0m") print('\033[34m在每次循环开始时,会将最新的邮件与上一次处理的邮件进行比较,如果是同一封邮件则跳过处理,否则进行处理并发送结果到目标邮箱。\033[0m\n\033[32m然后,更新上一次处理的邮件ID,并休眠10秒后再次执行循环\033[0m ') print() print() print('\033[35m欲讨论请联系软件作者 Fionne 1798576741@qq.com\033[0m') """ 你的 APPID AK SK """ APP_ID = '44568385' API_KEY = 'TnouKpQiiw4VhpxBuZ7GcYAq' SECRET_KEY = 'AypjgUGqVZHgwZWEA7MDPs5rAjaHBNiI'//百度AIP算法识别 client = AipFace(APP_ID, API_KEY, SECRET_KEY) # 上一次处理的邮件ID last_email_id = None while True: # 连接到QQ邮箱的IMAP服务器 mail = imaplib.IMAP4_SSL("imap.qq.com") # 登录QQ邮箱 mail.login("312313131@qq.com", "smtp授权码") # 选择邮箱中的收件箱 mail.select("inbox") # 搜索最新的邮件 result, data = mail.search(None, "ALL") latest_email_id = data[0].split()[-1] # 如果是第一次执行或者有新邮件到达 if last_email_id is None or latest_email_id != last_email_id: # 获取最新邮件的内容 result, data = mail.fetch(latest_email_id, "(RFC822)") raw_email = data[0][1] email_message = email.message_from_bytes(raw_email) # 遍历邮件的附件 for part in email_message.walk(): if part.get_content_maintype() == "multipart": continue if part.get("Content-Disposition") is None: continue # 保存附件到本地 filename = part.get_filename() if filename: filepath = os.path.join(r"C:\Users\路径\PycharmProjects\pythonProject", filename) with open(filepath, "wb") as f: f.write(part.get_payload(decode=True)) # 关闭连接 mail.logout() file_path = r"C:\Users\路径\PycharmProjects\pythonProject\1227_1.jpg" with open(file_path, 'rb') as f: image = base64.b64encode(f.read()).decode('utf-8') imageType = "BASE64" """ 调用人脸检测 """ result = client.detect(image, imageType) if 'result' in result and result['result'] is not None: face_num = result['result']['face_num'] result_text = "图中有 {} 个人脸\n Fionne \n 开源链接直达:http://47.115.230.137//wordpress".format(face_num) else: result_text = '图中没有人脸\n Fionne \n 开源链接直达:http://47.115.230.137//wordpress' # 发送结果到目标邮箱 smtp_server = "smtp.qq.com" smtp_port = 465 sender_email = "QQ@qq.com" sender_password = "授权码" receiver_email = "QQ@qq.com" message = MIMEMultipart() message["Subject"] = "人脸检测结果" message["From"] = sender_email message["To"] = receiver_email text = MIMEText(result_text) message.attach(text) with open(file_path, 'rb') as f: image = MIMEImage(f.read()) image.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_path)) message.attach(image) with smtplib.SMTP_SSL(smtp_server, smtp_port) as server: server.login(sender_email, sender_password) server.sendmail(sender_email, receiver_email, message.as_string()) print("\033[36m结果已发送到目标邮箱",'这是第',i,'次 个10s后的识别结果\033[0m') # 更新上一次处理的邮件ID last_email_id = latest_email_id # 休眠10秒 time.sleep(10) i += 1
文章来源地址https://www.toymoban.com/news/detail-771058.html
文章来源:https://www.toymoban.com/news/detail-771058.html
到了这里,关于基于smtp协议的QQ邮箱人脸识别AI(识别算法为百度 AIP)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!