forked from sean-obrien/rpotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpotter.py
207 lines (181 loc) · 6.92 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
_\
\
O O-O
O O
O
Raspberry Potter
Version 0.1.1
Use your own wand or your interactive Harry Potter wands to control the IoT.
Copyright (c) 2015 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 cv2
from cv2 import *
from cv import *
import picamera
import numpy as np
import threading
import sys
import math
import time
import pigpio
GPIOS=32
MODES=["INPUT", "OUTPUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"]
pi = pigpio.pi()
#pin for Powerswitch (Lumos,Nox)
switch_pin = 16
pi.set_mode(switch_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 = (9,9)
dilation_params = (5, 5)
movment_threshold = 80
# start capturing
cv2.namedWindow("Raspberry Potter")
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))
if (spell=="Colovaria"):
print "GPIO trinket"
pi.write(trinket_pin,0)
time.sleep(1)
pi.write(trinket_pin,1)
elif (spell=="Lumos"):
print "GPIO ON"
pi.write(switch_pin,1)
elif (spell=="Nox"):
print "GPIO OFF"
pi.write(switch_pin,0)
print "CAST: %s" %spell
def IsGesture(a,b,c,d,i):
print "point: %s" % i
#record basic movements - TODO: trained gestures
if ((a<(c-5))&(abs(b-d)<1)):
ig[i].append("left")
elif ((c<(a-5))&(abs(b-d)<1)):
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")
elif "leftdown" in astr:
Spell("Colovaria")
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 = cvtColor(old_frame,cv2.COLOR_BGR2GRAY)
#equalizeHist(old_gray,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)
#TODO: trained image recognition
p0 = cv2.HoughCircles(old_gray,cv2.cv.CV_HOUGH_GRADIENT,3,100,param1=100,param2=30,minRadius=4,maxRadius=15)
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
cam.release()
cv2.destroyAllWindows()
exit
def TrackWand():
global rval,old_frame,old_gray,p0,mask,color,ig,img,frame
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)
#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)
# Take first frame and find circles in it
p0 = cv2.HoughCircles(old_gray,cv2.cv.CV_HOUGH_GRADIENT,3,100,param1=100,param2=30,minRadius=4,maxRadius=15)
try:
p0.shape = (p0.shape[1], 1, p0.shape[2])
p0 = p0[:,:,0:2]
except:
print "No points found"
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
while True:
rval, frame = cam.read()
cv2.flip(frame,1,frame)
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#equalizeHist(frame_gray,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)
try:
# 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 15)
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))
except IndexError:
print "Index error"
cam.release()
cv2.destroyAllWindows()
break
except:
e = sys.exc_info()[0]
print "Error: %s" % e
cam.release()
cv2.destroyAllWindows()
break
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)
key = cv2.waitKey(20)
if key in [27, ord('Q'), ord('q')]: # exit on ESC
break
FindWand()
TrackWand()
cv2.destroyAllWindows()
cam.release()