Skip to content

Commit 24152aa

Browse files
Merge pull request #376 from JdeRobot/issue-375
Show graphically direction where the robot is heading on displayed image
2 parents 04dd8b6 + 0b6d0ba commit 24152aa

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

behavior_metrics/brains/f1/brain_f1_keras_opencv_dataset.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,23 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
6767
print("- Models path: " + PRETRAINED_MODELS)
6868
print("- Model: " + str(model))
6969

70-
def update_frame(self, frame_id, data):
70+
def update_frame(self, frame_id, data, angular_speed=None):
7171
"""Update the information to be shown in one of the GUI's frames.
7272
7373
Arguments:
7474
frame_id {str} -- Id of the frame that will represent the data
7575
data {*} -- Data to be shown in the frame. Depending on the type of frame (rgbimage, laser, pose3d, etc)
7676
"""
77+
if angular_speed:
78+
import math
79+
x1, y1 = int(data.shape[:2][1] / 2), data.shape[:2][0] # ancho, alto
80+
length = 200
81+
angle = (90 + int(math.degrees(-angular_speed))) * 3.14 / 180.0
82+
x2 = int(x1 - length * math.cos(angle))
83+
y2 = int(y1 - length * math.sin(angle))
84+
85+
line_thickness = 2
86+
cv2.line(data, (x1, y1), (x2, y2), (0, 0, 0), thickness=line_thickness)
7787
self.handler.update_frame(frame_id, data)
7888

7989
def execute(self):
@@ -83,12 +93,13 @@ def execute(self):
8393

8494
image = self.camera.getImage().data
8595
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
96+
base_image = image
8697

8798
if self.cont == 1:
8899
self.first_image = image
89100

90101
image = self.handler.transform_image(image, self.config['ImageTranform'])
91-
self.update_frame('frame_0', image)
102+
#self.update_frame('frame_0', image)
92103

93104
try:
94105
if self.config['ImageCropped']:
@@ -129,6 +140,8 @@ def execute(self):
129140
self.previous_v = prediction[0][0]
130141
self.previous_w = prediction[0][1]
131142

143+
self.update_frame('frame_0', base_image, prediction_w)
144+
132145
# GradCAM from image
133146
i = np.argmax(prediction[0])
134147
cam = GradCAM(self.net, i)
@@ -138,4 +151,4 @@ def execute(self):
138151
self.update_frame('frame_1', output)
139152

140153
except Exception as err:
141-
print(err)
154+
print(err)

behavior_metrics/brains/f1/brain_f1_keras_seq_3_opencv_dataset.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
5757
self.third_image = []
5858

5959
if self.config['GPU'] is False:
60-
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
60+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
6161

6262
self.gpu_inference = True if tf.test.gpu_device_name() else False
6363

@@ -72,13 +72,23 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
7272
print("- Models path: " + PRETRAINED_MODELS)
7373
print("- Model: " + str(model))
7474

75-
def update_frame(self, frame_id, data):
75+
def update_frame(self, frame_id, data, angular_speed=None):
7676
"""Update the information to be shown in one of the GUI's frames.
7777
7878
Arguments:
7979
frame_id {str} -- Id of the frame that will represent the data
8080
data {*} -- Data to be shown in the frame. Depending on the type of frame (rgbimage, laser, pose3d, etc)
8181
"""
82+
if angular_speed:
83+
import math
84+
x1, y1 = int(data.shape[:2][1] / 2), data.shape[:2][0] # ancho, alto
85+
length = 200
86+
angle = (90 + int(math.degrees(-angular_speed))) * 3.14 / 180.0
87+
x2 = int(x1 - length * math.cos(angle))
88+
y2 = int(y1 - length * math.sin(angle))
89+
90+
line_thickness = 2
91+
cv2.line(data, (x1, y1), (x2, y2), (0, 0, 0), thickness=line_thickness)
8292
self.handler.update_frame(frame_id, data)
8393

8494
def check_center(self, position_x):
@@ -115,17 +125,18 @@ def execute(self):
115125
'''
116126

117127
image = self.camera.getImage().data
128+
base_image = image
118129
if self.cont == 1:
119130
self.first_image = image
120-
image = self.handler.transform_image(image,self.config['ImageTranform'])
131+
image = self.handler.transform_image(image, self.config['ImageTranform'])
121132
try:
122133
if self.config['ImageCropped']:
123134
image = image[240:480, 0:640]
124135
if 'ImageSize' in self.config:
125136
img = cv2.resize(image, (self.config['ImageSize'][0], self.config['ImageSize'][1]))
126137
else:
127138
img = image
128-
self.update_frame('frame_0', img)
139+
# sself.update_frame('frame_0', img)
129140
if self.config['ImageNormalized']:
130141
AUGMENTATIONS_TEST = Compose([
131142
Normalize()
@@ -170,21 +181,22 @@ def execute(self):
170181
start_time = time.time()
171182
prediction = self.net.predict(img)
172183
self.inference_times.append(time.time() - start_time)
173-
#prediction = prediction[0]
174184
if self.config['PredictionsNormalized']:
175-
prediction_v = prediction[0][0]*(24 - (6.5)) + (6.5)
176-
prediction_w = prediction[0][1]*(7.1 - (-7.1)) + (-7.1)
185+
prediction_v = prediction[0][0] * (24 - (6.5)) + (6.5)
186+
prediction_w = prediction[0][1] * (7.1 - (-7.1)) + (-7.1)
177187
else:
178188
prediction_v = prediction[0][0]
179189
prediction_w = prediction[0][1]
180190
if prediction_w != '' and prediction_w != '':
181191
self.motors.sendV(prediction_v)
182192
self.motors.sendW(prediction_w)
183193

194+
self.update_frame('frame_0', base_image, prediction_w)
195+
184196
if self.previous_v != None:
185197
a = np.array((prediction[0][0], prediction[0][1]))
186198
b = np.array((self.previous_v, self.previous_w))
187-
distance = np.linalg.norm(a-b)
199+
distance = np.linalg.norm(a - b)
188200
self.suddenness_distance.append(distance)
189201
self.previous_v = prediction[0][0]
190202
self.previous_w = prediction[0][1]

behavior_metrics/brains/f1/brain_f1_opencv.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def __init__(self, sensors, actuators, handler, config=None):
5050
self.cont = 0
5151
self.iteration = 0
5252

53-
#self.previous_timestamp = 0
54-
#self.previous_image = 0
53+
# self.previous_timestamp = 0
54+
# self.previous_image = 0
5555

5656
self.previous_v = None
5757
self.previous_w = None
@@ -66,7 +66,23 @@ def __init__(self, sensors, actuators, handler, config=None):
6666
'''
6767
time.sleep(2)
6868

69-
def update_frame(self, frame_id, data):
69+
def update_frame(self, frame_id, data, angular_speed=None):
70+
"""Update the information to be shown in one of the GUI's frames.
71+
72+
Arguments:
73+
frame_id {str} -- Id of the frame that will represent the data
74+
data {*} -- Data to be shown in the frame. Depending on the type of frame (rgbimage, laser, pose3d, etc)
75+
"""
76+
if angular_speed:
77+
import math
78+
x1, y1 = int(data.shape[:2][1] / 2), data.shape[:2][0] # ancho, alto
79+
length = 200
80+
angle = (90 + int(math.degrees(-angular_speed))) * 3.14 / 180.0
81+
x2 = int(x1 - length * math.cos(angle))
82+
y2 = int(y1 - length * math.sin(angle))
83+
84+
line_thickness = 2
85+
cv2.line(data, (x1, y1), (x2, y2), (0, 0, 0), thickness=line_thickness)
7086
self.handler.update_frame(frame_id, data)
7187

7288
def collinear3(self, x1, y1, x2, y2, x3, y3):
@@ -115,8 +131,8 @@ def execute(self):
115131
self.previous_timestamp = timestamp
116132
if (timestamp - self.previous_timestamp >= 0.085):
117133
self.previous_image = self.camera.getImage().data
134+
image = self.previous_image
118135
'''
119-
#image = self.previous_image
120136

121137
image = self.camera.getImage().data
122138
if image.shape == (3, 3, 3):
@@ -144,9 +160,6 @@ def execute(self):
144160
image_mask = cv2.inRange(image_hsv, red_lower, red_upper)
145161
# image_eroded = cv2.erode(image_mask, kernel, iterations=3)
146162

147-
# show image in gui -> frame_0
148-
self.update_frame('frame_0', image)
149-
150163
rows, cols = image_mask.shape
151164
rows = rows - 1 # para evitar desbordamiento
152165

@@ -202,6 +215,8 @@ def execute(self):
202215
self.motors.sendW(w)
203216
self.motors.sendV(v)
204217

218+
self.update_frame('frame_0', image, w)
219+
205220
v = np.interp(np.array([v]), (6.5, 24), (0, 1))[0]
206221
w = np.interp(np.array([w]), (-7.1, 7.1), (0, 1))[0]
207222
if self.previous_v != None:

0 commit comments

Comments
 (0)