-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_detector.py
46 lines (35 loc) · 1.13 KB
/
motion_detector.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
#!/usr/bin/env python
import threading
class MotionDetector(object):
def __init__(self, video_source, algo, threshold, loglevel):
self.looping = False
self.on_motion_status_updated = []
self.video_source = video_source
self.algo = algo
self.threshold = threshold
self.loglevel = loglevel
self.loop_thread = threading.Thread(target=self.loop)
def start(self):
self.video_source.setup()
self.looping = True
for i in range(0,20):
self.video_source.update_frame()
self.loop_thread.start()
def stop(self):
self.looping = False
self.loop_thread.join()
self.video_source.teardown()
def loop(self):
while(self.looping):
self.video_source.update_frame()
mask = self.algo.apply(self.video_source.frame)
total = sum(map(sum, mask))
active = total >= self.threshold
for listener in self.on_motion_status_updated:
listener(active, mask)
# debug
if self.loglevel >= 1 and active:
print "ACTIVE %d" % total
elif self.loglevel == 2:
print total
self.looping = not self.video_source.final_image