-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
72 lines (56 loc) · 2.28 KB
/
utils.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
import numpy as np
import cv2
import math
def add_arrow(image, action):
# action: [steering_angle, linear_speed]
# image: cv2 read image
start_pos = (320, 320) # TODO: Will change this
# print('action: {}'.format(action))
# Calculate the length according the linear speed
# The maximum velocity is 0.2 m/s
arrow_len = 100 * abs(action[1])/0.6 # TODO: check this again
# Calculate the ending point
action_x = math.ceil(arrow_len * math.sin(action[0]))
action_y = math.ceil(arrow_len * math.cos(action[0]))
# Signs are for pictures:
# up means negative in y pixel axis, and right means positive
if action[1] > 0:
end_pos = (start_pos[0] + action_x, start_pos[1] - action_y)
else:
end_pos = (start_pos[0] - action_x, start_pos[1] + action_y)
cv2.arrowedLine(image, start_pos, end_pos, (0,255,255), 6)
image[start_pos[0]-1:start_pos[0]+1, start_pos[1]-1:start_pos[1]+1] = (0,0,0)
return image
def construct_run_command(script, arguments):
command = f'python3 {script}'
for k, v in arguments.items():
if isinstance(v, bool):
if v:
command += f' --{k}'
else:
command += f' --{k} {str(v)}'
return command
# Expects a list of dictionaries
def construct_variants(variants, default_dict=dict(), name_key=None):
level_keys = []
variant_levels = []
for var_level in variants:
keys, values = zip(*var_level.items())
assert all([len(v) == len(values[0]) for v in values])
variants = list(zip(*values))
level_keys.append(keys)
variant_levels.append(variants)
all_keys = sum(level_keys, tuple())
all_variants = list(itertools.product(*variant_levels))
all_variants = [sum(v, tuple()) for v in all_variants]
assert all([len(v) == len(all_keys) for v in all_variants])
final_variants = []
for variant in all_variants:
d = default_dict.copy()
d.update({k: v for k, v in zip(all_keys, variant)})
if name_key:
d[name_key] = '_'.join([f"[{k}]_{v.replace('/', '_') if type(v) == str else v}" for k, v in zip(all_keys, variant)])
final_variants.append(d)
return final_variants
# Methods to calculate similarity between embeddings
# def euclidean_similarity(z1, z2):