python實(shí)現(xiàn)調(diào)用攝像頭并拍照發(fā)郵箱
https://github.com/flygaga/camera
思路1、通過(guò)opencv調(diào)用攝像頭拍照保存圖像到本地
2、用email庫(kù)構(gòu)造郵件內(nèi)容,保存圖片以附件形式插入郵件內(nèi)容
3、用smtplib庫(kù)發(fā)送郵件到指定郵箱
4、生成 .exe 文件
5、設(shè)置開(kāi)機(jī)自啟(每次開(kāi)機(jī)自動(dòng)運(yùn)行,啟動(dòng)相機(jī),拍下照片發(fā)送到指定郵箱)
導(dǎo)入工具import cv2 # pip install opencv-python -i {指定鏡像源} 控制攝像頭from email.mime.image imort MIMEImage #用來(lái)構(gòu)造郵件內(nèi)容的庫(kù)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
設(shè)置開(kāi)機(jī)自啟1.右擊exe 創(chuàng)建快捷方式
2.win+r 輸入以下命令 shell:startup 點(diǎn)擊確定打開(kāi)一個(gè)文件夾
3.將生成的快捷文件復(fù)制到打開(kāi)的文件中,下次開(kāi)機(jī)exe程序就會(huì)自動(dòng)啟動(dòng)
python代碼實(shí)現(xiàn)調(diào)用攝像頭,并拍照發(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’ #授權(quán)碼sender = ’郵箱地址’ #發(fā)送方receiver = '郵箱地址' #接收方path = r’./’ #圖像保存路徑images = time.strftime('%Y-%m-%d-%H_%M_%S',time.localtime())def GetPicture(): ''' 拍照保存圖像 ''' #創(chuàng)建一個(gè)窗口camera cv2.namedWindow(’camera’,1) #’1’ 表示窗口不能隨意拖動(dòng) #調(diào)用攝像頭 cap = cv2.VideoCapture(0) ret,frame = cap.read() #讀取攝像頭內(nèi)容 cv2.imwrite(path+images+'.jpg',frame) #保存到磁盤 #釋放攝像頭 cap.release() #關(guān)閉窗口 cv2.destroyWindow('camera')def SetMsg(): ’’’ 設(shè)置郵件格式 :return: ’’’ msg = MIMEMultipart(’mixed’) #標(biāo)題 msg[’Subject’] = ’電腦已開(kāi)機(jī)’ msg[’From’] = sender msg[’To’] = receiver #郵件正文內(nèi)容 text = ’電腦已開(kāi)機(jī),請(qǐng)查收?qǐng)D片確認(rèn)是否為本人’ text_plain = MIMEText(text,’plain’,’utf-8’) #正文轉(zhuǎn)碼 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 :郵件內(nèi)容 :return ’’’ try:smtp = smtplib.SMTP_SSL(host,port) #創(chuàng)建一個(gè)郵件服務(wù)# smtp.connect(host)smtp.login(sender,pwd)smtp.sendmail(sender,receiver,msg)time.sleep(3)smtp.quit() #退出郵件服務(wù) except smtplib.SMTPException as e:print('e')#實(shí)現(xiàn)開(kāi)機(jī)自啟動(dòng)#打包實(shí)現(xiàn)啟動(dòng) 例:exe if __name__ == ’__main__’: # 1.拍照保存 GetPicture() # 2. 設(shè)置郵件格式 msg = SetMsg() # 3. 發(fā)送郵件 SendEmail(msg)
以上就是python實(shí)現(xiàn)調(diào)用攝像頭并拍照發(fā)郵箱的詳細(xì)內(nèi)容,更多關(guān)于python 調(diào)用攝像頭的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序2. js開(kāi)發(fā)中的頁(yè)面、屏幕、瀏覽器的位置原理(高度寬度)說(shuō)明講解(附圖)3. ASP動(dòng)態(tài)include文件4. CSS百分比padding制作圖片自適應(yīng)布局5. vue跳轉(zhuǎn)頁(yè)面常用的幾種方法匯總6. 不要在HTML中濫用div7. XML入門的常見(jiàn)問(wèn)題(三)8. XML 非法字符(轉(zhuǎn)義字符)9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. CSS清除浮動(dòng)方法匯總
