forked from sean-obrien/rpotter
-
Notifications
You must be signed in to change notification settings - Fork 8
/
rpotter.py
248 lines (221 loc) · 9.12 KB
/
rpotter.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
_\
\
O O-O
O O
O
Raspberry Potter
Ollivander - Version 0.2
Use your own wand or your interactive Harry Potter wands to control the IoT.
Copyright (c) 2016 Sean O'Brien. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import io
import numpy as np
import argparse
import cv2
from cv2 import *
#import picamera #only needed if using RPi camera module
import threading
import sys
import math
import time
import pigpio
import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)
GPIOS=32
MODES=["INPUT", "OUTPUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"]
pi = pigpio.pi()
#NOTE pins use BCM numbering in code. I reference BOARD numbers in my articles - sorry for the confusion!
#pin for Powerswitch (Lumos,Nox)
switch_pin = 23
pi.set_mode(switch_pin,pigpio.OUTPUT)
#pin for Particle (Nox)
nox_pin = 24
pi.set_mode(nox_pin,pigpio.OUTPUT)
#pin for Particle (Incendio)
incendio_pin = 22
pi.set_mode(incendio_pin,pigpio.OUTPUT)
#pin for Trinket (Colovario)
trinket_pin = 12
pi.set_mode(trinket_pin,pigpio.OUTPUT)
print("Initializing point tracking")
# Parameters
lk_params = dict( winSize = (15,15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
blur_params = (4,4)
dilation_params = (5, 5)
movment_threshold = 80
print("START switch_pin ON for pre-video test")
pi.write(nox_pin,0)
pi.write(incendio_pin,0)
pi.write(switch_pin,1)
# start capturing
cv2.namedWindow("Raspberry Potter")
#use next line for webcam
cam = cv2.VideoCapture(-1)
#use next line for RPi camera module
#cam = cv2.VideoCapture(0)
cam.set(3, 640)
cam.set(4, 480)
def Spell(spell):
#clear all checks
ig = [[0] for x in range(15)]
#Invoke IoT (or any other) actions here
cv2.putText(mask, spell, (5, 25),cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,0,0))
#renamed spell Aguamenti but kept Incendio naming - fix later
if (spell=="Aguamenti"):
print("incendio_pin ON")
pi.write(incendio_pin,1)
#keep fountain on for 7sec
threading.Timer(7, StopAguamenti).start()
elif (spell=="Lumos"):
print("switch_pin ON")
pi.write(switch_pin,1)
print("nox_pin OFF")
pi.write(nox_pin,0)
print("incendio_pin OFF")
pi.write(incendio_pin,0)
elif (spell=="Nox"):
print("switch_pin OFF")
pi.write(switch_pin,0)
print("nox_pin ON")
pi.write(nox_pin,1)
print("incendio_pin OFF")
pi.write(incendio_pin,0)
print("CAST: %s" %spell)
cv2.putText(frame, spell, (10,30), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
def StopAguamenti():
#turns off fountain
pi.write(incendio_pin,0)
def IsGesture(a,b,c,d,i):
print("point: %s" % i)
#look for basic movements - TODO: trained gestures
if ((a<(c-5))&(abs(b-d)<2)):
ig[i].append("left")
elif ((c<(a-5))&(abs(b-d)<2)):
ig[i].append("right")
elif ((b<(d-5))&(abs(a-c)<5)):
ig[i].append("up")
elif ((d<(b-5))&(abs(a-c)<5)):
ig[i].append("down")
#check for gesture patterns in array
astr = ''.join(map(str, ig[i]))
if "rightup" in astr:
Spell("Lumos")
elif "rightdown" in astr:
Spell("Nox")
#Colovaria spell removed
#elif "leftdown" in astr:
# Spell("Colovaria")
elif "leftup" in astr:
Spell("Aguamenti")
print(astr)
def FindWand():
global rval,old_frame,old_gray,p0,mask,color,ig,img,frame
try:
rval, old_frame = cam.read()
cv2.flip(old_frame,1,old_frame)
old_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY)
equalizeHist(old_gray)
old_gray = GaussianBlur(old_gray,(9,9),1.5)
dilate_kernel = np.ones(dilation_params, np.uint8)
old_gray = cv2.dilate(old_gray, dilate_kernel, iterations=1)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
old_gray = clahe.apply(old_gray)
#TODO: trained image recognition
p0 = cv2.HoughCircles(old_gray,cv2.HOUGH_GRADIENT,3,50,param1=240,param2=8,minRadius=4,maxRadius=15)
if p0 is not None:
p0.shape = (p0.shape[1], 1, p0.shape[2])
p0 = p0[:,:,0:2]
mask = np.zeros_like(old_frame)
ig = [[0] for x in range(20)]
print("finding...")
threading.Timer(3, FindWand).start()
except:
e = sys.exc_info()[1]
print("Error: %s" % e)
exit
def TrackWand():
global rval,old_frame,old_gray,p0,mask,color,ig,img,frame
try:
color = (0,0,255)
rval, old_frame = cam.read()
cv2.flip(old_frame,1,old_frame)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
equalizeHist(old_gray)
old_gray = GaussianBlur(old_gray,(9,9),1.5)
dilate_kernel = np.ones(dilation_params, np.uint8)
old_gray = cv2.dilate(old_gray, dilate_kernel, iterations=1)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
old_gray = clahe.apply(old_gray)
# Take first frame and find circles in it
p0 = cv2.HoughCircles(old_gray,cv2.HOUGH_GRADIENT,3,50,param1=240,param2=8,minRadius=4,maxRadius=15)
if p0 is not None:
p0.shape = (p0.shape[1], 1, p0.shape[2])
p0 = p0[:,:,0:2]
mask = np.zeros_like(old_frame)
except:
print("No points found")
# Create a mask image for drawing purposes
while True:
try:
rval, frame = cam.read()
cv2.flip(frame,1,frame)
if p0 is not None:
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
equalizeHist(frame_gray)
frame_gray = GaussianBlur(frame_gray,(9,9),1.5)
dilate_kernel = np.ones(dilation_params, np.uint8)
frame_gray = cv2.dilate(frame_gray, dilate_kernel, iterations=1)
frame_clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
frame_gray = frame_clahe.apply(frame_gray)
# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# Select good points
good_new = p1[st==1]
good_old = p0[st==1]
# draw the tracks
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
# only try to detect gesture on highly-rated points (below 10)
if (i<15):
IsGesture(a,b,c,d,i)
dist = math.hypot(a - c, b - d)
if (dist<movment_threshold):
cv2.line(mask, (a,b),(c,d),(0,255,0), 2)
cv2.circle(frame,(a,b),5,color,-1)
cv2.putText(frame, str(i), (a,b), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,0,255))
img = cv2.add(frame,mask)
cv2.putText(img, "Press ESC to close.", (5, 25),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,255,255))
cv2.imshow("Raspberry Potter", frame)
# get next frame
rval, frame = cam.read()
# Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)
except IndexError:
print("Index error - Tracking")
except:
e = sys.exc_info()[0]
print("Tracking Error: %s" % e)
key = cv2.waitKey(20)
if key in [27, ord('Q'), ord('q')]: # exit on ESC
cv2.destroyAllWindows()
cam.release()
break
try:
FindWand()
print("START incendio_pin ON and set switch off if video is running")
pi.write(incendio_pin,0)
pi.write(switch_pin,0)
TrackWand()
finally:
cam.release()
cv2.destroyAllWindows()