-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControl.py
513 lines (418 loc) · 16.4 KB
/
Control.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"""
"""
import pygame
import numpy as np
try:
import argparse
import imutils
import cv2
except:
print "Open CV libraries not loaded."
from curve import *
from Model import *
#define HSV color range of a tennis ball: open cv has range: H: 0-180, S: 0 -255, V: 0-255
colors = {'bright_green':[(29, 84, 6),(64, 255, 255)],'bright_pink':[(145,84,120),(175,255,255)]}
color = 'bright_green' # Used for OpenCV
#pull_mode can be 'Handle' or 'Curve'
pull_mode = "Handle"
class Controller(object):
"""
Handles Keyboard, Mouse and OpenCV inputs.
Instantiated in view.py.
Attributes:
running_points - Array holding points as they are drawn, but before a curve is created.
running - Bool telling main.py if the game is running (ie when it stops).
curve - Curve object, encapsulating the input line, the derivative and integral.
last - Dictionary of the previous status of relavent keys last loop.
pull_point - Index of the point being pulled when moving with Handles.
mode - Dictionary with the mode for key, and a bool indicating whether it is active for value.
model - Model object.
pull_mode - String, either Handle or Curve.
image - Image used by OpenCV.
Methods:
handle_events - Handles all events (run at every loop iteration).
change_mode - Change mode based on keypresses.
draw_with_mouse - Get mouse input and add to running_points. Makes sure that the x values are monotonically increasing.
pull_with_mouse - Logic behind moving a curve from mouse input.
find_curve - Hitbox handling when finding a handle on a curve.
"""
def __init__(self):
self.modes=[None, 'Mouse drawing','Open CV drawing', "Mouse pulling", 'Open CV calibrating','Show tangent', 'Show area', 'Show critical points']
try:
self.open_cv_control = Open_cv_control()
except:
pass
self.running_points = [] # Used for recording points
self.running = True # Used by main to check if the code quits
self.curve = None
self.last = { # Used to get key press
"space": False,
"press": False,
"g": False,
"l": False,
"w": False,
"a": False,
"c": False,
"h": False,
"s": False
}
self.mode = {
"Show tangent": False,
"Show area": False,
"Show critical points": False,
"Mouse drawing": False,
"Mouse pre-drawing": True, # Start in pre-drawing mode
"Mouse pulling": False,
"Open CV drawing": False,
"Open CV calibrating": False,
"Show help": False
}
self.pull_point = None # Determing which point is being pulled
self.model = Model()
self.pull_mode = pull_mode
self.image = None
self.save = False
def handle_events(self):
"""
Handles all events. Called by view.draw().
"""
# Check for quitting the game
for event in pygame.event.get():
if event.type == pygame.QUIT: # Handle window closing
try:
self.open_cv_control.close_camera()
except:
print "OpenCV not loaded, no camera to close"
self.running = False
keys = pygame.key.get_pressed() # Returns a tuple of 1s and 0s corresponding to the the pressed keys
hitbox_radius = 5 # Hitbox for clicking on curves
mouse1 = pygame.mouse.get_pressed()[0]
mouse1_used = False
# Perform the mode's action
# if self.mode['Mouse drawing']:
# self.draw_with_mouse()
# if mouse1 and not self.last["press"]: # Press Mouse1 to enter/leave Drawing mode
#
# mouse1_used = True
# self.clear_modes()
# if len(self.running_points)>15:
# self.curve = Curve(self.running_points[::len(self.running_points)/7], self.pull_mode) #[::len(self.running_points)/15]
# else:
# print 'Not enough points registered'
if self.mode['Mouse pre-drawing']:
if mouse1 and not self.last["press"]:
self.mode["Mouse pre-drawing"] = False
self.mode["Mouse drawing"] = True
elif self.mode['Mouse drawing']:
self.draw_with_mouse()
if mouse1 and not self.last["press"]: # Press Mouse1 to enter/leave Drawing mode
mouse1_used = True
self.clear_modes()
if len(self.running_points)>15:
self.curve = Curve(self.running_points[::len(self.running_points)/7], self.pull_mode) #[::len(self.running_points)/15]
else:
print 'Not enough points registered'
elif self.mode['Open CV calibrating']:
try:
self.open_cv_control.calibrate_color()
if keys[pygame.K_c] and not self.last["w"]:
self.clear_modes()
except:
print "OpenCV not loaded: Cannot calibrate OpenCV"
elif self.mode['Open CV drawing']:
try:
self.open_cv_control.draw_with_open_cv()
except:
pass
self.image = self.open_cv_control.image
self.running_points = self.open_cv_control.running_points
elif self.mode["Mouse pulling"]:
self.pull_with_mouse()
if self.mode["Show tangent"]:
idx = self.find_point(self.curve.line.points, vertical=False)
if idx != None:
self.tangent_point = idx
self.curve.line.make_tangent(self.tangent_point,200)
if self.mode['Show area']:
idx = self.find_point(self.curve.line.points, vertical=False)
if idx != None:
self.curve.line.make_area(idx)
if self.mode["Show critical points"]:
if self.curve:
self.curve.derivative.make_crpoints()
# Change self.mode according to keypresses
self.change_mode_key(keys)
# Change self.mode according to on-screen buttons
if mouse1 and not self.last["press"]:
# mouse1_used = True
self.change_mode_btn()
# Update the button's model of the current mode
self.update_buttons()
# Stuff to change grid, legend etc.
if keys[pygame.K_g] and not self.last["g"]:
self.model.grid_update()
if keys[pygame.K_l] and not self.last["l"]:
self.model.legend_update()
if keys[pygame.K_h] and not self.last["h"]:
self.model.legend_update()
# Keep track of last key presses
self.last["space"] = keys[pygame.K_SPACE]
self.last["press"] = mouse1 and not mouse1_used
self.last["g"] = keys[pygame.K_g]
self.last["l"] = keys[pygame.K_l]
self.last["w"] = keys[pygame.K_c]
self.last["t"] = keys[pygame.K_t]
self.last["a"] = keys[pygame.K_a]
self.last["c"] = keys[pygame.K_c]
self.last["h"] = keys[pygame.K_h]
self.last["s"] = keys[pygame.K_s]
def change_mode_key(self, keys):
"""
Check if the user is changing modes.
Used in self.handle_events()
"""
# Moving handles and creating Curve
if pygame.mouse.get_pressed()[0] and not self.last["press"]:
if self.mode["Mouse pulling"]: # Leave pulling mode if in drawing mode
self.mode["Mouse pulling"] = False
elif self.curve: # If there is already a curve, find a pull point and get into pulling mode
if self.pull_mode == "Handle":
self.pull_point = self.find_point(self.curve.line.pull_points)
elif self.pull_mode == "Curve":
self.pull_point = self.find_point(self.curve.line.points, vertical=True)
if self.pull_point != None:
print "Pulling point is number:", self.pull_point
self.mode['Mouse pulling'] = True
# Clearing the screen if mouse 2 is pressed
if pygame.mouse.get_pressed()[2]: # Mouse2 to clear
self.clear_screen()
# switching between Handle and Curve (does not work at the moment)
# if keys[pygame.K_o] and not self.last["o"]:
# if self.pull_mode == 'Handle':
# self.pull_mode = 'Curve'
# elif self.pull_mode == 'Curve':
# self.pull_mode = 'Handle'
# if self.curve:
# self.curve.line.pull_mode = self.pull_mode
#Open CV drawing
if keys[pygame.K_SPACE] and not self.last["space"]:
if self.mode['Open CV drawing']:
self.mode['Open CV drawing'] = False
if len(self.running_points)>15:
self.curve = Curve(self.running_points[::len(self.running_points)/15], self.pull_mode)
else:
print 'Not enough points registered'
else:
try:
self.open_cv_control.running_points = []
self.mode['Open CV drawing'] = True
self.running_points = []
self.curve = None
except:
print "Open CV Not available."
# Tangent
if keys[pygame.K_t] and not self.last["t"]:
if self.mode["Show tangent"]:
self.mode["Show tangent"] = False
elif self.curve:
self.mode["Show tangent"] = True
# Area
if keys[pygame.K_a] and not self.last["a"]:
if self.mode["Show area"]:
self.mode["Show area"] = False
elif self.curve:
self.mode["Show area"] = True
# Critical points
if keys[pygame.K_c] and not self.last["c"]:
if self.mode["Show critical points"]:
self.mode["Show critical points"] = False
elif self.curve:
self.mode["Show critical points"] = True
# Save image
if keys[pygame.K_s] and not self.last["s"]:
self.save = True
def update_buttons(self):
"""
Update the toggle attribute of the buttons based on the modes.
"""
self.model.buttons["Draw"].toggle = self.mode["Mouse drawing"] or self.mode["Mouse pre-drawing"]
self.model.buttons["Camera"].toggle = self.mode["Open CV drawing"]
self.model.buttons["Tangent"].toggle = self.mode["Show tangent"]
self.model.buttons["Area"].toggle = self.mode["Show area"]
self.model.buttons["Crit"].toggle = self.mode["Show critical points"]
def change_mode_btn(self):
"""
Change the various modes based on button clicks.
"""
if self.button_hit("Draw") and not self.curve:
self.clear_modes() # Get into Drawing mode
self.mode['Mouse pre-drawing'] = True
self.running_points = []
elif self.button_hit("Clear"):
self.clear_screen()
elif self.button_hit("Camera"):
# self.clear_modes()
if self.mode['Open CV drawing']:
self.mode['Open CV drawing'] = False
if len(self.running_points)>15:
self.curve = Curve(self.running_points[::len(self.running_points)/15], self.pull_mode)
else:
print 'Not enough points registered'
else:
try:
self.open_cv_control.running_points = []
self.mode['Open CV drawing'] = True
self.running_points = []
self.curve = None
except:
print "Open CV Not available."
elif self.button_hit("Tangent"):
# The following makes sense with logic table.
# The mode should only engage when there is a curve and the mode is off.
self.mode["Show tangent"] = not self.mode["Show tangent"] and bool(self.curve)
elif self.button_hit("Area"):
# Same logic as above
self.mode["Show area"] = not self.mode["Show area"] and bool(self.curve)
elif self.button_hit("Crit"):
# Same logic as above
self.mode["Show critical points"] = not self.mode["Show critical points"] and bool(self.curve)
elif self.button_hit("Grid"):
# Toggle Grid
self.model.grid_update()
elif self.button_hit("Help"):
# Toggle Help
self.mode["Show help"] = not self.mode["Show help"]
def button_hit(self, button_key):
"""
Determines if the mouse is in the button's hitbox. Returns appropriate bool.
"""
mouse = pygame.mouse.get_pos()
pos = self.model.buttons[button_key].position
if pos[0] <= mouse[0] and mouse[0] <= pos[0]+50: # Check x component
if pos[1] <= mouse[1] and mouse[1] <= pos[1]+50:
return True
return False
def clear_modes(self):
"""
Makes the value for all modes False, excluding "Show help".
Used when there should only be one mode eg: Drawing
"""
print "Clearing Modes"
for key in self.mode:
if key != "Show help":
self.mode[key] = False
def draw_with_mouse(self):
'''This method is currently called by view.draw_input()
Allows the user to draw several lines/curves in discrete intervals with mouse.
Press leftbutton to start drawing, move around the mouse to draw (or hold down the lef button while drawing.
Press leftbutton again to stop drawing. Press rightbutton to clear screen.
running_points stores the points of the user's curve as nested lists. (if the user draws a single curve, it would be [[(x,y)...]]
Next implementation would be to stop drawing when the leftbutton is released (MOUSEBUTTONUP doesn't work right now).'''
mouse_pos = pygame.mouse.get_pos()
# Add points based off of mouse position
if not self.running_points:
self.running_points.append(mouse_pos)
if mouse_pos != self.running_points[-1] and self.running_points[-1][0] < mouse_pos[0]: # NOTE: This is where we check if the user goes backwards
self.running_points.append(mouse_pos)
def pull_with_mouse(self):
# Get new mouse positions
mouse_pos = pygame.mouse.get_pos()
# Move point there
self.curve.move_point(self.pull_point, mouse_pos, line="line")
def clear_screen(self):
"""
Clears the curves.
"""
self.clear_modes()
self.running_points = []
try:
self.open_cv_control.running_points = []
except:
print "OpenCV not loaded, Cannot reset open_cv_control.running_points "
self.curve = None
def find_point(self, all_points, vertical=True, radius=5):
"""
Find a point from a mouse click on a list of points.
vertical is a bool; when False it disables vertical search (you can select a point by clicking under it)
"""
mouse = pygame.mouse.get_pos()
found_idx = None
for idx, pt in enumerate(all_points):
if abs(pt[0]-mouse[0]) < radius and (abs(pt[1]-mouse[1]) < radius or not vertical):
found_idx = idx
return found_idx
class Open_cv_control(object):
def __init__(self):
self.running_points = []
self.camera = cv2.VideoCapture(0)
self.camera.set(3,640) #setting camera size
self.camera.set(4,480) #setting camera size
self.prev_avg_col = (0,0,0)
self.color = color
self.draw_color_lower = colors[self.color][0]
self.draw_color_upper = colors[self.color][1]
self.image = None
print 'Initiated open CV'
def draw_with_open_cv(self):
# Grab the current frame (frame and masl are numpy.ndarray)
(grabbed, frame) = self.camera.read()
if grabbed:
# Resize the frame, blur it, and convert it to the HSV color space
frame = imutils.resize(frame, width=600)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Construct a mask for the color "green", then perform a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, self.draw_color_lower, self.draw_color_upper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# Flip the mask and frame horizontally so it's the direction of draw
hfmask = cv2.flip(mask,1)
hfframe = cv2.flip(frame,1) #flip if use open cv to display image
# Find contours in the mask and initialize the current
# (x, y) center of the balls
cnts = cv2.findContours(hfmask.copy(), cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
# Only proceed if at least one contour was found
if len(cnts) > 0:
# Find the largest contour in the mask, then use it to compute the minimum enclosing circle and centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
# Only proceed if the radius meets a minimum size
if radius > 30:
pts=(int(x),int(y))
cv2.circle(hfframe, pts, int(radius),(0, 255, 255), 2)
if not self.running_points:
print 'appending points'
self.running_points.append(pts)
if pts != self.running_points[-1] and self.running_points[-1][0] < pts[0]: # Add point if it is different than the previous
print 'appending points'
self.running_points.append(pts) # and if it doesn't curl back (last x < new x)
for i in xrange(1, len(self.running_points)):
# if either of the tracked points are None, ignore them
if self.running_points[i - 1] is None or self.running_points[i] is None:
continue
# otherwise, compute the thickness of the line and draw the connecting lines
thickness = 2
cv2.line(hfframe, self.running_points[i - 1], self.running_points[i], (255, 0, 0), thickness)
#Set self.image to frame to display in pygame window
self.image = cv2.cvtColor(hfframe,cv2.COLOR_BGR2RGB)
self.image = cv2.flip(self.image,1) #flip the image back for pygame display and rotate the image
self.image = np.rot90(self.image)
#Uncomment the following lines to show frame and mask in separate windows
# cv2.imshow("Mask", hfmask)
# cv2.imshow("Horizontal flip", hfframe)
cv2.waitKey(1) #waitKey displays each image for 1 ms. and allow the loop to run. if itt's 0 the image will be displayed infinitely and no input will be accepted
def calibrate_color(self):
(grabbed, frame) = self.camera.read()
if grabbed:
frame_size = frame.shape
frame_ct = (frame_size[1]/2,frame_size[0]/2)
print frame[frame_ct[0],frame_ct[1]]
cv2.circle(frame, frame_ct, 20,(142, 37, 149), -1) #bright pink color
cv2.rectangle(frame, (frame_ct[0]-50,frame_ct[1]-50),(frame_ct[0]+50,frame_ct[1]+50), (0,255,255),2)
cv2.imshow('Frame',frame)
cv2.waitKey(1)
def close_camera(self):
self.camera.release()
cv2.destroyAllWindows()