Download calculate_camera.py from syCen/action-worldmodel-bench: direct link, hf CLI and curl.
- Browser
- Download file 6.53 kB
-
https://huggingface.co/datasets/syCen/action-worldmodel-bench/resolve/main/calculate_camera.py
- Command line
-
hf download hf://datasets/syCen/action-worldmodel-bench/calculate_camera.py
-
curl -L -o calculate_camera.py https://huggingface.co/datasets/syCen/action-worldmodel-bench/resolve/main/calculate_camera.py
6.53 kB
| import argparse | |
| import json | |
| import os | |
| import pickle | |
| from collections import Counter, defaultdict | |
| from concurrent.futures import ProcessPoolExecutor, as_completed | |
| from pathlib import Path | |
| from tqdm import tqdm | |
| def inspect_cameras(image_data): | |
| """ | |
| Record the frame count of every camera entry. | |
| A camera is valid only when: | |
| frame_count > 0 | |
| """ | |
| camera_frame_counts = {} | |
| if not isinstance(image_data, dict): | |
| return camera_frame_counts, [] | |
| for camera_name, camera_data in image_data.items(): | |
| if not isinstance(camera_data, dict): | |
| continue | |
| if "image" not in camera_data: | |
| continue | |
| frames = camera_data["image"] | |
| if frames is None: | |
| frame_count = 0 | |
| else: | |
| try: | |
| frame_count = len(frames) | |
| except TypeError: | |
| frame_count = 0 | |
| camera_frame_counts[str(camera_name)] = int(frame_count) | |
| valid_cameras = sorted( | |
| camera_name | |
| for camera_name, frame_count in camera_frame_counts.items() | |
| if frame_count > 0 | |
| ) | |
| return camera_frame_counts, valid_cameras | |
| def process_one_image_pkl(image_pkl_path_str, source_root_str): | |
| """ | |
| Worker function executed in a separate process. | |
| Only returns camera statistics. The loaded image data remains inside | |
| the worker and is released after this function finishes. | |
| """ | |
| image_pkl_path = Path(image_pkl_path_str) | |
| source_root = Path(source_root_str) | |
| episode_dir = image_pkl_path.parent | |
| try: | |
| relative_folder = str(episode_dir.relative_to(source_root)) | |
| with open(image_pkl_path, "rb") as f: | |
| image_data = pickle.load(f) | |
| camera_frame_counts, valid_cameras = inspect_cameras(image_data) | |
| empty_cameras = sorted( | |
| camera_name | |
| for camera_name, frame_count in camera_frame_counts.items() | |
| if frame_count == 0 | |
| ) | |
| return { | |
| "success": True, | |
| "folder": relative_folder, | |
| "num_cameras_in_pkl": len(camera_frame_counts), | |
| "num_valid_cameras": len(valid_cameras), | |
| "camera_frame_counts": camera_frame_counts, | |
| "valid_cameras": valid_cameras, | |
| "empty_cameras": empty_cameras, | |
| } | |
| except Exception as e: | |
| try: | |
| relative_folder = str(episode_dir.relative_to(source_root)) | |
| except Exception: | |
| relative_folder = str(episode_dir) | |
| return { | |
| "success": False, | |
| "folder": relative_folder, | |
| "image_pkl": str(image_pkl_path), | |
| "error": repr(e), | |
| } | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description=( | |
| "Count non-empty cameras in every image.pkl using " | |
| "parallel worker processes." | |
| ) | |
| ) | |
| parser.add_argument( | |
| "--source_root", | |
| type=str, | |
| required=True, | |
| help="Root directory containing episode folders.", | |
| ) | |
| parser.add_argument( | |
| "--output", | |
| type=str, | |
| default="camera_count_summary.json", | |
| help="Output JSON path.", | |
| ) | |
| parser.add_argument( | |
| "--workers", | |
| type=int, | |
| default=min(8, os.cpu_count() or 1), | |
| help="Number of worker processes. Start with 8 for large image.pkl files.", | |
| ) | |
| args = parser.parse_args() | |
| source_root = Path(args.source_root).resolve() | |
| output_path = Path(args.output).resolve() | |
| image_pkl_paths = sorted(source_root.rglob("image.pkl")) | |
| print(f"Source root: {source_root}") | |
| print(f"Found {len(image_pkl_paths)} image.pkl files") | |
| print(f"Using {args.workers} worker processes") | |
| valid_camera_count_distribution = Counter() | |
| folders_by_valid_camera_count = defaultdict(list) | |
| episode_details = [] | |
| failed_folders = [] | |
| with ProcessPoolExecutor(max_workers=args.workers) as executor: | |
| futures = [ | |
| executor.submit( | |
| process_one_image_pkl, | |
| str(image_pkl_path), | |
| str(source_root), | |
| ) | |
| for image_pkl_path in image_pkl_paths | |
| ] | |
| for future in tqdm( | |
| as_completed(futures), | |
| total=len(futures), | |
| desc="Inspecting episodes", | |
| ): | |
| result = future.result() | |
| if result["success"]: | |
| result.pop("success") | |
| num_valid_cameras = result["num_valid_cameras"] | |
| folder = result["folder"] | |
| valid_camera_count_distribution[num_valid_cameras] += 1 | |
| folders_by_valid_camera_count[num_valid_cameras].append(folder) | |
| episode_details.append(result) | |
| else: | |
| result.pop("success") | |
| failed_folders.append(result) | |
| # Parallel workers finish in arbitrary order, so sort the results. | |
| episode_details.sort(key=lambda item: item["folder"]) | |
| failed_folders.sort(key=lambda item: item["folder"]) | |
| for folders in folders_by_valid_camera_count.values(): | |
| folders.sort() | |
| output = { | |
| "source_root": str(source_root), | |
| "workers": args.workers, | |
| "total_folders_with_image_pkl": len(image_pkl_paths), | |
| "successfully_processed_folders": len(episode_details), | |
| "failed_folders_count": len(failed_folders), | |
| "valid_camera_count_distribution": { | |
| str(num_cameras): folder_count | |
| for num_cameras, folder_count in sorted( | |
| valid_camera_count_distribution.items() | |
| ) | |
| }, | |
| "folders_by_valid_camera_count": { | |
| str(num_cameras): folders | |
| for num_cameras, folders in sorted( | |
| folders_by_valid_camera_count.items() | |
| ) | |
| }, | |
| "episode_details": episode_details, | |
| "failed_folders": failed_folders, | |
| } | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| json.dump(output, f, indent=2, ensure_ascii=False) | |
| print("\nValid camera count distribution:") | |
| for num_cameras, folder_count in sorted( | |
| valid_camera_count_distribution.items() | |
| ): | |
| print( | |
| f" {num_cameras} non-empty camera(s): " | |
| f"{folder_count} folder(s)" | |
| ) | |
| print(f"\nSuccessfully processed: {len(episode_details)}") | |
| print(f"Failed: {len(failed_folders)}") | |
| print(f"Output saved to: {output_path}") | |
| if __name__ == "__main__": | |
| main() |