/
bbg.py
executable file
·154 lines (127 loc) · 3.98 KB
/
bbg.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
import cv
import motion
import os
import Image
import logging
import freenect
import numpy as np
from datetime import datetime
from time import sleep
import smtplib
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.Utils import formatdate
HOST = "smtp.gmail.com:587"
EMAIL_FROM = 'spawnedc@gmail.com'
EMAIL_TO = ['spawnedc@gmail.com']
EMAIL_SUBJECT = 'Evde biseyler oluyo!'
USERNAME = 'spawnedc@gmail.com'
PASSWORD = ''
COMMASPACE = ', '
# Kinect constants:
# DEPTH_10BIT
# DEPTH_10BIT_PACKED
# DEPTH_11BIT
# DEPTH_11BIT_PACKED
# DEVICE_AUDIO
# DEVICE_CAMERA
# DEVICE_MOTOR
# LED_BLINK_GREEN
# LED_BLINK_RED_YELLOW
# LED_GREEN
# LED_OFF
# LED_RED
# LED_YELLOW
# RESOLUTION_HIGH
# RESOLUTION_LOW
# RESOLUTION_MEDIUM
# VIDEO_BAYER
# VIDEO_IR_10BIT
# VIDEO_IR_10BIT_PACKED
# VIDEO_IR_8BIT
# VIDEO_RGB
# VIDEO_YUV_RAW
# VIDEO_YUV_RGB
class BBG:
capture_folder = 'captures'
filename_format = '%s.png'
datetime_format = '%d-%m-%y-%H-%M-%S'
sensitivity = 0.08
kinect = None
def __init__(self, cam=-1):
# Initialize freenect and get the context
print 'Initalize kinect'
context = freenect.init()
# Open the device and get the device
print 'Open device'
self.kinect = freenect.open_device(context, 0)
# Turn the led off
print 'Turning the led off'
freenect.set_led(self.kinect, freenect.LED_OFF)
# Close the device
print 'Closing the device'
freenect.close_device(self.kinect)
def capture_image(self, filename=None):
if not filename:
filename = datetime.now().strftime(self.datetime_format)
print 'Setting filename'
filename = os.path.join(self.capture_folder, self.filename_format % filename)
print 'Getting sync rgb video'
rgb, _ = freenect.sync_get_video()
print 'Creating numpy array'
na = np.array(rgb[::1,::-1,::-1])
print 'Fetching frame'
frame = cv.fromarray(na)
print 'Saving actual image'
cv.SaveImage(filename, frame)
return (filename, frame)
def get_diff(self, delay=0.5):
print 'Capturing first image'
filename1, frame1 = self.capture_image('1')
print 'Sleeping for %s' % delay
sleep(delay)
print 'Capturing second image'
filename2, frame2 = self.capture_image('2')
print 'Setting images'
self.image1 = Image.open(filename1)
self.image2 = Image.open(filename2)
print 'Getting image diff'
self.diff = motion.images_diff(self.image1, self.image2)
return self.diff
def send_email(self, im):
msg = MIMEMultipart()
msg["From"] = EMAIL_FROM
msg["To"] = COMMASPACE.join(EMAIL_TO)
msg["Subject"] = EMAIL_SUBJECT
msg['Date'] = formatdate(localtime=True)
part = MIMEBase('application', "octet-stream")
part.set_payload( open(im.filename, "rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(im.filename))
msg.attach(part)
server = smtplib.SMTP(HOST)
server.starttls()
server.login(USERNAME, PASSWORD)
try:
server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
server.close()
except Exception, e:
error_msg = "Unable to send email. Error: %s" % str(e)
print error_msg
def start(self):
print 'Starting loop'
while True:
print 'Getting diff'
diff = self.get_diff()
if diff > self.sensitivity:
print 'Motion detected'
logging.warning('Motion detected! (%s)' % diff)
self.send_email(self.image2)
filename, frame = self.capture_image()
else:
print 'Nothing yet. (%s)' % diff
if __name__ == '__main__':
bbg = BBG()
bbg.start()