-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathannotator.py
More file actions
executable file
·206 lines (181 loc) · 5.57 KB
/
annotator.py
File metadata and controls
executable file
·206 lines (181 loc) · 5.57 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
import roslib
import cv2
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import rospy
from std_msgs.msg import String
import subprocess
import signal
import os
import sys
import time
import threading
import rosbag
import yaml
import numpy as np
global bag_file, feature_file, input_topic
global pause, buff, time_buff, buff_size, counter, current, framerate, step, start_time, compressed
pause = compressed = append = False
counter = 0
pause_time = None
buff_size = 100
input_topic = None
def main(argv):
"""
Input : @list argv
Output : -
Description : Parses the arguments and initializes the main process loop accordingly
"""
global bag_file, feature_file, input_topic, append
global start_time, pause, buff, time_buff, buff_size, counter, current, framerate, step, compressed
# Process args
if (len(argv) >= 2):
if '-a' in argv:
append = True
if '-h' in argv or '-help' in argv or 'help' in argv:
print "\nThis script annotates a rosbag file and creates a result file"
print "\nAvailable arguments"
print "\t- First argument, rosbag file, absolute path"
print "\t- Second argument, topic to be used for annotation, e.g. camera/rgb/image_raw"
print "\nOptional arguments"
print "\t-a: Append result file instead of creating new"
print "\nInformation on Keys:"
print "\tEsc: Quits"
print "\ta: Go back 1 frame"
print "\td: Go forward 1 frame"
print "\tz: Writes the timestamp on the result file with id 4"
print "\te: Writes the timestamp on the result file with id 3"
print "\tq: Writes the timestamp on the result file with id 2"
print "\tw: Writes the timestamp on the result file with id 1"
print "\ts: Writes the timestamp on the result file with id 0"
print "\tspace: Pause image"
print "\t<-: Reduce playback speed"
print "\t->: Increase playback speed"
exit(0)
elif (len(argv) < 3):
print"Too few arguments, type -h for more info"
exit(0)
else:
print"Too few arguments, type -h for more info"
exit(0)
#Open bag and get framerate
bag_file = argv[1]
bag = rosbag.Bag(bag_file)
info_dict = yaml.load(bag._get_yaml_info())
topics = info_dict['topics']
topic = topics[1]
messages = topic['messages']
duration = info_dict['duration']
topic_type = topic['type']
#Get the topic name
input_topic = argv[2]
#Messages for test
print "Script parameters: ","\n\t- Bag file: ", bag_file, "\n\t- Topic: ", input_topic,
print "\nRosbag topics found: "
for top in topics:
print "\t- ", top["topic"], "\n\t\t-Type: ", topic["type"],"\n\t\t-Fps: ", topic["frequency"]
#Checking if the topic is compressed
if 'CompressedImage' in topic_type:
compressed = True
#Get framerate
framerate = messages/duration
step = framerate/5
#Create results file
feature_file = ((bag_file.split(".")[0]).split("/")[-1]) + "_RES"
if os.path.exists(feature_file):
if not append:
os.remove(feature_file)
file_obj = open(feature_file, 'a')
bridge = CvBridge()
buff = []
time_buff = []
#Loop through the rosbag
for topic, msg, t in bag.read_messages(topics=[input_topic]):
current = counter
#Get the image
if not compressed:
try:
cv_image = bridge.imgmsg_to_cv2(msg, "bgr8")
except CvBridgeError as e:
print(e)
else:
nparr = np.fromstring(msg.data, np.uint8)
cv_image = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
if counter == 0:
start_time = t
#Append image buffer and time buffer
buff.append(msg)
time_buff.append(t.to_sec() - start_time.to_sec())
#Display image
cv2.imshow("Image", cv_image)
keyPressed(file_obj)
#If the image is paused
while(pause):
if not compressed:
cv2.imshow("Image", bridge.imgmsg_to_cv2(buff[counter], "bgr8"))
else:
nparr = np.fromstring(buff[counter].data, np.uint8)
cv2.imshow("Image", cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR))
keyPressed(file_obj)
if counter < current and not pause:
for msg in buff[counter::]:
if not compressed:
cv2.imshow("Image", bridge.imgmsg_to_cv2(msg, "bgr8"))
else:
nparr = np.fromstring(msg.data, np.uint8)
cv2.imshow("Image", cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR))
keyPressed(file_obj)
if pause:
break
if counter < current:
counter += 1
if len(buff) >= buff_size:
del buff[0:10]
if len(time_buff) >= buff_size:
del time_buff[0:10]
counter -= 10
counter += 1
bag.close()
def keyPressed(file_obj, key = None):
global bag_file, prev_frame, pause, counter, time_buff, counter, current, framerate, step
key = cv2.waitKey(int(round(1000/framerate)));
if key == -1:
return
if key & 0xFF == 27:
cv2.destroyAllWindows()
exit(0)
if key == 1113937:
if framerate - step > 0:
framerate = framerate - step
if key == 1113939:
framerate = framerate + step
if key & 0xFF == ord('a'):
pause = True
if counter == 0:
return
counter -= 1
if key & 0xFF == ord('d'):
pause = True
if current == counter:
return
counter += 1
if key & 0xFF == ord('s'):
file_obj.write(str(time_buff[counter]) + "\t0\n")
if key & 0xFF == ord('w'):
file_obj.write(str(time_buff[counter]) + "\t1\n")
if key & 0xFF == ord('q'):
file_obj.write(str(time_buff[counter]) + "\t2\n")
if key & 0xFF == ord('e'):
file_obj.write(str(time_buff[counter]) + "\t3\n")
if key & 0xFF == ord('z'):
file_obj.write(str(time_buff[counter]) + "\t4\n")
if key & 0xFF == ord(' '):
pause_time = None
if pause is True:
pause = False
else:
pause = True
if __name__ =='__main__':
main(sys.argv)