/
fish_tank.py
362 lines (294 loc) · 12.7 KB
/
fish_tank.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
'''
Created on Jun 7, 2012
@author: thomasdunne
'''
"""
Object:
Tank:
Vars:
Array of Fish:
Single Water:
Shared Array of Faces: (REM: shared, therefore use RLocks)
-- Lauch a new thread to try to keep this array as up to date as possible
Methods:
Update Array of Fish:
Update Background:
Draw:
Fish:
Vars:
Position:
Previous Position:
Direction:
Original Image: (to avoid repetative scaling induced blur)
Drawable Image:
Drawable Position:
Methods:
Update: (REM: last position = position, and direction)
Draw:
Water:
Vars:
Background objs
Methods:
Update:
Draw:
"""
from SimpleCV import Display, Image, HaarCascade, Kinect, pg
import time, threading
lock = threading.RLock()
class FaceListUpdater(threading.Thread):
def __init__(self, parent, which):
self.parent = parent
self.haar = HaarCascade(which)
threading.Thread.__init__(self)
def run(self):
while self.parent.running == True:
with lock:
self.parent.camera_image = self.parent.camera.getImage().flipHorizontal()
self.parent.face_array = self.parent.camera_image.resize(w=320).findHaarFeatures(self.haar)
print "*************Thread Func***********"
print "* cam =", self.parent.camera
print "* cam_img =", self.parent.camera_image
print "* arr =", self.parent.face_array
print "***********************************"
class Tank():
def __init__(self, cam):
self.running = True
self.camera = cam
self.camera_image = None
self.fish_array = []
self.face_array = []
self.face_updater = FaceListUpdater(self, 'face2.xml')
self.face_updater.start()
# self._test_run(self)
print "**************Main Func************"
print "* cam =", self.camera
print "* cam_img =", self.camera_image
print "* arr =", self.face_array
print "***********************************"
self.water = Water(self)
self.canvas = self.camera.getImage().copy()
def __del__(self):
print "*** DELETING ***"
self.running = False
self.face_updater.join()
def _test_run(self, parent):
print "calling camera methods..."
with lock:
parent.camera_image = parent.camera.getImage().flipHorizontal()
parent.face_array = parent.camera_image.resize(w=320).findHaarFeatures(HaarCascade('face2.xml'))
print "updating camera", parent.camera_image
def update(self):
#self._test_run(self)
self.update_fish()
self.update_background()
def update_fish(self):
if self.face_array is not None:
diff = len(self.face_array) - len(self.fish_array)
if diff > 0:
for dif in range(diff):
self.fish_array.append(Fish("images/fish1.png", "images/alpha_fish.png"))
for index in range(len(self.face_array)):
face = self.face_array[index]
self.fish_array[index].update(face.x, face.y, face.width(), face.height())
def update_background(self):
if self.water is not None:
self.water.update()
def draw(self, canvas):
if self.water is not None:
self.water.draw(self)
for fish in self.fish_array:
fish.draw(self)
self.canvas.save(canvas)
class Fish():
def __init__(self, image_url, mask_url):
self.position = (10, 10)
self.last_position = (0, 0)
self.draw_position = (0, 0)
self.direction = "left"
self.orig_image = Image(image_url)
self.orig_mask = Image(mask_url).invert()
self.draw_image = self.orig_image.copy()
self.draw_mask = self.orig_mask.copy()
def update(self, x, y, w, h):
self.update_dir()
self.position = (x, y)
self.draw_image = self.orig_image.resize(w=w, h=h)
self.draw_mask = self.orig_mask.resize(w=w, h=h)
if self.direction == "left":
self.draw_position = self.position
elif self.direction == "right":
# TODO: setup offsets here for image when facing another direction
self.draw_position = (self.position[0] + 0, self.position[1] + 0)
pass
def update_dir(self):
hor_change = self.position[0] - self.last_position[0]
# if the difference is more than 10 pixels
if abs(hor_change) > 5:
# TODO: can optimise this slightly by checking if == left && horchange is positive
# (so doesnt flip it every time over 10 pixels in the same direction)
if self.direction == "left":
self.direction = "right"
self.draw_image = self.orig_image.flipHorizontal().copy()
self.draw_mask = self.orig_mask.flipHorizontal().copy()
elif self.direction == "right":
self.direction = "left"
self.draw_image = self.orig_image.copy()
self.draw_mask = self.orig_mask.copy()
self.last_position = self.position
def draw(self, parent):
#self.draw_image.save(canvas)
print self.draw_image
print self.draw_position
parent.canvas = parent.canvas.blit(self.draw_image, pos=self.draw_position, alphaMask=self.draw_mask)
class Water():
def __init__(self, parent):
self.position = (0, 0)
self.draw_images = []
self.parent = parent
def assign(self, image):
print "Assigned", type(image), "to Water"
self.draw_images.append(image)
def update(self):
"""
print "calling camera methods..."
print "*************Thread Func***********"
print "* cam =", cam
print "* cam_img =", img[0]
print "* arr =", array[0]
print "***********************************"
"""
pass
def draw(self, parent):
parent.canvas = parent.canvas.blit(parent.camera_image, pos=self.position)
for image in self.draw_images:
if image is not None:
with lock:
image.save(canvas)
if __name__ == '__main__':
#disp = Display(flags = pg.FULLSCREEN)
disp = Display()
cam = Kinect()
time.sleep(0.05)
fishtank = Tank(cam)
#fish = Fish("images/fish1.png", "images/alpha_fish.png")
#fishtank.fish_array.append(fish)
print "Everything should be initalised..."
try:
while not disp.isDone():
print "Looping..."
if disp.mouseLeft:
fishtank.running = False
break
fishtank.update()
fishtank.draw(disp)
#time.sleep(0.05)
#time.sleep(1)
finally:
fishtank.running = False
"""
class tfish():
def __init__(self, image):
self.image = scv.Image(image)
self.orig_image_left = scv.Image(image)
self.orig_image_right = scv.Image(image)#.flipHorizontal()
self.direction = "left"
self.pos = (0, 0)
self.lastpos = (-1, -1)
self.print_pos = self.pos
def update(self, newx, newy):
# the new point is more left than old point
# therefore we must be swimming left
if self.pos[0] <= self.lastpos[0]:
# if we're swimming right
if self.direction == "right":
# change fish to swim left
self.direction = "left"
# fish should no swim left
# if should be swimming right
else:
# but we are actually swimming left
if self.direction == "left":
# flip image to swim right
self.direction = "right"
# now we should be swimming right
if self.direction == "right":
self.image = self.orig_image_right.resize(h=int(newy*1.25)).copy()#(int(newx * 1.75), int(newy)).copy()
#self.print_pos = (self.pos[0] + 10 - 50, int(self.pos[1] - 10))
else:
self.image = self.orig_image_left.resize(h=int(newy*1.25)).copy()#(int(newx * 1.75), int(newy)).copy()
self.print_pos = (self.pos[0] + 10, int(self.pos[1] - 10))
#print "fish is now swimming", self.direction, "from", self.lastpos, "to", self.pos
if __name__ == '__main__':
cam = scv.Kinect()
disp = scv.Display(flags=scv.pg.FULLSCREEN)
hc = scv.HaarCascade("face2.xml")
hmouth = scv.HaarCascade("mouth.xml")
image_mask = scv.Image("fish/alpha_mask_fish.png")
water_image = scv.Image("fish/water.png")
water_images = [scv.Image(wa) for wa in ["fish/water1.png", "fish/water2.png", "fish/water3.png"]]
image_urls = ["fish/fish1.png", "fish/fish2.png", "fish/fish3.png"]
fish = []
for url in image_urls:
fish.append(tfish(url))
start_time = time.time()
water_index = 0
water_time = 0
while not disp.isDone():
""
depth = cam.getDepth()
if disp.mouseLeft:
break
depth.save(disp)
""
image = cam.getImage().flipHorizontal()#.scale(320,240)
faces = image.findHaarFeatures(hc) # load in trained face file
#image = image.blit(water_image, pos=(0, 0), alpha=0.5)
water_time += 1
if water_time == 10:
water_index += 1
water_time = 0
if water_index >= len(water_images):
water_index = 0
image = image.blit(water_images[water_index], pos=(0, 0), alpha=0.75)
if faces:
faces.sortArea()
for index in range(len(faces)):
face = faces[index]
facex = face.x - (face.width() / 2)
facey = face.y - (face.height() / 2)
voff = face.height() * 0.25
half = (face.width()/2, face.height()/2)
image.drawRectangle(face.x-half[0], face.y-half[1]-voff, face.width(), face.height()+voff, colour, thickness, 255)
index = index % len(fish)
fish[index].lastpos = fish[index].pos
fish[index].pos = (int(face.x-half[0]), int(face.y-half[1]-voff))
fish[index].update(face.width(), face.height()+voff)
size = fish[index].image.size()
#image.blit(fish[index].image, alphaMask=image_mask.scale(fish[index].image.size()[0], fish[index].image.size()[1]).toGray(), pos=fish[index].print_pos)
if fish[index].direction == "left":
image = image.blit(fish[index].image, pos=fish[index].print_pos, alphaMask=image_mask.scale(size[0], size[1]))
else:
image = image.blit(fish[index].image, pos=fish[index].print_pos, alphaMask=image_mask.scale(size[0], size[1]))#).flipHorizontal())
myFace = face.crop()
myFace = myFace.crop(0, myFace.height/2, myFace.width, myFace.height/2)
mouths = myFace.findHaarFeatures(hmouth)
if mouths:
mouths.sortArea()
for ind in [-1]:#range(len(mouths)):
mouth = mouths[ind]
mouthx = facex + mouth.x - (mouth.width() / 2)
mouthy = facey + myFace.height + mouth.y - (mouth.height() / 2)
image.drawRectangle(mouthx, mouthy, mouth.width(), mouth.height(), scv.Color.BLUE, thickness, -1)
image.drawText(str(ind), facex+mouth.x, facey+mouth.y, scv.Color.BLUE, 20)
#myFace.drawRectangle(mouth.x-mouth.width()/2, mouth.y-mouth.height()/2, mouth.width(), mouth.height(), scv.Color.BLUE, thickness, -1)
#myFace.save(disp)
else:
image.drawText("No mouths detected :O", 20, 20, scv.Color.RED, 20)
image.save(disp) #display the image
#time.sleep(0.01) # Let the program sleep for 1 millisecond so the comp
#findHaarFeatures(self, cascade,scale factor, min neighbors, use canny)
#img.upload(dest, api key, api secret, verbose)
#img.smooth(algorithm name, aperature, sigma, spatial sigma, grayscale)
#img.drawCircle(ctr, rad, color, thickness)
print "Exiting... Freed Kinect Ownership"
"""