| import glob |
| import json |
| import time |
| from pathlib import Path |
|
|
| import datasets |
| from datasets.table import embed_table_storage |
| from tqdm import tqdm |
|
|
|
|
| def extract_number(s): |
| |
| if s.endswith(".png"): |
| s = s.rsplit('.', 1)[0] |
|
|
| |
| last_underscore_pos = s.rfind("_") |
| if last_underscore_pos == -1: |
| return float("inf") |
|
|
| number_part = s[last_underscore_pos + 1:] |
|
|
| |
| try: |
| return int(number_part) |
| except ValueError: |
| return float("inf") |
|
|
| def sort_strings(strings): |
| |
| return sorted(strings, key=extract_number) |
|
|
|
|
| def embed_images(dataset: datasets.Dataset) -> datasets.Dataset: |
| format = dataset.format |
| dataset = dataset.with_format("arrow") |
| dataset = dataset.map(embed_table_storage, batched=False) |
| dataset = dataset.with_format(**format) |
| return dataset |
|
|
| def read_jsonl(file_path): |
| with open(file_path, 'r') as f: |
| content = f.read() |
|
|
| |
| json_objects = [] |
|
|
| |
| brace_count = 0 |
| start = 0 |
|
|
| |
| for idx, char in enumerate(content): |
| if char == '{': |
| |
| if brace_count == 0: |
| start = idx |
| brace_count += 1 |
| elif char == '}': |
| |
| brace_count -= 1 |
| if brace_count == 0: |
| |
| json_str = content[start:idx+1] |
| try: |
| |
| json_objects.append(json.loads(json_str)) |
| except json.JSONDecodeError as e: |
| print(f"Error parsing JSON: {e}") |
| continue |
| action_dicts = [d for d in json_objects if 'action' in d] |
|
|
| return action_dicts |
|
|
|
|
| features = datasets.Features( |
| { |
| "env_id": datasets.Value("string"), |
| "traj_id": datasets.Value("string"), |
| "image_id": datasets.Value("string"), |
| "frame_index": datasets.Value("int32"), |
| "image": datasets.Image(), |
| "pos": datasets.Sequence(length=3, feature=datasets.Value("float64")), |
| "yaw": datasets.Value("float64"), |
| "action_type": datasets.Value("string"), |
| "action_value": datasets.Value("int32"), |
| } |
| ) |
|
|
|
|
| json_path = '/cpfs01/shared/optimal/Datasets/Openfly/OpenFly/Annotation/unseen.json' |
|
|
| with open(json_path, 'r') as file: |
| data = json.load(file) |
|
|
| for item in tqdm(data, ncols=100, position=0, desc="Progress"): |
| path = '/cpfs01/shared/optimal/Datasets/Openfly/OpenFly/Image/'+str(item['image_path']) + '/' |
| json_path = path + 'pose.jsonl' |
| path += '*.png' |
| print(json_path) |
| |
| png_files = sort_strings(glob.glob(path)) |
| action_list = read_jsonl(json_path) |
| action_list.sort(key=lambda x: int(x['action']['imageid'])) |
| |
| image_path = Path(item['image_path']) |
| env_id_item = image_path.parent |
| traj_id_item = image_path.stem |
| img_id = [Path(item).stem for item in png_files] |
| num = len(png_files) |
| assert num == len(action_list), f"Number of images and actions do not match: {num} != {len(action_list)}" |
| data_dict = { |
| "env_id" : [env_id_item] * num, |
| "traj_id" : [traj_id_item] * num, |
| "image_id" : img_id, |
| "frame_index" : [i for i in range(num)], |
| "image" : ['/cpfs01/shared/optimal/Datasets/Openfly/OpenFly/Image/'+str(image_path) + '/' + img_id[i] + '.png' for i in range(num)], |
| "pos": [action_list[i]['action']['pos'][:3] for i in range(num)], |
| "yaw": [action_list[i]['action']['yaw'] for i in range(num)], |
| "action_type": [action_list[i]['action']['type'] for i in range(num)], |
| "action_value": [action_list[i]['action']['value'] for i in range(num)] |
| } |
| |
|
|
| path = Path("./hf") |
| episode_dict = {key: data_dict[key] for key in features} |
| ep_dataset = datasets.Dataset.from_dict(episode_dict, features=features, split="train") |
| ep_dataset = embed_images(ep_dataset) |
| ep_data_path = (path / data_dict["env_id"][0] / data_dict["traj_id"][0]).with_suffix(".parquet") |
| ep_data_path.parent.mkdir(parents=True, exist_ok=True) |
| ep_dataset.to_parquet(ep_data_path) |
| |
|
|
| |
| |
|
|
|
|
|
|
|
|
| |
|
|
| |
|
|
| |
| |
|
|
|
|
|
|
|
|