/
ColorTracker_jevois.py
221 lines (197 loc) · 9.17 KB
/
ColorTracker_jevois.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
import libjevois as jevois
import cv2
import numpy as np
import time
## Simple example of image processing using OpenCV in Python on JeVois
#
# This module is here for you to experiment with Python OpenCV on JeVois.
#
# By default, we get the next video frame from the camera as an OpenCV BGR (color) image named 'inimg'.
# We then apply some image processing to it to create an output BGR image named 'outimg'.
# We finally add some text drawings to outimg and send it to host over USB.
#
# See http://jevois.org/tutorials for tutorials on getting started with programming JeVois in Python without having
# to install any development software on your host computer.
#
# @author Laurent Itti
#
# @videomapping YUYV 352 288 30.0 YUYV 352 288 30.0 JeVois PythonSandbox
# @email itti\@usc.edu
# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
# @copyright Copyright (C) 2017 by Laurent Itti, iLab and the University of Southern California
# @mainurl http://jevois.org
# @supporturl http://jevois.org/doc
# @otherurl http://iLab.usc.edu
# @license GPL v3
# @distribution Unrestricted
# @restrictions None
# @ingroup modules
class ColorTracker:
# ###################################################################################################
## Constructor
def __init__(self):
# Instantiate a JeVois Timer to measure our processing framerate:
self.timer = jevois.Timer("sandbox", 100, jevois.LOG_INFO)
# ###################################################################################################
## Process function with USB output
def process(self, inframe, outframe):
def tellRobot(bbox, out_center_x, out_center_y, serial_format="XY"):
if bbox is None:
jevois.sendSerial("stop")
else:
box_center_x, box_center_y = bbox[0]+bbox[2]/2, bbox[1]+bbox[3]/2
if serial_format == "XY":
if out_center_x < box_center_x:
move_x = box_center_x - out_center_x
elif box_center_x < out_center_x:
move_x = out_center_x - box_center_x
elif box_center_x == out_center_x:
move_x = 0
if out_center_y < box_center_y:
move_y = box_center_y - out_center_y
elif box_center_y < out_center_y:
move_y = out_center_y - box_center_y
elif box_center_y == out_center_y:
move_y = 0
if move_x < 100:
move_x = 100
if move_y < 100:
move_y = 100
jevois.sendSerial("smoothmove {} {}".format(int(move_x), int(move_y)))
else:
jevois.sendSerial("Invalid Serial Format")
img = inframe.getCvBGR()
frameHeight = img.shape[0]
frameWidth = img.shape[1]
out_center_x, out_center_y = frameWidth/2, frameHeight/2
# Set the frame rate
time.sleep(0.2)
# Set the serial output format
serial_format = "XY" #Options: "Belts", "XY"
# Preprocess the input
blurred = cv2.bilateralFilter(img,9,75,75)
# blurred = cv2.GaussianBlur(img, (21, 21), 0)
ret, thresh = cv2.threshold(blurred, 50, 255, cv2.THRESH_BINARY)
hsv = cv2.cvtColor(thresh, cv2.COLOR_BGR2HSV)
mask = np.zeros((thresh.shape[0], thresh.shape[1], 3), np.uint8)
# Setup the tracker
tracker = cv2.TrackerKCF_create()
bbox = None
# Filter the desired color range
greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
redLower = (0,10,70)
redUpper = (40,255,255)
image = cv2.inRange(hsv, redLower, redUpper)
image = cv2.erode(image, None, iterations=2)
image = cv2.dilate(image, None, iterations=2)
# Find the biggest contour
contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
if contours:
biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
x,y,w,h = cv2.boundingRect(biggest_contour)
box_center_x, box_center_y = x+w/2, y+h/2
cv2.drawContours(mask, [biggest_contour], -1, 255, -1)
# Track the biggest contour
if bbox is None:
bbox = (x, y, w, h)
ok = tracker.init(img, bbox)
cv2.rectangle(mask,(x,y), (x+w, y+h), (0,255,0), 2)
else:
ok, bbox = tracker.update(img)
if ok:
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
box_center_x, box_center_y = bbox[0]+bbox[2]/2, bbox[1]+bbox[3]/2
cv2.rectangle(mask,p1, p2, (0,255,0), 2)
else:
bbox = None
else:
bbox = None
cv2.putText(mask, "BBOX: " + str(bbox), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (50,170,50), 2)
# Tell the robot what to do
tellRobot(bbox, out_center_x, out_center_y)
# Organize the visual output
toprow = np.hstack((img, blurred))
bottomrow = np.hstack((thresh, mask))
outimg = np.vstack((toprow, bottomrow))
outframe.sendCv(outimg)
# ###################################################################################################
## Process function without USB output
def processNoUSB(self, inframe):
def tellRobot(bbox, out_center_x, out_center_y, serial_format="XY"):
if bbox is None:
jevois.sendSerial("stop")
else:
box_center_x, box_center_y = bbox[0]+bbox[2]/2, bbox[1]+bbox[3]/2
if serial_format == "XY":
if out_center_x < box_center_x:
move_x = box_center_x - out_center_x
elif box_center_x < out_center_x:
move_x = out_center_x - box_center_x
elif box_center_x == out_center_x:
move_x = 0
if out_center_y < box_center_y:
move_y = box_center_y - out_center_y
elif box_center_y < out_center_y:
move_y = out_center_y - box_center_y
elif box_center_y == out_center_y:
move_y = 0
if move_x < 100:
move_x = 100
if move_y < 100:
move_y = 100
jevois.sendSerial("smoothmove {} {}".format(int(move_x), int(move_y)))
else:
jevois.sendSerial("Invalid Serial Format")
img = inframe.getCvBGR()
out_x, out_y = 352, 288
out_center_x, out_center_y = out_x/2, out_y/2
# Set the frame rate
time.sleep(0.2)
# Set the serial output format
serial_format = "XY" #Options: "Belts", "XY"
# Preprocess the input
blurred = cv2.bilateralFilter(img,9,75,75)
# blurred = cv2.GaussianBlur(img, (21, 21), 0)
ret, thresh = cv2.threshold(blurred, 50, 255, cv2.THRESH_BINARY)
hsv = cv2.cvtColor(thresh, cv2.COLOR_BGR2HSV)
mask = np.zeros((thresh.shape[0], thresh.shape[1], 3), np.uint8)
# Setup the tracker
tracker = cv2.TrackerKCF_create()
bbox = None
# Filter the desired color range
greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
redLower = (0,10,70)
redUpper = (40,255,255)
image = cv2.inRange(hsv, redLower, redUpper)
image = cv2.erode(image, None, iterations=2)
image = cv2.dilate(image, None, iterations=2)
# Find the biggest contour
contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
if contours:
biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
x,y,w,h = cv2.boundingRect(biggest_contour)
box_center_x, box_center_y = x+w/2, y+h/2
cv2.drawContours(mask, [biggest_contour], -1, 255, -1)
# Track the biggest contour
if bbox is None:
bbox = (x, y, w, h)
ok = tracker.init(img, bbox)
cv2.rectangle(mask,(x,y), (x+w, y+h), (0,255,0), 2)
else:
ok, bbox = tracker.update(img)
if ok:
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
box_center_x, box_center_y = bbox[0]+bbox[2]/2, bbox[1]+bbox[3]/2
cv2.rectangle(mask,p1, p2, (0,255,0), 2)
else:
bbox = None
else:
bbox = None
# Tell the robot what to do
tellRobot(bbox, out_center_x, out_center_y)