監控攝像頭安裝步驟:如何調用攝像頭

思路

1、通過opencv調用攝像頭拍照保存圖像到本地

2、用email庫構造郵件內容,保存圖片以附件形式插入郵件內容

3、用smtplib庫發送郵件到指定郵箱

4、生成 .exe 文件

5、設置開機自啟(每次開機自動運行,啟動相機,拍下照片發送到指定郵箱)

導入工具

import cv2 # pip install opencv-python -i {指定鏡像源} 控制攝像頭

from email.mime.image imort MIMEImage #用來構造郵件內容的庫

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

import smtplib #發送郵件

編譯環境

系統:Windows10

軟件:
Miniconda3-latest-Windows-x86_64

模塊:opencv-python smtplib numpy email pyinstaller

生成exe文件

pyinstaller -F -w path/camera.py

設置開機自啟

1.右擊exe 創建快捷方式

2.win+r 輸入以下命令 shell:startup 點擊確定打開一個文件夾

3.將生成的快捷文件複製到打開的文件中,下次開機exe程序就會自動啟動

python代碼實現調用攝像頭,並拍照發送郵件

主要代碼

camera.py

import cv2

from email.mime.image import MIMEImage

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

# import smtplib #發送郵件

import smtplib

from smtplib import SMTP

import time

host = ‘smtp.qq.com’ #郵箱的接口

port = ’25’ #端口

pwd = ‘neelrhh88******ch’ #授權碼

sender = ‘郵箱地址’ #發送方

receiver = “郵箱地址” #接收方

path = r’./’ #圖像保存路徑

images = time.strftime(“%Y-%m-%d-%H_%M_%S”,time.localtime())

def GetPicture():

“””

拍照保存圖像

“””

#創建一個窗口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):

”’

發送郵件

:msg :郵件內容

:return

”’

try:

smtp = smtplib.SMTP_SSL(host,port) #創建一個郵件服務

# 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. 發送郵件

SendEmail(msg)

以上就是python實現調用攝像頭並拍照發郵箱的詳細內容啦

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/214379.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-08 19:06
下一篇 2024-12-08 19:06

相關推薦

發表回復

登錄後才能評論