-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskittle_track.py
160 lines (132 loc) · 5.4 KB
/
skittle_track.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
"""Track colours on skittlebot. Show X and Y only"""
from picamera.array import PiRGBArray
from picamera import PiCamera
from fractions import Fraction
from controlled_variable import ControlledVariable
import cv2
import numpy as np
import time
from piwars_bot import Robot
def compute_contours(cnts):
"""Centroid stuff"""
# 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)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
return center, radius, (x, y)
def setup_keyboard():
lh = ControlledVariable(0, 255, 90, (ord('a'), ord('s')))
uh = ControlledVariable(0, 255, 120, (ord('k'), ord('l')))
ls = ControlledVariable(0, 255, 10, (ord('q'), ord('w')))
hs = ControlledVariable(0, 255, 255, (ord('o'), ord('p')))
lv = ControlledVariable(0, 255, 240, (ord('z'), ord('x')))
hv = ControlledVariable(0, 255, 255, (ord('m'), ord('n')))
return lh, uh, ls, hs, lv, hv
def setup_camera():
camera = PiCamera()
camera.resolution = (320, 240)
time.sleep(2)
camera.shutter_speed = camera.exposure_speed
camera.exposure_mode = "off"
gain = camera.awb_gains
camera.awb_mode = "off"
camera.awb_gains = gain
camera.vflip = True
stream = PiRGBArray(camera, size=(320, 240))
return camera, stream
def handle_keys(k, lh, uh, ls, hs, lv, hv):
# Return True if it should exit.
should_exit = False
if k == 27:
should_exit = True
lh.handle_key(k)
uh.handle_key(k)
ls.handle_key(k)
hs.handle_key(k)
lv.handle_key(k)
hv.handle_key(k)
should_toggle_motors = False
if k == ord('g'):
should_toggle_motors = True
return should_exit, should_toggle_motors
def track():
# illumination can be a problem due to colour value
# hsv? yuv?- define range (try hsv later)
# Colour range - hsv. H110-130 blueish.
# sv - right the way up to allow for illumination
# Lego thing: [90 0 240] [120 255 255]
# Low light butterfly or skittle [90 10 240] [120 255 255]
# Green skittle - [ 35 130 80] [ 95 255 255], [30 80 85] [ 95 255 255]
# Green box - [25 30 80] [ 90 230 175]
# Primary red H30-90
# Primary Green
# Primary Blue
# Yellow
lh, uh, ls, hs, lv, hv = setup_keyboard()
camera, stream = setup_camera()
motors = False
with Robot() as robot:
for frame in camera.capture_continuous(stream, format='bgr', use_video_port=True):
frame = stream.array
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_range = np.array([lh.value, ls.value, lv.value], np.uint8)
upper_range = np.array([uh.value, hs.value, hv.value], np.uint8)
# Create a mask around that colour
inrange = cv2.inRange(hsv, lower_range, upper_range)
kernel = np.ones((5,5),np.uint8)
mask = cv2.morphologyEx(inrange, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# find contours in the mask and initialize the current
# (x, y) center of the object
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
if len(cnts) > 0:
center, radius, (x, y) = compute_contours(cnts)
if radius > 10:
cv2.circle(frame, (int(x), int(y)), int(radius),(0, 255, 255), 2)
cv2.circle(frame, center, 3, (0, 0, 255), -1)
cv2.putText(frame,"centroid", (center[0]+10,center[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(0, 0, 255),1)
cv2.putText(frame,"("+str(center[0])+","+str(center[1])+")", (center[0]+10,center[1]+15), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(0, 0, 255),1)
cv2.putText(frame,"r"+str(int(radius)), (center[0]+10,center[1]+30), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(0, 0, 255),1)
_, w, _ = frame.shape
hw = 160
if x < hw - 45:
print("Driving right", x, y)
if motors:
robot.set_left(100)
robot.set_right(0)
elif x > hw + 45:
print("Driving left", x, y)
if motors:
robot.set_left(0)
robot.set_right(100)
else:
print("Ramming speed!!!", x, y)
if motors:
robot.set_left(100)
robot.set_right(100)
else:
if motors:
robot.set_left(0)
robot.set_right(100)
cv2.imshow("Ranged", inrange)
cv2.imshow("Frame", frame)
print(lower_range, upper_range)
k = cv2.waitKey(1)
should_exit, should_toggle_motors = handle_keys(k, lh, uh, ls, hs, lv, hv)
if should_exit:
break
if should_toggle_motors:
print("Toggling motors")
motors = not motors
if not motors:
print("off")
robot.stop()
stream.truncate(0)
def main():
track()
if __name__ == '__main__':
main()