-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotion.py
61 lines (57 loc) · 1.94 KB
/
motion.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
import Image
from camera import camera
from subprocess import call
class motion:
currentImage = Image.new('RGB',(150,150),"black")
prevImage = Image.new('RGB',(150,150),"black")
alarmEndPoint = '/entrance/?format=json'
camera = camera()
width = 0
height = 0
totalMovement = 0
def __init__(self):
self.setPrevImage()
self.setWidthHeight()
def movement(self):
self.setCurrentImage()
currentImagePixels = self.currentImage.load()
prevImagePixels = self.prevImage.load()
x=0
y=0
while y<self.height:
while x<self.width:
p1,p2,p3 = currentImagePixels[x,y]
c1,c2,c3 = prevImagePixels[x,y]
difference = (p1 - c1) + (p2 - c2) + (p3 - c3)
if difference > 20:
self.totalMovement +=1
x+=1
x=0
y+=1
print self.totalMovement
self.prevImage = self.currentImage
if self.totalMovement > 500:
self.totalMovement = 0
return True
else:
self.totalMovement = 0
return False
def setCurrentImage(self):
self.camera.takePicture('currentImage.jpg','150','150')
self.currentImage=Image.open("/home/pi/Desktop/currentImage.jpg")
def setPrevImage(self):
self.camera.takePicture('prevImage.jpg','150','150')
self.prevImage = Image.open("/home/pi/Desktop/prevImage.jpg")
def setWidthHeight(self):
self.width,self.height = self.prevImage.size
def registerMotionAlarm(self,webServer)
data = {'raspberry_pi_id':raspberryPi.getId(),
'status':False,
'gpio':30,
'label':'Motion Alarm',
'entrance-type' :'door'
}
if webServer.postToDatabase(data,self.alarmEndPoint):
return 'Motion Alarm successfully registered'
else:
return 'Probelm registering Motion Alarm'