forked from rdachere/whosthere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognizer.py
94 lines (67 loc) · 3.04 KB
/
recognizer.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
#! /usr/bin/env python
# Read the output of an Arduino which is constantly printing sensor output.
# See also
# http://www.arcfn.com/2009/06/arduino-sheevaplug-cool-hardware.html
# http://shallowsky.com
import sys, serial, threading
from subprocess import call
from time import time, sleep
import datetime
BASEDIR_FOR_GIT_IMAGES = "./snaps/"
MAX_TRIGGER_DISTANCE = 30 # inches
MIN_TRIGGER_DISTANCE = 0 # inches
VALID_PIC_THRESHOLD = 5 # weak attempt at mitigating "noise" (butterfly/moth/squirrel)
class Arduino(threading.Thread) :
def run(self, interactive) :
global BASEDIR_FOR_GIT_IMAGES, MIN_TRIGGER_DISTANCE, MAX_TRIGGER_DISTANCE
# Port may vary, so look for it:
baseport = "/dev/ttyACM0"
self.ser = serial.Serial(baseport, 115200, timeout=800)
if not self.ser :
print "Couldn't open a serial port"
sys.exit(1)
print "Opened ", baseport
self.ser.flushInput()
valid_pic_count = 0
while True :
data = self.ser.readline().strip()
if data :
if interactive :
print data
if int(data) > MIN_TRIGGER_DISTANCE and int(data) < MAX_TRIGGER_DISTANCE:
print "****** DETECTED AN OBJECT AT -- ", data ,"-- INCHES ****** "
valid_pic_count += 1
if valid_pic_count == VALID_PIC_THRESHOLD:
#build snapshot filename with date and timestamp
now = datetime.datetime.now()
snapshot_filename = "visitor-%d:%d:%d-%d:%d:%d.jpg" % (now.year, now.month, now.day, now.hour, now.minute, now.second)
#take a photo and reset valid_pic_count
take_snapshot_cmd = "fswebcam "+ BASEDIR_FOR_GIT_IMAGES + snapshot_filename
print "\n\n************************** TAKING SNAPSHOT: %s ************************ \n\n" % take_snapshot_cmd
snapshot_return_code = call(take_snapshot_cmd, shell=True)
#print "Snapshot return code is ", snapshot_return_code
print "\n\n************************** DOING GIT STUFF.... ***********************\n\n"
do_git_stuff = "git add " + BASEDIR_FOR_GIT_IMAGES + \
"; git commit -m \"another visitor\" " + BASEDIR_FOR_GIT_IMAGES + \
"; git push"
git_cmds_return_code = call(do_git_stuff, shell=True)
#print "Git stuff return code is ", git_cmds_return_code
sleep(3)
#send sms with url to the latest visitor photo
print "\n\n**************************** CALLING SEND_SMS COMMAND.... **********************\n\n"
send_sms_cmd = "python ./send_sms.py -u https://github.com/rdachere/whosthere/blob/master/snaps/" + snapshot_filename
sms_url_rc = call(send_sms_cmd, shell=True)
valid_pic_count = 0
#sleep for a short while before checking again
sleep(20)
print "Done sleeping - I'm awake again!!!! "
continue
else:
print data
# If -i, run in interactive mode
if len (sys.argv) > 1 and sys.argv[1] == '-i' :
interactive = True
else :
interactive = False
arduino = Arduino()
arduino.run(interactive)