-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrobot_utils.py
292 lines (257 loc) · 10.1 KB
/
robot_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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import cv2
import time
from collections import deque
import numpy as np
from threading import Thread
from typing import Union
import os
class ImageRecorderRos:
def __init__(
self, camera_names, is_debug=False, topic_names=None, show_images=True
):
print("Starting image recorder...")
from collections import deque
import rospy
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
self.is_debug = is_debug
self.show_images = show_images
self.bridge = CvBridge()
self.camera_names = camera_names
self.image_info = {"raw_image": {}, "secs": {}, "nsecs": {}, "timestamps": {}}
for c, cam_name in enumerate(camera_names):
topic_name = (
f"/usb_cam_{cam_name}/image_raw"
if topic_names is None
else topic_names[c]
)
rospy.wait_for_message(topic_name, Image, timeout=3)
rospy.Subscriber(topic_name, Image, self.image_callback, (cam_name))
if self.is_debug:
setattr(self, f"{cam_name}_timestamps", deque(maxlen=50))
time.sleep(0.5)
print("Image recorder started.")
def close(self):
pass
def image_callback(self, data, cam_name):
self.image_info["raw_image"][cam_name] = self.bridge.imgmsg_to_cv2(
data, desired_encoding="passthrough"
)
self.image_info["secs"][cam_name] = data.header.stamp.secs
self.image_info["nsecs"][cam_name] = data.header.stamp.nsecs
if self.show_images:
cv2.imshow(cam_name, self.image_info["raw_image"][cam_name])
cv2.waitKey(1)
if self.is_debug:
self.image_info["timestamps"][cam_name].append(
data.header.stamp.secs + data.header.stamp.secs * 1e-9
)
def get_images(self):
return self.image_info["raw_image"]
def print_diagnostics(self):
def dt_helper(l):
l = np.array(l)
diff = l[1:] - l[:-1]
return np.mean(diff)
for cam_name in self.camera_names:
image_freq = 1 / dt_helper(self.image_info["timestamps"][cam_name])
print(f"{cam_name} {image_freq=:.2f}")
print()
class ImageRecorderVideo:
def __init__(
self,
cameras: Union[dict, list],
is_debug=False,
image_shape=(480, 640, 3),
show_images=False,
fps=30,
):
print("Starting image recorder...")
self.is_debug = is_debug
self.show_images = show_images
self.fps = fps
if isinstance(cameras, dict):
camera_names = list(cameras.keys())
camera_indices = list(cameras.values())
self.name2index = cameras
self.index2name = {index: name for name, index in cameras.items()}
else:
camera_names = [f"camera_{index}" for index in cameras]
camera_indices = cameras
self.name2index = {
name: index for name, index in zip(camera_names, camera_indices)
}
self.index2name = {
index: name for name, index in zip(camera_names, camera_indices)
}
self.camera_names = camera_names
self.camera_indices = camera_indices
self.image_info = {"raw_image": {}, "secs": {}, "nsecs": {}}
self.cap = {index: cv2.VideoCapture(int(index)) for index in camera_indices}
# check
for index in camera_indices:
if not self.cap[index].isOpened():
raise Exception(f"Failed to open camera {index}")
else:
ret, frame = self.cap[index].read()
if not ret:
raise Exception(f"Failed to read from camera {index}")
else:
assert tuple(frame.shape) == image_shape
if self.is_debug:
self.timestamps = {index: deque(maxlen=50) for index in cameras}
self.is_running = True
self.img_reading_thread = Thread(target=self._image_reading, daemon=True)
self.img_reading_thread.start()
print("Image video recorder started.")
def close(self):
self.is_running = False
self.img_reading_thread.join()
def _read_image_once(self):
images = {}
for cam_index in self.camera_indices:
ret, frame = self.cap[cam_index].read()
if ret:
images[self.index2name[cam_index]] = frame
else:
raise Exception(f"Failed to read from camera {cam_index}")
return images
def _read_one_image(self, cam_name):
ret, frame = self.cap[self.name2index[cam_name]].read()
if ret:
return frame
else:
raise Exception(f"Failed to read from camera {cam_name}")
def _image_reading(self):
duration = 1 / self.fps
if self.show_images:
# 将图像现实在一个窗口中,在图像上显示图像对应的摄像头编号
window_name = f"Camera {self.camera_indices}"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
start_time = time.time()
while self.is_running:
fps_time = start_time
image_info = {"raw_image": {}, "secs": {}, "nsecs": {}}
for cam_name in self.camera_names:
image_info["raw_image"][cam_name] = self._read_one_image(cam_name)
time_sec = int(time.time())
image_info["secs"][cam_name] = time_sec
time_ns = time.time_ns()
image_info["nsecs"][cam_name] = time_ns - time_sec
if self.is_debug:
self.timestamps[cam_name].append(time_ns * 1e-9)
# 同步更新
self.image_info = image_info
# 将图像现实在一个窗口中,在图像上显示图像对应的摄像头编号
frames = list(self.image_info["raw_image"].values())
combined_frame = np.hstack(frames)
time.sleep(max(0, duration - (time.time() - start_time)))
start_time = time.time()
if self.show_images:
fps = 1 / (time.time() - fps_time)
cv2.putText(
combined_frame,
f"FPS: {fps:.2f}",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0, 255, 0),
2,
)
cv2.imshow(window_name, combined_frame)
cv2.waitKey(1)
start_time = time.time()
for index in self.camera_indices:
self.cap[index].release()
cv2.destroyWindow(window_name)
def get_images(self):
return self.image_info["raw_image"]
def print_diagnostics(self):
def dt_helper(l):
l = np.array(l)
diff = l[1:] - l[:-1]
return np.mean(diff)
for index in self.camera_indices:
if index in self.timestamps:
image_freq = 1 / dt_helper(self.timestamps[index])
print(f"Camera {index} image frequency: {image_freq:.2f}")
else:
print(f"No timestamps available for camera {index}")
print()
class ImageRecorderFake(object):
def __init__(self, camera_names, is_debug=False, show_images=True):
print("Starting fake image recorder...")
self.is_debug = is_debug
self.show_images = show_images
self.camera_names = camera_names
self.image_info = {"raw_image": {}, "secs": {}, "nsecs": {}}
self.fake_image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
if self.is_debug:
self.timestamps = {cam_name: deque(maxlen=50) for cam_name in camera_names}
Thread(target=self._image_reading, daemon=True).start()
print("Fake image recorder started.")
def _image_reading(self):
while True:
time_sec = int(time.time())
time_ns = time.time_ns()
for cam_name in self.camera_names:
self.image_info["raw_image"][cam_name] = self.fake_image
self.image_info["secs"][cam_name] = time_sec
self.image_info["nsecs"][cam_name] = time_ns - time_sec
if self.is_debug:
self.timestamps[cam_name].append(time_ns * 1e-9)
time.sleep(1 / 30)
def close(self):
pass
def get_images(self):
return self.image_info["raw_image"]
def print_diagnostics(self):
def dt_helper(l):
l = np.array(l)
diff = l[1:] - l[:-1]
return np.mean(diff)
for cam_name in self.camera_names:
if cam_name in self.timestamps:
image_freq = 1 / dt_helper(self.timestamps[cam_name])
print(f"{cam_name} image frequency: {image_freq:.2f}")
else:
print(f"No timestamps available for camera {cam_name}")
print()
def calibrate_linear_vel(base_action: np.ndarray, c=None):
if c is None:
c = 0.0
v = base_action[..., 0]
w = base_action[..., 1]
base_action = base_action.copy()
base_action[..., 0] = v - c * w
return base_action
def smooth_base_action(base_action):
return np.stack(
[
np.convolve(base_action[:, i], np.ones(5) / 5, mode="same")
for i in range(base_action.shape[1])
],
axis=-1,
).astype(np.float32)
def postprocess_base_action(base_action):
linear_vel, angular_vel = base_action
angular_vel *= 0.9
return np.array([linear_vel, angular_vel])
if __name__ == "__main__":
show_images = False
recorder = ImageRecorderVideo(cameras=[0], is_debug=False, show_images=show_images)
while True:
if not show_images:
images = recorder.get_images()
for index, image in images.items():
cv2.imshow(f"Camera {index}", image)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
time.sleep(1)
def ping_ip(ip) -> bool:
response = os.system(f"ping -c 1 -w 2 {ip}") # 对于Windows,使用 "ping -n 1 {ip}"
if response == 0:
return True
else:
return False