Skip to content

Commit f7ec622

Browse files
committed
temp
1 parent 2138390 commit f7ec622

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

python/rcsss/camera/hw.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def start(self, warm_up: bool = True):
9494
self._thread.start()
9595

9696
def record_video(self, path: Path, episode: int):
97+
if self.recording_ongoing():
98+
return
99+
print("start recording")
100+
self.clear_buffer()
97101
for camera in self.camera_names:
98102
self.writer[camera] = cv2.VideoWriter(
99103
str(path / f"episode_{episode}_{camera}.mp4"),
@@ -104,19 +108,21 @@ def record_video(self, path: Path, episode: int):
104108
)
105109

106110
def recording_ongoing(self) -> bool:
107-
return len(self.writer) > 0
111+
with self._buffer_lock:
112+
return len(self.writer) > 0
108113

109114
def stop_video(self):
110115
if len(self.writer) > 0:
111-
for camera_key, writer in self.writer.items():
112-
for i in range(self._next_ring_index):
113-
frameset = self._buffer[i]
114-
assert frameset is not None
115-
# rgb to bgr as expected by opencv
116-
writer.write(frameset.frames[camera_key].camera.color.data[:, :, ::-1])
117-
for camera in self.camera_names:
118-
self.writer[camera].release()
119-
self.writer = {}
116+
with self._buffer_lock:
117+
for camera_key, writer in self.writer.items():
118+
for i in range(self._next_ring_index):
119+
frameset = self._buffer[i]
120+
assert frameset is not None
121+
# rgb to bgr as expected by opencv
122+
writer.write(frameset.frames[camera_key].camera.color.data[:, :, ::-1])
123+
for camera in self.camera_names:
124+
self.writer[camera].release()
125+
self.writer = {}
120126

121127
def warm_up(self):
122128
for _ in range(self.config.warm_up_disposal_frames):
@@ -138,7 +144,8 @@ def polling_thread(self, warm_up: bool = True):
138144
writer.write(frameset.frames[camera_key].camera.color.data[:, :, ::-1])
139145
self._next_ring_index = (self._next_ring_index + 1) % self.config.max_buffer_frames
140146
self._buffer_len = max(self._buffer_len + 1, self.config.max_buffer_frames)
141-
self.rate(self.config.frame_rate)
147+
# self.rate(self.config.frame_rate)
148+
sleep(1 / self.config.frame_rate)
142149

143150
def poll_frame_set(self) -> FrameSet:
144151
"""Gather frames over all available cameras."""

python/rcsss/control/vive.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
INCLUDE_ROTATION = True
4242
ROBOT_IP = "192.168.101.1"
4343
ROBOT_INSTANCE = RobotInstance.HARDWARE
44-
DEBUG = True
44+
DEBUG = False
4545

4646

4747
class Button(IntFlag):
@@ -223,6 +223,10 @@ def input_loop(env_rel, action_server: UDPViveActionServer, camera_set: RealSens
223223
# record videos
224224
video_path = env_rel.path / "videos"
225225
video_path.mkdir(parents=True, exist_ok=True)
226+
# camera_set.record_video(env_rel.path / "videos", env_rel.episode_count)
227+
228+
gif_path = env_rel.path / "gifs"
229+
gif_path.mkdir(parents=True, exist_ok=True)
226230
print(f'{env_rel.episode_count = }')
227231

228232
thread = threading.Thread(target=action_server.environment_step_loop)
@@ -252,7 +256,7 @@ def main():
252256
"side": "243522070385",
253257
}
254258
camera_set = default_realsense(camera_dict)
255-
camera_set = None
259+
# camera_set = None
256260
env_rel = fr3_hw_env(
257261
ip=ROBOT_IP,
258262
camera_set = camera_set,
@@ -261,7 +265,7 @@ def main():
261265
control_mode=ControlMode.CARTESIAN_TQuart,
262266
# control_mode=ControlMode.JOINTS,
263267
gripper_cfg=default_fr3_hw_gripper_cfg(),
264-
max_relative_movement=(0.01, np.deg2rad(5)),
268+
max_relative_movement=(0.5, np.deg2rad(90)),
265269
# max_relative_movement=(0.5, np.deg2rad(90)),
266270
# TODO: max should be always according to the last step
267271
# max_relative_movement=np.deg2rad(20),
@@ -283,7 +287,7 @@ def main():
283287
env_rel.get_wrapper_attr("sim").open_gui()
284288

285289
if not DEBUG:
286-
env_rel = StorageWrapper(env_rel, path="/home/juelg/code/frankcsy/record_real_christmas", camera_set=camera_set)
290+
env_rel = StorageWrapper(env_rel, path="/home/juelg/test_data", camera_set=camera_set)
287291
# ip_secondary = "192.168.102.1"
288292
# with Desk.fci(ip_secondary, user, pw):
289293
# f = rcsss.hw.FR3(ip_secondary)
@@ -299,7 +303,7 @@ def main():
299303
with env_rel:
300304
with UDPViveActionServer(VIVE_HOST, VIVE_PORT, env_rel) as action_server:
301305
if not DEBUG:
302-
input_loop(env_rel, action_server, None)
306+
input_loop(env_rel, action_server, camera_set)
303307

304308
else:
305309
action_server.environment_step_loop()

python/rcsss/envs/wrappers.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class StorageWrapper(gym.Wrapper):
1919
# TODO: this should also record the instruction
2020
FILE = "episode_{}.npz"
21-
GIF = "{}_episode_{}_{}.gif"
21+
GIF = "gifs/{}_episode_{}_{}.gif"
2222
FOLDER = "experiment_{}"
2323
GIF_DURATION_S = 0.5
2424

@@ -40,6 +40,7 @@ def __init__(
4040
self.record_numpy = record_numpy
4141
self.gif = gif
4242
self.camera_set = camera_set
43+
print("camera_set", self.camera_set)
4344

4445
# make folders
4546
self.path = Path(path) / self.FOLDER.format(self.timestamp)
@@ -69,9 +70,11 @@ def flush(self):
6970
if self.record_numpy:
7071
np.savez(self.path / self.FILE.format(self.episode_count), **self.data)
7172
if self.camera_set is not None and self.camera_set.recording_ongoing():
73+
print("stopping video recording")
7274
self.camera_set.stop_video()
7375

7476
if self.gif:
77+
print("saving gifs")
7578
# for key in ["side", "wrist", "bird_eye", "openvla_view"]:
7679
for key in ["side", "right_side", "bird_eye", "left_side", "front"]:
7780
if f"observation.frames.{key}.rgb" in self.data:
@@ -84,6 +87,7 @@ def flush(self):
8487
continue
8588
previous_timestamp = self.data["timestamp"][idx]
8689
imgs.append(Image.fromarray(img))
90+
print(self.path / self.GIF.format(self.timestamp, self.episode_count, key))
8791
imgs[0].save(
8892
self.path / self.GIF.format(self.timestamp, self.episode_count, key),
8993
save_all=True,
@@ -100,7 +104,8 @@ def step(self, action: dict) -> tuple[Any, SupportsFloat, bool, bool, dict[str,
100104
obs, reward, terminated, truncated, info = super().step(action)
101105
# write obs and action into data
102106
act_obs = {"action": action, "observation": obs}
103-
flattened_act_obs = flatten(copy.deepcopy(act_obs), reducer="dot")
107+
# act_obs = {"action": action}
108+
flattened_act_obs = act_obs #flatten(act_obs, reducer="dot")
104109
# add timestamp
105110
flattened_act_obs["timestamp"] = datetime.now().timestamp()
106111
self.data["language_instruction"] = self.language_instruction
@@ -116,8 +121,10 @@ def reset(self, *, seed: int | None = None, options: dict[str, Any] | None = Non
116121
self.flush()
117122
self.step_count = 0
118123
re = super().reset(seed=seed, options=options)
119-
if self.camera_set is not None:
120-
self.camera_set.record_video(self.path, self.episode_count)
124+
# if self.camera_set is not None:
125+
# print("starting video recording")
126+
# self.camera_set.clear_buffer()
127+
# self.camera_set.record_video(self.path / "videos", self.episode_count)
121128
return re
122129

123130
def close(self):

0 commit comments

Comments
 (0)