前言
今天這個案例,就是控制自己的攝像頭拍照,并且把拍下來的照片,通過郵件發到自己的郵箱里,
想完成今天的這個案例,只要記住一個重點:你需要一個攝像頭
思路
- 通過opencv呼叫攝像頭拍照保存影像本地
- 用email庫構造郵件內容,保存的影像以附件形式插入郵件內容
- 用smtplib庫發送郵件到指定郵箱
對于本篇文章有疑問的同學可以加【資料白嫖、解答交流群:910981974】
開始代碼
工具匯入
import time import cv2 # pip install opencv-python -i 鏡像源網址 from email.mime.image import MIMEImage # 用來構造郵件內容的庫 from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib # 發送郵件
呼叫攝像頭,保存圖片
拍照呢,是用手機的攝像頭,軟體用的是:IP攝像頭(安卓),因為在同一個局域網內,打開APP,里面出現的網址就是攝像頭的地址
def GetPicture(): """ 拍照保存影像 :return: """ # 創建一個視窗 cv2.namedWindow('camera', 1) # 呼叫攝像頭 IP攝像頭APP video = "http://admin:[email protected]:8081/video" cap = cv2.VideoCapture(video) while True: success, img = cap.read() cv2.imshow("camera", img) # 按鍵處理 key = cv2.waitKey(10) if key == 27: # esc break if key == 32: # 空格 fileaname = 'frames.jpg' cv2.imwrite(fileaname, img) # 釋放攝像頭 cap.release() # 關閉視窗 cv2.destroyWindow("camera")
運行代碼,就會出現效果

創建一個函式,設定一下我的郵件內容
def SetMsg(): """ 郵件格式設定 :return: """ msg = MIMEMultipart('mixed') # 標題 msg['Subject'] = '小姐姐照片' msg['From'] = sender # 發送方郵箱 msg['To'] = receiver # 接收方郵箱 # 郵件正文 text = '你要的小姐姐照片到了,請接收' text_plain = MIMEText(text, 'plain', 'utf-8') # 正文轉碼 msg.attach(text_plain) # 圖片附件 SendImageFile = open('D:/控制攝像頭拍照并發送郵件/frames.jpg', 'rb').read() image = MIMEImage(SendImageFile) # 將收件人看見的附件照片名稱改為people.png. image['Content-Disposition'] = 'attachment; filename = "people.png"' msg.attach(image) return msg.as_string()
郵件埠設定
授權碼可以在這里領取

# 授權碼 pwd = "******" # 最好寫自己的 # 服務器介面 host = 'smtp.163.com' port = 25 sender = '[email protected]' # 最好寫自己的 receiver = '[email protected]' # 最好寫自己的
發送郵件功能
def SendEmail(msg): """ 發送郵件 :param msg:郵件內容 :return: """ smtp = smtplib.SMTP() smtp.connect(host,port=25) smtp.login(sender, pwd) smtp.sendmail(sender, receiver, msg) time.sleep(2) smtp.quit()
進行封裝
if __name__ == '__main__': # 1.拍照保存 GetPicture() # 2.設定郵件格式 msg = SetMsg() # 3.發送郵件 SendEmail(msg)
運行代碼,演示效果
先拍照

發送到了郵箱


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458216.html
標籤:其他
