forked from kstevica/raspberry_pi_camera_alarm
/
camera_alarm.py
69 lines (56 loc) · 2.05 KB
/
camera_alarm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import subprocess
from SimpleCV import Camera, Display, Image
import time
import numpy
import os
def send_email(percentage):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
# Prepare actual message
msg = MIMEMultipart()
msg['From'] = "kstevica@gmail.com" # change to your mail
msg['To'] = "kstevica@gmail.com" # change to your mail
msg['Subject'] = "RPi Camera Alarm!"
imgcv = Image("image.jpg")
imgcv.save("imagesend.jpg", quality=50) # reducing quality of the image for smaller size
img1 = MIMEImage(open("imagesend.jpg","rb").read(), _subtype="jpg")
img1.add_header('Content-Disposition', 'attachment; filename="image.jpg"')
msg.attach(img1)
part = MIMEText('text', "plain")
part.set_payload(("Raspberry Pi camera alarm activated with level {:f}").format(percentage))
msg.attach(part)
try:
server = smtplib.SMTP("mail.htnet.hr", 25) #change to your SMTP provider
server.ehlo()
server.starttls()
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print 'Successfully sent the mail'
except smtplib.SMTPException as e:
print(e)
def imageDifference():
if not os.path.isfile("image.jpg"):
subprocess.call("raspistill -n -t 1 -w 640 -h 480 -e jpg -o image.jpg", shell=True)
time.sleep(0.5)
img = Image("image.jpg")
time.sleep(0.2)
subprocess.call("raspistill -n -t 1 -w 640 -h 480 -e jpg -o image.jpg", shell=True)
img2 = Image("image.jpg")
diffimg = img2 - img
matrix = diffimg.getNumpy()
flat = matrix.flatten()
mean = matrix.mean()
return mean
try:
while True:
percent = imageDifference()
print percent
if percent>6.2: # 6.2 is a good level to get activated during the day
send_email(percent)
print ("Camera alarm activated at level {:f}").format(percent)
time.sleep(60)
time.sleep(1.5)
except KeyboardInterrupt:
print "Exit"