-
Notifications
You must be signed in to change notification settings - Fork 3
/
anotacion.py
215 lines (167 loc) · 5.83 KB
/
anotacion.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
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
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
import subprocess, os, signal, sys, time, threading, rosbag, yaml, numpy as np, roslib, cv2, rospy, pyautogui, pynput
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage
from cv_bridge import CvBridge, CvBridgeError
from ros import rosbag
from std_msgs.msg import String
roslib.load_manifest('sensor_msgs')
from sensor_msgs.msg import Image
from pynput.mouse import Listener
from sensor_msgs.msg import CompressedImage
from rosbag import Bag
from tkinter import *
global bag_file, feature_file, input_topic, 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):
global bag_file, feature_file, input_topic, append, start_time, pause, buff, time_buff, buff_size, counter, current, framerate, step, compressed, image_pub, new_bag
rospy.init_node('image_feature', anonymous=True)
#ic = image_feature()
#new_bag = "prueba.bag"
image_pub = rospy.Publisher("/output/image_raw/compressed", CompressedImage, queue_size=10)
if (len(argv) < 3):
print("Necesita al menos 3 argumentos. (Ruta absoluta bag/Topico)")
exit(0)
#Abrir bag y obtener frame
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']
#Obtener el nombre del topico
input_topic = argv[2]
#Parametros
print ("Parametros: ","\n\t- Archivo bag: ", bag_file, "\n\t- Topico: ", input_topic)
print ("\nTopicos Rosbag encontrados: ")
for top in topics:
print("\t- ", top["topic"], "\n\t\t-Tipo: ", topic["type"],"\n\t\t-Fps: ", topic["frequency"])
#Comprobar la compresion del topico
if 'CompressedImage' in topic_type:
compressed = True
#Obtener fps
framerate = messages/duration
step = framerate/5
#Crear archivo de resultados
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 = []
#Bucle recorrido bag
for topic, msg, t in bag.read_messages(topics=[input_topic]):
current = counter
#Obtener fotograma
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
#Asociar imagen buffer
buff.append(msg)
time_buff.append(t.to_sec() - start_time.to_sec())
#Mostrar frame
cv2.imshow("Image", cv_image)
keyPressed(cv_image, file_obj)
#Pausar imagen
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(cv_image, 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(cv_image, 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
cv2.destroyAllWindows()
bag.close()
new_bag.close()
#Controles aplicacion
def keyPressed(cv_image, file_obj, key = None):
global bag_file, prev_frame, pause, counter, time_buff, counter, current, framerate, step, x_mouse, y_mouse
key = cv2.waitKey(int(round(1000/framerate)))
if key == -1:
return
if key & 0xFF == 27:
cv2.destroyAllWindows()
exit(0)
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('t'):
x_mouse, y_mouse = pyautogui.position()
if key & 0xFF == ord('c'):
x1_mouse, y1_mouse = pyautogui.position()
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
cv2.putText(cv_image,'Car',(x_mouse-100,y_mouse-110), font, 0.85,(0,255,0),1,cv2.LINE_AA)
cv2.rectangle(cv_image,(x_mouse-100,y_mouse-100),(x1_mouse-100,y1_mouse-100),(0,255,0),3)
publicar(cv_image)
file_obj.write(str(time_buff[counter]) + "\car\n")
cv2.imshow("I", cv_image)
if key & 0xFF == ord('p'):
x1_mouse, y1_mouse = pyautogui.position()
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
cv2.putText(cv_image,'Person',(x_mouse-100,y_mouse-110), font, 0.85,(0,255,0),1,cv2.LINE_AA)
cv2.rectangle(cv_image,(x_mouse-100,y_mouse-100),(x1_mouse-100,y1_mouse-100),(0,255,0),3)
publicar(cv_image)
file_obj.write(str(time_buff[counter]) + "\person\n")
cv2.imshow("I", cv_image)
if key & 0xFF == ord('s'):
x1_mouse, y1_mouse = pyautogui.position()
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
cv2.putText(cv_image,'Signal',(x_mouse-100,y_mouse-110), font, 0.85,(0,255,0),1,cv2.LINE_AA)
cv2.rectangle(cv_image,(x_mouse-100,y_mouse-100),(x1_mouse-100,y1_mouse-100),(0,255,0),3)
publicar(cv_image)
file_obj.write(str(time_buff[counter]) + "\tsignal\n")
cv2.imshow("I", cv_image)
if key & 0xFF == ord(' '):
pause_time = None
if pause is True:
pause = False
else:
pause = True
def publicar(cv_image):
msg = CompressedImage()
msg.header.stamp = rospy.Time.now()
msg.format = "jpeg"
msg.data = np.array(cv2.imencode('.jpg', cv_image)[1]).tobytes()
if __name__ =='__main__':
main(sys.argv)
#python3 anotacion.py /home/roberott/Downloads/coche.bag /cv_camera/image_raw