-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDragon.py
More file actions
274 lines (215 loc) · 6.98 KB
/
Dragon.py
File metadata and controls
274 lines (215 loc) · 6.98 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
from StringIO import StringIO
import pycurl
import signal, sys
import cv2
from subprocess import call
import piconzero as pz
from Queue import Queue
from threading import Thread
import datetime,time
import dropbox, os
# Ref https://www.troyfawkes.com/learn-python-multithreading-queues-basics/
# https://docs.python.org/2/library/queue.html
class ServoTask:
def __init__(self, angle, delay):
self.angle = angle
self.delay = delay
def run(self):
# print self.angle
pz.setOutput(0, self.angle)
time.sleep(self.delay)
class LEDTask:
def __init__(self, rgb, delay):
self.rgb = rgb
self.delay = delay
def run(self):
# print self.rgb.red, self.rgb.green, self.rgb.blue
pz.setOutput(1, self.rgb.red)
pz.setOutput(2, self.rgb.green)
pz.setOutput(3, self.rgb.blue)
time.sleep(self.delay)
class RGB:
def __init__(self, red, green, blue):
self.red = red
self.green = green
self.blue = blue
def processq(q):
while True:
task = q.get()
task.run()
q.task_done()
def call_api(url):
r = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.CONNECTTIMEOUT, 10)
c.setopt(c.TIMEOUT, 60)
c.setopt(c.WRITEFUNCTION, r.write)
c.perform()
c.close()
return r.getvalue()
def get_IFTTTkey():
with open('IFTTTKey.conf', 'r') as f:
key = f.readline()
f.close()
return key
# Have created a Dropbox App folder with the DropBox Developer console
# https://www.dropbox.com/developers/apps
# Get the access token from a file, as created by above console
def get_dropboxkey():
with open('DropBoxKey.conf', 'r') as f:
key = f.readline()
f.close()
return key
def notify(numDragons, key):
url = "https://maker.ifttt.com/trigger/DragonDetected/with/key/" + key + "?value1=" + str(numDragons)
r = call_api(url)
print r
# Simplified upload from the updown.py example
# https://github.com/dropbox/dropbox-sdk-python/blob/master/example/updown.py
def upload(sourcefile, destfile, overwrite=False):
"""Upload a file.
Return the request response, or None in case of error.
"""
mode = (dropbox.files.WriteMode.overwrite
if overwrite
else dropbox.files.WriteMode.add)
mtime = os.path.getmtime(sourcefile)
with open(sourcefile, 'rb') as f:
data = f.read()
try:
res = dbx.files_upload(
data, destfile, mode,
client_modified=datetime.datetime(*time.gmtime(mtime)[:6]),
mute=True)
except dropbox.exceptions.ApiError as err:
print('*** API error', err)
return None
print 'Uploaded', res.name.encode('utf8')
return res
def purge_old_files(daystokeep):
files = 0
print 'Deleting dropbox files older than {} days'.format(daystokeep)
now = datetime.datetime.now()
for entry in dbx.files_list_folder('').entries:
delta = now - entry.server_modified
if delta.days > daystokeep:
files += 1
print 'Deleting {} from {}'.format(entry.name, entry.server_modified)
try:
dbx.files_delete('/' + entry.name)
except dropbox.exceptions.ApiError as err:
print('*** API error', err)
return None
if files > 0:
print 'Deleted {} files'.format(files)
def capture():
filename = "CaptureInput" + datetime.datetime.now().isoformat().replace(":", "") + ".jpeg"
cmdline = "streamer -c /dev/video0 -b 32 -f jpeg -o " + filename
call(cmdline, shell=True)
return cv2.imread(filename)
def detect(img):
dclassifier = cv2.CascadeClassifier('DragonClassifier50x50-5v2.xml')
detected = 0
dragons = dclassifier.detectMultiScale(img)
for (x, y, w, h) in dragons:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
detected += 1
newfilename = ""
if detected > 0:
newfilename = "Detected" + datetime.datetime.now().isoformat().replace(":", "") + ".jpg"
cv2.imwrite(newfilename, img)
print(str(detected) + " dragons")
print ""
return detected, newfilename
def sensor_activated():
return pz.readInput(0)
def waitforaction():
# Wait till both queues are empty
qLED.join()
qServo.join()
def deactivate_defences():
print "Looking for Dragons"
qLED.put(LEDTask(RGB(0, 50, 0), 0.5))
waitforaction()
def activated():
# Flash blue
print "Motion detected"
qServo.put(ServoTask(120, 0.2))
qLED.put(LEDTask(RGB(0, 0, 50), 0.5))
qLED.put(LEDTask(RGB(0, 0, 0), 0.5))
qLED.put(LEDTask(RGB(0, 0, 50), 0.5))
qLED.put(LEDTask(RGB(0, 0, 0), 0.5))
qLED.put(LEDTask(RGB(0, 0, 50), 0.5))
waitforaction()
def activate_defences():
print "Dragon detected"
# Three sword blows
for f in range(1, 4, 1):
qServo.put(ServoTask(30, 0.5))
for i in range(30, 120, 2):
qServo.put(ServoTask(i, 0.01))
greenlim = 30
for f in range(1, 8, 1):
qLED.put(LEDTask(RGB(80, greenlim, 0), 0.5))
for i in range(greenlim, 0, -1):
qLED.put(LEDTask(RGB(80, i, 0), 0.01))
for i in range(0, greenlim, 1):
qLED.put(LEDTask(RGB(80, i, 0), 0.01))
# And back to red
for i in range(greenlim, 0, -1):
qLED.put(LEDTask(RGB(80, i, 0), 0.01))
waitforaction()
# Handle exit and kill from OS
def set_exit_handler(func):
signal.signal(signal.SIGTERM, func)
def on_exit(sig, func=None):
print "Exiting DragonDetector"
pz.cleanup()
sys.exit(1)
def initialise():
# Setup
pz.init()
pz.setOutputConfig(0, 2) # set output 0 to Servo
pz.setOutputConfig(1, 1) # set output 1 to PWM
pz.setOutputConfig(2, 1) # set output 2 to PWM
pz.setOutputConfig(3, 1) # set output 3 to PWM
pz.setInputConfig(0, 0) # set input 1 to digital
# Setup Action queues
global qLED
qLED = Queue(maxsize=0)
global qServo
qServo = Queue(maxsize=0)
workerLED = Thread(target=processq, args=(qLED,))
workerLED.setDaemon(True)
workerLED.start()
workerServo = Thread(target=processq, args=(qServo,))
workerServo.setDaemon(True)
workerServo.start()
# Initialise dropbox
global dbx
dbx = dropbox.Dropbox(get_dropboxkey())
def main():
try:
initialise()
while True:
deactivate_defences()
time.sleep(10) # Avoid rapid retriggering
while not sensor_activated():
time.sleep(0.2)
activated()
image = capture()
dragons, outfile = detect(image)
if dragons > 0:
notify(dragons, get_IFTTTkey())
upload(outfile, '/' + outfile, True)
activate_defences()
purge_old_files(30) # Keep 30 days of history
except KeyboardInterrupt:
print 'Stopping...'
on_exit(None)
# Run program
if __name__ == '__main__':
set_exit_handler(on_exit)
sys.exit(main())