python實現調用攝像頭并拍照發(fā)郵箱
https://github.com/flygaga/camera
思路1、通過opencv調用攝像頭拍照保存圖像到本地
2、用email庫構造郵件內容,保存圖片以附件形式插入郵件內容
3、用smtplib庫發(fā)送郵件到指定郵箱
4、生成 .exe 文件
5、設置開機自啟(每次開機自動運行,啟動相機,拍下照片發(fā)送到指定郵箱)
導入工具import cv2 # pip install opencv-python -i {指定鏡像源} 控制攝像頭from email.mime.image imort MIMEImage #用來構造郵件內容的庫from email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport smtplib #發(fā)送郵件編譯環(huán)境
系統(tǒng):Windows10
軟件:Miniconda3-latest-Windows-x86_64
模塊:opencv-python smtplib numpy email pyinstaller
生成exe文件pyinstaller -F -w path/camera.py
設置開機自啟1.右擊exe 創(chuàng)建快捷方式
2.win+r 輸入以下命令 shell:startup 點擊確定打開一個文件夾
3.將生成的快捷文件復制到打開的文件中,下次開機exe程序就會自動啟動
python代碼實現調用攝像頭,并拍照發(fā)送郵件
主要代碼camera.py
import cv2from email.mime.image import MIMEImagefrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipart# import smtplib #發(fā)送郵件import smtplibfrom smtplib import SMTPimport timehost = ’smtp.qq.com’ #郵箱的接口port = ’25’ #端口pwd = ’neelrhh88******ch’ #授權碼sender = ’郵箱地址’ #發(fā)送方receiver = '郵箱地址' #接收方path = r’./’ #圖像保存路徑images = time.strftime('%Y-%m-%d-%H_%M_%S',time.localtime())def GetPicture(): ''' 拍照保存圖像 ''' #創(chuàng)建一個窗口camera cv2.namedWindow(’camera’,1) #’1’ 表示窗口不能隨意拖動 #調用攝像頭 cap = cv2.VideoCapture(0) ret,frame = cap.read() #讀取攝像頭內容 cv2.imwrite(path+images+'.jpg',frame) #保存到磁盤 #釋放攝像頭 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(path+images+’.jpg’,’rb’).read() image = MIMEImage(SendImageFile) image[’Content-Disposition’] = ’attachment;filename='people.jpg'’ msg.attach(image) return msg.as_string()def SendEmail(msg): ’’’ 發(fā)送郵件 :msg :郵件內容 :return ’’’ try:smtp = smtplib.SMTP_SSL(host,port) #創(chuàng)建一個郵件服務# smtp.connect(host)smtp.login(sender,pwd)smtp.sendmail(sender,receiver,msg)time.sleep(3)smtp.quit() #退出郵件服務 except smtplib.SMTPException as e:print('e')#實現開機自啟動#打包實現啟動 例:exe if __name__ == ’__main__’: # 1.拍照保存 GetPicture() # 2. 設置郵件格式 msg = SetMsg() # 3. 發(fā)送郵件 SendEmail(msg)
以上就是python實現調用攝像頭并拍照發(fā)郵箱的詳細內容,更多關于python 調用攝像頭的資料請關注好吧啦網其它相關文章!
相關文章:
