-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_process_and_robot_control.py
393 lines (286 loc) · 9.2 KB
/
image_process_and_robot_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
#!/usr/bin/env python2
from __future__ import print_function
import numpy as np
import cv2
import requests
import numpy as np
import time, json
import threading
import struct, socket
# Functions for controlling the robot.
#
# A phone connected to the robot has a running webcam,
# and the livestream is processed to determine what
# kind of environment the robot is in. Using computer vision,
# the robot can autonomously follow a line, solve the labyrinth,
# and detect and cross brigdes.
# It can also determine that it's in a forest, but solving the
# forest autonomously is still TODO.
## Commands for robot control via UDP
CMD_MOVE = 1
CMD_ROTATE = 2
CMD_STOP = 3
CMD_JOG = 4
CMD_CRANE = 5
ROBOT_ADDRESS = '192.168.43.123'
ROBOT_PORT = 8123
#
# Communication with robot
#
# create UDP socket
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
# function to pack and pad a control message for robot
def pack( cmd, flags, speed, value ):
return struct.pack( ">BBhh", cmd, flags, int(speed), int(value) ) + b" "*10
# function to rotate robot
def rotate_robot( angle ):
print( "Posting to robot")
msg = pack( CMD_ROTATE, 0x00, 300, angle )
sock.sendto( msg, (ROBOT_ADDRESS, ROBOT_PORT ) )
# function to move the robot
def move_robot( dist ):
print( "Posting to robot")
msg = pack( CMD_MOVE, 0x00, 450, dist )
sock.sendto( msg, (ROBOT_ADDRESS, ROBOT_PORT ) )
#
# Image processing and robot control
#
# compute image entropy
def compute_entropy( img ):
hist = cv2.calcHist([img], [0], None, [256], [0,256] )
hist = hist.ravel() / hist.sum()
logs = np.log2( hist + 1e-4 )
entropy = -1 * (hist*logs).sum()
return entropy
# Generate and test hypotheses whether image contains lines
def generate_line_hypotheses( img ):
img[:,0:int(img.shape[1]*0.3)] = 0
img[:, int(img.shape[1]*0.7):-1] = 0
mu = cv2.moments( img )
total_area = img.shape[0]*img.shape[1]
fillrate = mu['m00'] / 256 / total_area
hypothesis_no_track = fillrate < 0.05
hypothesis_all_track = fillrate > 0.5
hypothesis_some_track = not hypothesis_all_track and not hypothesis_no_track
if mu['m00'] < 1:
mu['m00'] = 1
cx = int(mu['m10' ]/mu['m00'])
cy = int(mu['m01']/mu['m00'])
left_total = img[:,:img.shape[1]/2].sum()
right_total = img[:,img.shape[1]/2:].sum()
return {"no": hypothesis_no_track, "all": hypothesis_all_track, "some": hypothesis_some_track, "centroid": (cx,cy), "side": left_total < right_total}
# find image centroid (center of "mass" )
def find_centroid( img ):
mom = cv2.moments(img)
ret = False
cx = 0
cy = 0
if mom['m00'] > 255*100:
cx = int(mom['m01']/mom['m00'])
cy = int(mom['m10']/mom['m00'])
ret = True
return ret, (cx, cy)
# Thread class for receiving and parsing the MJPEG stream from the phone
class StreamThread( threading.Thread ):
def __init__( self ):
threading.Thread.__init__( self )
self.output = None
self.cnt = 0
self.setDaemon( True )
def run( self ):
try:
while True:
r = requests.get('https://192.168.43.100:8080/video', stream=True, verify = False)
if r.status_code == 200:
data = bytes()
for chunk in r.iter_content( chunk_size = 1024 ):
data += chunk
# search for jpeg boundaries
a = data.find( b'\xff\xd8' )
b = data.find( b'\xff\xd9' )
if a != -1 and b != -1:
jpg = data[ a:b+2 ]
data = data[ b+2: ]
self.output = jpg
self.cnt += 1
except:
print( "Missing frame..." )
# Negative feedback control for servoing the robot towards target
def servo_towards_centroid( mask, cx ):
global servo_t
dangle = cx - mask.shape[1] / 2
angle = int(dangle * 1.1)
print( "dangle", dangle, "angle", angle )
# Measured: 3456 tacho steps per metre
gain = 0.2893013100436681
# if angular error is small enough -> move towards target
if abs(dangle) < 15:
move_robot( int( 35 / gain ) )
else:
rotate_robot( angle )
time.sleep(0.55 )
def compute_sparsest_area( img ):
values = [row.sum() for row in cv2.transpose(img)]
print( "len(values)", len(values) )
w = [values[0]]*40
flt = []
for v in values:
w.append( v )
w = w[1:]
flt.append( sum(w) * 1.0 / len(w) )
mf = max(flt)
flt2 = []
for v in flt:
flt2.append( 1 - v / mf )
Xflt2 = 0.0
Aflt2 = 0.0
for i in range( len(flt2) ):
Xflt2 += i * flt2[i]
Aflt2 += flt2[i]
print( "XFlt2", Xflt2, "Aflt2", Aflt2 )
return Xflt2 / (Aflt2 + 1)
stream = StreamThread()
stream.start()
ROBOT_STATE = "IDLE"
STATE_HISTORY = []
bridge_cnt = 5
prev_cnt = 0
t_last_servo = time.time()
while True:
# Process image from stream. Rotate it correctly (depends on how
# camera is positioned), and convert it to HSV for easier detection
# of color changes. Based on colors and their distribution in the
# image, detect what type of area we are in and control robot
# accordingly.
try:
if prev_cnt == stream.cnt:
continue
prev_cnt = stream.cnt
t0 = time.time()
img = cv2.imdecode( np.fromstring( stream.output, dtype = np.uint8 ), cv2.IMREAD_COLOR )
bw = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
rows, cols = bw.shape
M = cv2.getRotationMatrix2D( (cols/2, rows/2), -90, 1 )
bw = cv2.warpAffine( bw, M, (cols, rows) )
bw = cv2.flip( bw, 1 )
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv = cv2.warpAffine( hsv, M, (cols, rows) )
true_color = cv2.warpAffine( img, M, (cols, rows) )
cv2.imshow( "True", true_color )
cv2.imshow( "HSV", hsv )
if ROBOT_STATE != "IDLE":
print( ROBOT_STATE )
hsv = hsv[int(hsv.shape[0]*0.3):, :]
found_lines = False
# Lines on the floor
if True:
limit1 = np.array([0, 0, 220])
limit2 = np.array([180, 15, 255])
mask = cv2.inRange( hsv, limit1, limit2 )
ret, centroid = find_centroid( mask )
if ret:
cx, cy = centroid
cv2.circle(mask, (cy,cx), 10, (255,0,0), 1)
left = mask[:, :int(1*mask.shape[1]/3)].sum()
centre = mask[:, int(1*mask.shape[1]/3):int(2*mask.shape[1]/3)].sum()
right = mask[:, int(2*mask.shape[1]/3):].sum()
q_left = left*1.0/centre
q_right = right*1.0/centre
if False:
if q_left > 0.4:
# turn left
print( "TURN LEFT!" )
pass
elif q_right > 0.4:
#turn right
print( "TURN RIGHT!" )
pass
viivat_lattiassa_mask = mask.copy()
found_lines = True
bridge_cnt = 5
mask = cv2.cvtColor( mask, cv2.COLOR_GRAY2BGR )
cv2.imshow( "Mask, viiva lattiassa", mask )
# Labyrinth button
if True:
limit1 = np.array([25, 220, 220])
limit2 = np.array([35, 255, 255])
mask = cv2.inRange( hsv, limit1, limit2 )
ret, centroid = find_centroid( mask )
mask = cv2.cvtColor( mask, cv2.COLOR_GRAY2BGR )
if ret:
if ROBOT_STATE == "BRIDGE":
ROBOT_STATE = "LABYRINTH_BUTTON"
move_robot(300)
time.sleep(0.25)
cx, cy = centroid
cv2.circle(mask, (cy,cx), 10, (255,0,0), 1)
if ROBOT_STATE == "LABYRINTH_BUTTON":
servo_towards_centroid( mask, cy )
else:
if ROBOT_STATE == "LABYRINTH_BUTTON":
STATE_HISTORY.append( ROBOT_STATE )
time.sleep(1.0)
rotate_robot(-80)
time.sleep(0.55)
move_robot(800)
time.sleep(0.55)
print( "Changing to BRIDGE")
ROBOT_STATE = "BRIDGE"
cv2.imshow( "Mask, labyrinth button", mask )
# Bridge
if True:
limit1 = np.array([0, 50, 150])
limit2 = np.array([35, 255, 255])
mask = cv2.inRange( hsv, limit1, limit2 )
if found_lines:
print( mask.shape, viivat_lattiassa_mask.shape )
mask = cv2.add( mask, viivat_lattiassa_mask )
mask = mask[:mask.shape[0], :]
ret, centroid = find_centroid( mask )
mask = cv2.cvtColor( mask, cv2.COLOR_GRAY2BGR )
if ret:
cx, cy = centroid
cv2.circle(mask, (cy,cx), 10, (255,0,0), 1)
if ROBOT_STATE == "BRIDGE":
servo_towards_centroid( mask, cy )
else:
if ROBOT_STATE == "BRIDGE":
STATE_HISTORY.append( ROBOT_STATE )
bridge_cnt -= 1
if bridge_cnt < 1:
ROBOT_STATE = "IDLE"
cv2.imshow( "Mask, bridge", mask )
# "Forest" of metal pillars
if True:
limit1 = np.array([0, 0, 0])
limit2 = np.array([180, 50, 80])
mask = cv2.inRange( hsv, limit1, limit2 )
ret, centroid = find_centroid( mask )
mask = cv2.cvtColor( mask, cv2.COLOR_GRAY2BGR )
if ret:
cx, cy = centroid
cv2.circle(mask, (cy,cx), 10, (255,0,0), 1)
h,s,v = cv2.split( hsv )
img2 = 255 - v
img2 = cv2.resize( img2, None, fx = 2.0, fy = 2.0 )
cv2.imshow( "Mask, pillars, v2", img2 )
if ROBOT_STATE == "FOREST":
# TODO complete this
sparsest = compute_sparsest_area( img2 )
print("sparsest", sparsest)
#servo_towards_centroid( mask, int(sparsest) )
#time.sleep(0.5)
#move_robot(200)
#time.sleep(0.5)
except IOError:
# If frames are missing (not processed quick enough etc),
# just move to the next one
print( "Missing frame.." )
key = cv2.waitKey(25)
# Exit on Esc
if key & 0xff == 27:
exit(0)
if key & 0xff == ord( 'a' ):
ROBOT_STATE = "BRIDGE"
if key & 0xff == ord(' '):
ROBOT_STATE = "IDLE"