python實(shí)現(xiàn)調(diào)用攝像頭并拍照發(fā)郵箱
https://github.com/flygaga/camera
思路1、通過opencv調(diào)用攝像頭拍照保存圖像到本地
2、用email庫構(gòu)造郵件內(nèi)容,保存圖片以附件形式插入郵件內(nèi)容
3、用smtplib庫發(fā)送郵件到指定郵箱
4、生成 .exe 文件
5、設(shè)置開機(jī)自啟(每次開機(jī)自動(dòng)運(yùn)行,啟動(dòng)相機(jī),拍下照片發(fā)送到指定郵箱)
導(dǎo)入工具import cv2 # pip install opencv-python -i {指定鏡像源} 控制攝像頭from email.mime.image imort MIMEImage #用來構(gòu)造郵件內(nèi)容的庫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è)置開機(jī)自啟1.右擊exe 創(chuàng)建快捷方式
2.win+r 輸入以下命令 shell:startup 點(diǎn)擊確定打開一個(gè)文件夾
3.將生成的快捷文件復(fù)制到打開的文件中,下次開機(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’] = ’電腦已開機(jī)’ msg[’From’] = sender msg[’To’] = receiver #郵件正文內(nèi)容 text = ’電腦已開機(jī),請查收圖片確認(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)開機(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)用攝像頭的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解2. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解3. Yii2.0引入CSS,JS文件方法4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析5. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. vue使用webSocket更新實(shí)時(shí)天氣的方法7. 淺談python出錯(cuò)時(shí)traceback的解讀8. android studio 打包自動(dòng)生成版本號與日期,apk輸入路徑詳解9. Nginx+php配置文件及原理解析10. JavaMail 1.4 發(fā)布
