KillzoneCC commited on
Commit ·
498c927
1
Parent(s): 6230dba
213123
Browse files- collect_and_upload.py +8 -39
collect_and_upload.py
CHANGED
|
@@ -29,10 +29,9 @@ ROSBRIDGE_PORT = 9090
|
|
| 29 |
REPO_ID = "KillzoneCCO/Ainex"
|
| 30 |
LOCAL_ROOT_DIR = Path("./")
|
| 31 |
|
| 32 |
-
# --- ROS Топики и типы сообщений для сбора данных
|
| 33 |
OBSERVATION_TOPICS = {
|
| 34 |
"camera_generic": {"topic": "/camera/image_raw", "msg_type": "sensor_msgs/Image"},
|
| 35 |
-
"imu": {"topic": "/imu", "msg_type": "sensor_msgs/Imu"},
|
| 36 |
"joint_positions": {"topic": "/ros_robot_controller/bus_servo/set_position", "msg_type": "ros_robot_controller/SetBusServosPosition"},
|
| 37 |
}
|
| 38 |
|
|
@@ -96,19 +95,6 @@ def create_image_callback(camera_key):
|
|
| 96 |
latest_data[camera_key] = None
|
| 97 |
return callback
|
| 98 |
|
| 99 |
-
def imu_callback(message):
|
| 100 |
-
with data_lock:
|
| 101 |
-
try:
|
| 102 |
-
imu_data = np.array([
|
| 103 |
-
message['orientation']['x'], message['orientation']['y'], message['orientation']['z'], message['orientation']['w'],
|
| 104 |
-
message['angular_velocity']['x'], message['angular_velocity']['y'], message['angular_velocity']['z'],
|
| 105 |
-
message['linear_acceleration']['x'], message['linear_acceleration']['y'], message['linear_acceleration']['z']
|
| 106 |
-
], dtype=np.float32)
|
| 107 |
-
latest_data["imu"] = (imu_data, time.time())
|
| 108 |
-
except Exception as e:
|
| 109 |
-
print(f"DEBUG: Ошибка обработки IMU: {e}")
|
| 110 |
-
latest_data["imu"] = None
|
| 111 |
-
|
| 112 |
def joint_positions_callback(message):
|
| 113 |
with data_lock:
|
| 114 |
try:
|
|
@@ -189,7 +175,7 @@ def create_video(frames, video_path):
|
|
| 189 |
def collect_data_and_save():
|
| 190 |
global running
|
| 191 |
|
| 192 |
-
# ---
|
| 193 |
META_DIR = LOCAL_ROOT_DIR / "meta"
|
| 194 |
DATA_DIR = LOCAL_ROOT_DIR / "data" / "chunk-000"
|
| 195 |
VIDEOS_DIR = LOCAL_ROOT_DIR / "videos" / "observation.images.generic"
|
|
@@ -199,7 +185,6 @@ def collect_data_and_save():
|
|
| 199 |
os.makedirs(VIDEOS_DIR, exist_ok=True)
|
| 200 |
|
| 201 |
info_path = META_DIR / "info.json"
|
| 202 |
-
episodes_jsonl_path = META_DIR / "episodes.jsonl"
|
| 203 |
|
| 204 |
episode_idx = 0
|
| 205 |
|
|
@@ -211,9 +196,9 @@ def collect_data_and_save():
|
|
| 211 |
]
|
| 212 |
create_tasks_file(META_DIR, tasks_list)
|
| 213 |
|
| 214 |
-
# ---
|
| 215 |
info = {
|
| 216 |
-
"codebase_version": "v1.
|
| 217 |
"total_frames": 0,
|
| 218 |
"total_episodes": 0,
|
| 219 |
"episode_lengths": [],
|
|
@@ -225,7 +210,6 @@ def collect_data_and_save():
|
|
| 225 |
"action": { "dtype": "float32", "shape": [26] },
|
| 226 |
"observation": {
|
| 227 |
"joint_positions": { "dtype": "float32", "shape": [26] },
|
| 228 |
-
"imu": { "dtype": "float32", "shape": [10] },
|
| 229 |
"images.generic": {
|
| 230 |
"dtype": "video",
|
| 231 |
"media_type": "video/mp4",
|
|
@@ -271,19 +255,17 @@ features:
|
|
| 271 |
|
| 272 |
print("Установка подписок...")
|
| 273 |
ws_manager.subscribe(OBSERVATION_TOPICS["camera_generic"]["topic"], OBSERVATION_TOPICS["camera_generic"]["msg_type"], create_image_callback("camera_generic"))
|
| 274 |
-
ws_manager.subscribe(OBSERVATION_TOPICS["imu"]["topic"], OBSERVATION_TOPICS["imu"]["msg_type"], imu_callback)
|
| 275 |
ws_manager.subscribe(OBSERVATION_TOPICS["joint_positions"]["topic"], OBSERVATION_TOPICS["joint_positions"]["msg_type"], joint_positions_callback)
|
| 276 |
print("Подписки установлены.")
|
| 277 |
|
| 278 |
print("\nСбор данных запущен. Выполняйте движения роботом.")
|
| 279 |
print("Нажмите 'Ctrl+C' для завершения и сохранения датасета.")
|
| 280 |
|
| 281 |
-
episode_duration_seconds = 5.0
|
| 282 |
current_episode_start_time = time.time()
|
| 283 |
|
| 284 |
all_images_generic = []
|
| 285 |
all_joint_positions = []
|
| 286 |
-
all_imu_data = []
|
| 287 |
all_timestamps = []
|
| 288 |
all_actions = []
|
| 289 |
|
|
@@ -291,30 +273,22 @@ features:
|
|
| 291 |
|
| 292 |
try:
|
| 293 |
while running:
|
| 294 |
-
# --- ИСПРАВЛЕНО: Асинхронная обработка данных ---
|
| 295 |
with data_lock:
|
| 296 |
new_image_data_with_timestamp = latest_data["camera_generic"]
|
| 297 |
if new_image_data_with_timestamp is not None:
|
| 298 |
-
# Сбрасываем latest_data["camera_generic"] сразу, чтобы избежать повторной обработки
|
| 299 |
latest_data["camera_generic"] = None
|
| 300 |
|
| 301 |
if new_image_data_with_timestamp is not None:
|
| 302 |
img_generic, img_timestamp = new_image_data_with_timestamp
|
| 303 |
|
| 304 |
-
# Берём последние доступные данные со всех топиков
|
| 305 |
with data_lock:
|
| 306 |
-
imu_data = latest_data["imu"][0] if latest_data["imu"] is not None else np.array([0.0]*10, dtype=np.float32)
|
| 307 |
joint_positions_data = latest_data["joint_positions"][0] if latest_data["joint_positions"] is not None else np.array([0.0]*26, dtype=np.float32)
|
| 308 |
|
| 309 |
all_images_generic.append(img_generic)
|
| 310 |
all_joint_positions.append(joint_positions_data.tolist())
|
| 311 |
-
|
| 312 |
-
# ИСПРАВЛЕНО: action теперь содержит актуальные команды сервоприводов
|
| 313 |
-
all_actions.append(joint_positions_data.tolist())
|
| 314 |
-
# ИСПРАВЛЕНО: Относительные временные метки
|
| 315 |
all_timestamps.append(img_timestamp - episode_start_timestamp)
|
| 316 |
|
| 317 |
-
# --- Логика сохранения эпизода ---
|
| 318 |
if (time.time() - current_episode_start_time) >= episode_duration_seconds:
|
| 319 |
if len(all_images_generic) > 0:
|
| 320 |
print(f"Собрано {len(all_images_generic)} шагов для эпизода {episode_idx:06d}. Сохраняю...")
|
|
@@ -323,12 +297,9 @@ features:
|
|
| 323 |
create_video(all_images_generic, video_path_generic)
|
| 324 |
|
| 325 |
df = pd.DataFrame({
|
| 326 |
-
|
| 327 |
-
'observation.images.generic': [f"videos/observation.images.generic/episode_{episode_idx:06d}.mp4"] * len(all_images_generic),
|
| 328 |
'observation.joint_positions': all_joint_positions,
|
| 329 |
-
'
|
| 330 |
-
# ИСПРАВЛЕНО: action.servo_commands -> action
|
| 331 |
-
'action': all_actions,
|
| 332 |
'timestamp': all_timestamps,
|
| 333 |
'frame_index': list(range(len(all_images_generic))),
|
| 334 |
'episode_index': [episode_idx] * len(all_images_generic),
|
|
@@ -356,7 +327,6 @@ features:
|
|
| 356 |
episode_start_timestamp = time.time()
|
| 357 |
all_images_generic = []
|
| 358 |
all_joint_positions = []
|
| 359 |
-
all_imu_data = []
|
| 360 |
all_timestamps = []
|
| 361 |
all_actions = []
|
| 362 |
|
|
@@ -374,7 +344,6 @@ features:
|
|
| 374 |
df = pd.DataFrame({
|
| 375 |
'observation.images.generic': [f"videos/observation.images.generic/episode_{episode_idx:06d}.mp4"] * len(all_images_generic),
|
| 376 |
'observation.joint_positions': all_joint_positions,
|
| 377 |
-
'observation.imu': all_imu_data,
|
| 378 |
'action': all_actions,
|
| 379 |
'timestamp': all_timestamps,
|
| 380 |
'frame_index': list(range(len(all_images_generic))),
|
|
|
|
| 29 |
REPO_ID = "KillzoneCCO/Ainex"
|
| 30 |
LOCAL_ROOT_DIR = Path("./")
|
| 31 |
|
| 32 |
+
# --- ROS Топики и типы сообщений для сбора данных ---
|
| 33 |
OBSERVATION_TOPICS = {
|
| 34 |
"camera_generic": {"topic": "/camera/image_raw", "msg_type": "sensor_msgs/Image"},
|
|
|
|
| 35 |
"joint_positions": {"topic": "/ros_robot_controller/bus_servo/set_position", "msg_type": "ros_robot_controller/SetBusServosPosition"},
|
| 36 |
}
|
| 37 |
|
|
|
|
| 95 |
latest_data[camera_key] = None
|
| 96 |
return callback
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def joint_positions_callback(message):
|
| 99 |
with data_lock:
|
| 100 |
try:
|
|
|
|
| 175 |
def collect_data_and_save():
|
| 176 |
global running
|
| 177 |
|
| 178 |
+
# --- Создание папок с правильной структурой ---
|
| 179 |
META_DIR = LOCAL_ROOT_DIR / "meta"
|
| 180 |
DATA_DIR = LOCAL_ROOT_DIR / "data" / "chunk-000"
|
| 181 |
VIDEOS_DIR = LOCAL_ROOT_DIR / "videos" / "observation.images.generic"
|
|
|
|
| 185 |
os.makedirs(VIDEOS_DIR, exist_ok=True)
|
| 186 |
|
| 187 |
info_path = META_DIR / "info.json"
|
|
|
|
| 188 |
|
| 189 |
episode_idx = 0
|
| 190 |
|
|
|
|
| 196 |
]
|
| 197 |
create_tasks_file(META_DIR, tasks_list)
|
| 198 |
|
| 199 |
+
# --- Создание метаданных для датасета ---
|
| 200 |
info = {
|
| 201 |
+
"codebase_version": "v1.3",
|
| 202 |
"total_frames": 0,
|
| 203 |
"total_episodes": 0,
|
| 204 |
"episode_lengths": [],
|
|
|
|
| 210 |
"action": { "dtype": "float32", "shape": [26] },
|
| 211 |
"observation": {
|
| 212 |
"joint_positions": { "dtype": "float32", "shape": [26] },
|
|
|
|
| 213 |
"images.generic": {
|
| 214 |
"dtype": "video",
|
| 215 |
"media_type": "video/mp4",
|
|
|
|
| 255 |
|
| 256 |
print("Установка подписок...")
|
| 257 |
ws_manager.subscribe(OBSERVATION_TOPICS["camera_generic"]["topic"], OBSERVATION_TOPICS["camera_generic"]["msg_type"], create_image_callback("camera_generic"))
|
|
|
|
| 258 |
ws_manager.subscribe(OBSERVATION_TOPICS["joint_positions"]["topic"], OBSERVATION_TOPICS["joint_positions"]["msg_type"], joint_positions_callback)
|
| 259 |
print("Подписки установлены.")
|
| 260 |
|
| 261 |
print("\nСбор данных запущен. Выполняйте движения роботом.")
|
| 262 |
print("Нажмите 'Ctrl+C' для завершения и сохранения датасета.")
|
| 263 |
|
| 264 |
+
episode_duration_seconds = 5.0
|
| 265 |
current_episode_start_time = time.time()
|
| 266 |
|
| 267 |
all_images_generic = []
|
| 268 |
all_joint_positions = []
|
|
|
|
| 269 |
all_timestamps = []
|
| 270 |
all_actions = []
|
| 271 |
|
|
|
|
| 273 |
|
| 274 |
try:
|
| 275 |
while running:
|
|
|
|
| 276 |
with data_lock:
|
| 277 |
new_image_data_with_timestamp = latest_data["camera_generic"]
|
| 278 |
if new_image_data_with_timestamp is not None:
|
|
|
|
| 279 |
latest_data["camera_generic"] = None
|
| 280 |
|
| 281 |
if new_image_data_with_timestamp is not None:
|
| 282 |
img_generic, img_timestamp = new_image_data_with_timestamp
|
| 283 |
|
|
|
|
| 284 |
with data_lock:
|
|
|
|
| 285 |
joint_positions_data = latest_data["joint_positions"][0] if latest_data["joint_positions"] is not None else np.array([0.0]*26, dtype=np.float32)
|
| 286 |
|
| 287 |
all_images_generic.append(img_generic)
|
| 288 |
all_joint_positions.append(joint_positions_data.tolist())
|
| 289 |
+
all_actions.append(joint_positions_data.tolist())
|
|
|
|
|
|
|
|
|
|
| 290 |
all_timestamps.append(img_timestamp - episode_start_timestamp)
|
| 291 |
|
|
|
|
| 292 |
if (time.time() - current_episode_start_time) >= episode_duration_seconds:
|
| 293 |
if len(all_images_generic) > 0:
|
| 294 |
print(f"Собрано {len(all_images_generic)} шагов для эпизода {episode_idx:06d}. Сохраняю...")
|
|
|
|
| 297 |
create_video(all_images_generic, video_path_generic)
|
| 298 |
|
| 299 |
df = pd.DataFrame({
|
| 300 |
+
'observation.images.generic': [f"videos/observation.images.generic/episode_{episode_idx:06d}.mp4"] * len(all_images_generic),
|
|
|
|
| 301 |
'observation.joint_positions': all_joint_positions,
|
| 302 |
+
'action': all_actions,
|
|
|
|
|
|
|
| 303 |
'timestamp': all_timestamps,
|
| 304 |
'frame_index': list(range(len(all_images_generic))),
|
| 305 |
'episode_index': [episode_idx] * len(all_images_generic),
|
|
|
|
| 327 |
episode_start_timestamp = time.time()
|
| 328 |
all_images_generic = []
|
| 329 |
all_joint_positions = []
|
|
|
|
| 330 |
all_timestamps = []
|
| 331 |
all_actions = []
|
| 332 |
|
|
|
|
| 344 |
df = pd.DataFrame({
|
| 345 |
'observation.images.generic': [f"videos/observation.images.generic/episode_{episode_idx:06d}.mp4"] * len(all_images_generic),
|
| 346 |
'observation.joint_positions': all_joint_positions,
|
|
|
|
| 347 |
'action': all_actions,
|
| 348 |
'timestamp': all_timestamps,
|
| 349 |
'frame_index': list(range(len(all_images_generic))),
|