-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (42 loc) · 1.58 KB
/
utils.py
File metadata and controls
58 lines (42 loc) · 1.58 KB
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
import datetime
import os
def create_img_directories(output_directory, topics):
for topic in topics:
path = os.path.join(output_directory, directoryname_from_topic(topic))
try:
os.makedirs(path, exist_ok=True)
except OSError as error:
print(f"Error: {error}")
def directoryname_from_topic(topic):
dn = topic.replace('/', '--')
# try to find the a suitable directory name:
idx = find_first_of_substrings(dn, ['left', 'right', 'center', 'depth'])
if idx != -1:
dn = dn[idx:]
return dn
def find_first_of_substrings(string, substrings):
for substring in substrings:
index = string.find(substring)
if index != -1:
return index
return -1
def filename_from_msg(msg):
# TODO if milliseconds of timestampt not available, then this collapses all images
# within a second into one
t = msg.header.stamp.to_sec()
dt_object = datetime.datetime.fromtimestamp(t) # turn into datetime object
# format to year,month,day,hour,minute,second
formatted_stamp = dt_object.strftime("%Y%m%d-%H%M%S")
# add non-decimal part of seconds at the end
return f"{formatted_stamp}-{str(t)[11:15]}"
def get_img_topics(bag, topics, img_topics_list):
img_topics = set()
covered_topics = set()
all_topics = set(topics)
for topic, msg, t in bag.read_messages(topics=topics):
if msg._type in img_topics_list:
img_topics.add(topic)
covered_topics.add(topic)
if covered_topics == all_topics:
break
return list(img_topics)