Skip to content

Commit b446b4c

Browse files
Merge pull request #378 from JdeRobot/issue-377
Updated opencv brain displaying predicted values
2 parents 24152aa + 2f3e9aa commit b446b4c

File tree

3 files changed

+90
-46
lines changed

3 files changed

+90
-46
lines changed

behavior_metrics/brains/f1/brain_f1_keras_opencv_dataset.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
1010
"""
1111

12-
import tensorflow as tf
13-
import numpy as np
1412
import cv2
15-
import time
13+
import math
14+
import numpy as np
1615
import os
17-
18-
from utils.constants import PRETRAINED_MODELS_DIR, ROOT_PATH
19-
from os import path
16+
import tensorflow as tf
17+
import time
2018
from albumentations import (
2119
Compose, Normalize
2220
)
21+
from os import path
22+
from utils.constants import PRETRAINED_MODELS_DIR, ROOT_PATH
2323
from utils.gradcam.gradcam import GradCAM
2424

2525
PRETRAINED_MODELS = ROOT_PATH + '/' + PRETRAINED_MODELS_DIR + 'tf_models/'
@@ -49,6 +49,7 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
4949
self.suddenness_distance = []
5050
self.previous_v = None
5151
self.previous_w = None
52+
self.previous_w_normalized = None
5253

5354
if self.config['GPU'] is False:
5455
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
@@ -67,23 +68,35 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
6768
print("- Models path: " + PRETRAINED_MODELS)
6869
print("- Model: " + str(model))
6970

70-
def update_frame(self, frame_id, data, angular_speed=None):
71+
def update_frame(self, frame_id, data, current_angular_speed=None, previous_angular_speed=None, distance=None):
7172
"""Update the information to be shown in one of the GUI's frames.
7273
7374
Arguments:
7475
frame_id {str} -- Id of the frame that will represent the data
7576
data {*} -- Data to be shown in the frame. Depending on the type of frame (rgbimage, laser, pose3d, etc)
7677
"""
77-
if angular_speed:
78-
import math
78+
if current_angular_speed:
79+
data = np.array(data, copy=True)
80+
7981
x1, y1 = int(data.shape[:2][1] / 2), data.shape[:2][0] # ancho, alto
8082
length = 200
81-
angle = (90 + int(math.degrees(-angular_speed))) * 3.14 / 180.0
83+
angle = (90 + int(math.degrees(-current_angular_speed))) * 3.14 / 180.0
8284
x2 = int(x1 - length * math.cos(angle))
8385
y2 = int(y1 - length * math.sin(angle))
8486

85-
line_thickness = 2
87+
line_thickness = 10
8688
cv2.line(data, (x1, y1), (x2, y2), (0, 0, 0), thickness=line_thickness)
89+
length = 150
90+
angle = (90 + int(math.degrees(-previous_angular_speed))) * 3.14 / 180.0
91+
x2 = int(x1 - length * math.cos(angle))
92+
y2 = int(y1 - length * math.sin(angle))
93+
94+
cv2.line(data, (x1, y1), (x2, y2), (255, 0, 0), thickness=line_thickness)
95+
if float(distance) > 0.01:
96+
cv2.putText(data, distance, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
97+
else:
98+
cv2.putText(data, distance, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
99+
87100
self.handler.update_frame(frame_id, data)
88101

89102
def execute(self):
@@ -99,7 +112,7 @@ def execute(self):
99112
self.first_image = image
100113

101114
image = self.handler.transform_image(image, self.config['ImageTranform'])
102-
#self.update_frame('frame_0', image)
115+
self.update_frame('frame_0', image)
103116

104117
try:
105118
if self.config['ImageCropped']:
@@ -132,6 +145,7 @@ def execute(self):
132145
self.motors.sendV(prediction_v)
133146
self.motors.sendW(prediction_w)
134147

148+
current_w_normalized = prediction_w
135149
if self.previous_v != None:
136150
a = np.array((prediction[0][0], prediction[0][1]))
137151
b = np.array((self.previous_v, self.previous_w))
@@ -140,7 +154,9 @@ def execute(self):
140154
self.previous_v = prediction[0][0]
141155
self.previous_w = prediction[0][1]
142156

143-
self.update_frame('frame_0', base_image, prediction_w)
157+
if self.previous_w_normalized != None:
158+
self.update_frame('frame_2', base_image, current_w_normalized, self.previous_w_normalized, str(round(distance, 4)))
159+
self.previous_w_normalized = current_w_normalized
144160

145161
# GradCAM from image
146162
i = np.argmax(prediction[0])

behavior_metrics/brains/f1/brain_f1_keras_seq_3_opencv_dataset.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99
1010
"""
1111

12-
import numpy as np
13-
1412
import cv2
15-
import time
13+
import math
14+
import numpy as np
1615
import os
1716
import tensorflow as tf
18-
19-
from utils.constants import PRETRAINED_MODELS_DIR, ROOT_PATH
20-
from os import path
17+
import time
2118
from albumentations import (
2219
Compose, Normalize
2320
)
21+
from os import path
22+
from utils.constants import PRETRAINED_MODELS_DIR, ROOT_PATH
2423
from utils.gradcam.gradcam import GradCAM
2524

2625
PRETRAINED_MODELS = ROOT_PATH + '/' + PRETRAINED_MODELS_DIR + 'tf_models/'
@@ -53,6 +52,7 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
5352
self.suddenness_distance = []
5453
self.previous_v = None
5554
self.previous_w = None
55+
self.previous_w_normalized = None
5656

5757
self.third_image = []
5858

@@ -72,23 +72,35 @@ 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, angular_speed=None):
75+
def update_frame(self, frame_id, data, current_angular_speed=None, previous_angular_speed=None, distance=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
82+
if current_angular_speed:
83+
data = np.array(data, copy=True)
84+
8485
x1, y1 = int(data.shape[:2][1] / 2), data.shape[:2][0] # ancho, alto
8586
length = 200
86-
angle = (90 + int(math.degrees(-angular_speed))) * 3.14 / 180.0
87+
angle = (90 + int(math.degrees(-current_angular_speed))) * 3.14 / 180.0
8788
x2 = int(x1 - length * math.cos(angle))
8889
y2 = int(y1 - length * math.sin(angle))
8990

90-
line_thickness = 2
91+
line_thickness = 10
9192
cv2.line(data, (x1, y1), (x2, y2), (0, 0, 0), thickness=line_thickness)
93+
length = 150
94+
angle = (90 + int(math.degrees(-previous_angular_speed))) * 3.14 / 180.0
95+
x2 = int(x1 - length * math.cos(angle))
96+
y2 = int(y1 - length * math.sin(angle))
97+
98+
cv2.line(data, (x1, y1), (x2, y2), (255, 0, 0), thickness=line_thickness)
99+
if float(distance) > 0.01:
100+
cv2.putText(data, distance, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
101+
else:
102+
cv2.putText(data, distance, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
103+
92104
self.handler.update_frame(frame_id, data)
93105

94106
def check_center(self, position_x):
@@ -136,7 +148,7 @@ def execute(self):
136148
img = cv2.resize(image, (self.config['ImageSize'][0], self.config['ImageSize'][1]))
137149
else:
138150
img = image
139-
# sself.update_frame('frame_0', img)
151+
self.update_frame('frame_0', img)
140152
if self.config['ImageNormalized']:
141153
AUGMENTATIONS_TEST = Compose([
142154
Normalize()
@@ -191,8 +203,7 @@ def execute(self):
191203
self.motors.sendV(prediction_v)
192204
self.motors.sendW(prediction_w)
193205

194-
self.update_frame('frame_0', base_image, prediction_w)
195-
206+
current_w_normalized = prediction_w
196207
if self.previous_v != None:
197208
a = np.array((prediction[0][0], prediction[0][1]))
198209
b = np.array((self.previous_v, self.previous_w))
@@ -201,6 +212,10 @@ def execute(self):
201212
self.previous_v = prediction[0][0]
202213
self.previous_w = prediction[0][1]
203214

215+
if self.previous_w_normalized != None and distance:
216+
self.update_frame('frame_1', base_image, current_w_normalized, self.previous_w_normalized, str(round(distance, 4)))
217+
self.previous_w_normalized = current_w_normalized
218+
204219

205220
except Exception as err:
206221
print(err)

behavior_metrics/brains/f1/brain_f1_opencv.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
import threading
4-
import time
5-
3+
import csv
64
import cv2
5+
import math
76
import numpy as np
7+
import threading
8+
import time
9+
from albumentations import (
10+
Compose, Normalize, RandomRain, RandomBrightness, RandomShadow, RandomSnow, RandomFog, RandomSunFlare
11+
)
12+
from utils.constants import DATASETS_DIR, ROOT_PATH
813

9-
time_cycle = 80
1014
error = 0
1115
integral = 0
1216
v = 0
1317
w = 0
1418
current = 'straight'
15-
time_cycle = 80
1619
RED = (255, 0, 0)
1720
GREEN = (0, 255, 0)
1821
BLUE = (0, 0, 255)
@@ -24,15 +27,8 @@
2427
V_MULT = 2
2528
v_mult = V_MULT
2629

27-
import csv
28-
from utils.constants import DATASETS_DIR, ROOT_PATH
29-
3030
GENERATED_DATASETS_DIR = ROOT_PATH + '/' + DATASETS_DIR
3131

32-
from albumentations import (
33-
Compose, Normalize, RandomRain, RandomBrightness, RandomShadow, RandomSnow, RandomFog, RandomSunFlare
34-
)
35-
3632

3733
class Brain:
3834

@@ -55,6 +51,7 @@ def __init__(self, sensors, actuators, handler, config=None):
5551

5652
self.previous_v = None
5753
self.previous_w = None
54+
self.previous_w_normalized = None
5855
self.suddenness_distance = []
5956

6057
# Save dataset
@@ -66,23 +63,35 @@ def __init__(self, sensors, actuators, handler, config=None):
6663
'''
6764
time.sleep(2)
6865

69-
def update_frame(self, frame_id, data, angular_speed=None):
66+
def update_frame(self, frame_id, data, current_angular_speed=None, previous_angular_speed=None, distance=None):
7067
"""Update the information to be shown in one of the GUI's frames.
7168
7269
Arguments:
7370
frame_id {str} -- Id of the frame that will represent the data
7471
data {*} -- Data to be shown in the frame. Depending on the type of frame (rgbimage, laser, pose3d, etc)
7572
"""
76-
if angular_speed:
77-
import math
73+
if current_angular_speed:
74+
data = np.array(data, copy=True)
75+
7876
x1, y1 = int(data.shape[:2][1] / 2), data.shape[:2][0] # ancho, alto
7977
length = 200
80-
angle = (90 + int(math.degrees(-angular_speed))) * 3.14 / 180.0
78+
angle = (90 + int(math.degrees(-current_angular_speed))) * 3.14 / 180.0
8179
x2 = int(x1 - length * math.cos(angle))
8280
y2 = int(y1 - length * math.sin(angle))
8381

84-
line_thickness = 2
82+
line_thickness = 10
8583
cv2.line(data, (x1, y1), (x2, y2), (0, 0, 0), thickness=line_thickness)
84+
length = 150
85+
angle = (90 + int(math.degrees(-previous_angular_speed))) * 3.14 / 180.0
86+
x2 = int(x1 - length * math.cos(angle))
87+
y2 = int(y1 - length * math.sin(angle))
88+
89+
cv2.line(data, (x1, y1), (x2, y2), (255, 0, 0), thickness=line_thickness)
90+
if float(distance) > 0.01:
91+
cv2.putText(data, distance, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
92+
else:
93+
cv2.putText(data, distance, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
94+
8695
self.handler.update_frame(frame_id, data)
8796

8897
def collinear3(self, x1, y1, x2, y2, x3, y3):
@@ -215,7 +224,8 @@ def execute(self):
215224
self.motors.sendW(w)
216225
self.motors.sendV(v)
217226

218-
self.update_frame('frame_0', image, w)
227+
self.update_frame('frame_0', image)
228+
current_w_normalized = w
219229

220230
v = np.interp(np.array([v]), (6.5, 24), (0, 1))[0]
221231
w = np.interp(np.array([w]), (-7.1, 7.1), (0, 1))[0]
@@ -227,6 +237,10 @@ def execute(self):
227237
self.previous_v = v
228238
self.previous_w = w
229239

240+
if self.previous_w_normalized != None:
241+
self.update_frame('frame_2', image, current_w_normalized, self.previous_w_normalized, str(round(distance, 4)))
242+
self.previous_w_normalized = current_w_normalized
243+
230244
'''
231245
if (save_dataset):
232246
# Save dataset
@@ -250,4 +264,3 @@ def execute(self):
250264
(10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, MAGENTA, 2, cv2.LINE_AA)
251265

252266
self.update_frame('frame_1', image_mask)
253-

0 commit comments

Comments
 (0)