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郵箱通訊協(xié)議SMTP第三方服務(wù)器實現(xiàn)的AI人臉識別項目\033[0m\n") print("\033[33m實現(xiàn)原理是利用Pycharm的email庫實現(xiàn)smtp協(xié)議連接 time實現(xiàn)每延遲10s執(zhí)行一次本代碼\033[0m") print('\033[34m在每次循環(huán)開始時,會將最新的郵件與上一次處理的郵件進行比較,如果是同一封郵件則跳過處理,否則進行處理并發(fā)送結(jié)果到目標郵箱。\033[0m\n\033[32m然后,更新上一次處理的郵件ID,并休眠10秒后再次執(zhí)行循環(huán)\033[0m ') print() print() print('\033[35m欲討論請聯(lián)系軟件作者 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服務(wù)器 mail = imaplib.IMAP4_SSL("imap.qq.com") # 登錄QQ郵箱 mail.login("312313131@qq.com", "smtp授權(quán)碼") # 選擇郵箱中的收件箱 mail.select("inbox") # 搜索最新的郵件 result, data = mail.search(None, "ALL") latest_email_id = data[0].split()[-1] # 如果是第一次執(zhí)行或者有新郵件到達 if last_email_id is None or latest_email_id != last_email_id: # 獲取最新郵件的內(nèi)容 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)) # 關(guān)閉連接 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" """ 調(diào)用人臉檢測 """ 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' # 發(fā)送結(jié)果到目標郵箱 smtp_server = "smtp.qq.com" smtp_port = 465 sender_email = "QQ@qq.com" sender_password = "授權(quán)碼" receiver_email = "QQ@qq.com" message = MIMEMultipart() message["Subject"] = "人臉檢測結(jié)果" 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結(jié)果已發(fā)送到目標郵箱",'這是第',i,'次 個10s后的識別結(jié)果\033[0m') # 更新上一次處理的郵件ID last_email_id = latest_email_id # 休眠10秒 time.sleep(10) i += 1
文章來源地址http://www.zghlxwxcb.cn/news/detail-771058.html
文章來源:http://www.zghlxwxcb.cn/news/detail-771058.html
到了這里,關(guān)于基于smtp協(xié)議的QQ郵箱人臉識別AI(識別算法為百度 AIP)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!