Skip to content

Commit 8ba3423

Browse files
committed
Extended for both TF brains
1 parent 46e5778 commit 8ba3423

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@
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/'
2726

28-
import math
27+
2928

3029
class Brain:
3130
"""Specific brain for the f1 robot. See header."""

0 commit comments

Comments
 (0)