|
|
|
|
| import tensorflow as tf |
| from android_env.proto.a11y import android_accessibility_forest_pb2 |
| import json |
| import os |
| from google.protobuf.json_format import MessageToJson |
| from tqdm import tqdm |
|
|
|
|
| def parse_node(node, nodes, depth=0, parent_bounds=None): |
| |
| class_name = node.get("className", "Unknown") |
| |
| view_id = node.get("viewIdResourceName", "") |
| simplified_view_id = view_id.split(":")[-1] if view_id else "" |
| simplified_view_id = ' '.join(simplified_view_id.split('_')) |
| simplified_view_id = simplified_view_id.replace('id/','') |
|
|
| bounds = node.get("boundsInScreen", {}) |
| bounds_str = f"[{bounds.get('left', 0)}, {bounds.get('top', 0)}, {bounds.get('right', 0)}, {bounds.get('bottom', 0)}]" |
|
|
| if parent_bounds and bounds == parent_bounds: |
| bounds_str = "" |
|
|
| node_str = f"{' ' * depth}└── {simplified_view_id} {f'({bounds_str})' if bounds_str else ''}" |
|
|
| children_str = "" |
| if "childIds" in node: |
| children = [child for child in nodes if 'uniqueId' in child and child['uniqueId'] in node['childIds']] |
| for child in children: |
| children_str += "\n" + parse_node(child, nodes, depth + 1, bounds) |
|
|
| return node_str + children_str |
|
|
|
|
| def parse_tree(window_data): |
| |
| window_info = window_data['windows'][0] |
| window_type = window_info.get('windowType', 'Unknown') |
| root_node_str = f"Window ({window_type})" |
| nodes = window_info['tree']['nodes'] |
| root_node_str += "\n" + parse_node(nodes[0], nodes, 1) |
| return root_node_str |
|
|
|
|
|
|
| |
| output_dir = "output_data" |
| os.makedirs(output_dir, exist_ok=True) |
|
|
| intermediate_json_path = os.path.join("/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/wangbaode/NIPS_2025/Agent/Dataset/android_env/intermediate_data_8k_succ.json") |
| intermediate_json_path_new = os.path.join("/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/wangbaode/NIPS_2025/Agent/Dataset/android_env/intermediate_data.jsonl") |
|
|
|
|
|
|
| |
| processed_episodes = set() |
| all_data = [] |
|
|
| if os.path.exists(intermediate_json_path): |
| with open(intermediate_json_path, "r", encoding="utf-8") as f: |
| try: |
| all_data = json.load(f) |
| for record in all_data: |
| processed_episodes.add(record["episode_id"]) |
| print(f"恢复 {len(processed_episodes)} 条已处理记录") |
| except Exception as e: |
| print(f"读取中间文件失败: {e}") |
|
|
| |
| filenames = tf.io.gfile.glob('/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/wangbaode/NIPS_2025/Agent/Dataset/AndControl/android_control*') |
| raw_dataset = tf.data.TFRecordDataset(filenames, compression_type='GZIP') |
|
|
| |
| record_index = len(all_data) |
|
|
| for raw_record in tqdm(raw_dataset, desc="遍历 TFRecords"): |
| try: |
| example = tf.train.Example.FromString(raw_record.numpy()) |
|
|
| episode_id = example.features.feature['episode_id'].int64_list.value[0] |
| if episode_id in processed_episodes: |
| continue |
|
|
| goal = example.features.feature['goal'].bytes_list.value[0].decode('utf-8') |
| screenshots = example.features.feature['screenshots'].bytes_list.value |
| accessibility_trees = [ |
| android_accessibility_forest_pb2.AndroidAccessibilityForest().FromString(tree) |
| for tree in example.features.feature['accessibility_trees'].bytes_list.value |
| ] |
| screenshot_widths = list(example.features.feature['screenshot_widths'].int64_list.value) |
| screenshot_heights = list(example.features.feature['screenshot_heights'].int64_list.value) |
| actions = [a.decode('utf-8') for a in example.features.feature['actions'].bytes_list.value] |
| step_instructions = [s.decode('utf-8') for s in example.features.feature['step_instructions'].bytes_list.value] |
|
|
| |
| data = { |
| "episode_id": episode_id, |
| "goal": goal, |
| "screenshots": [], |
| "screenshot_widths": screenshot_widths, |
| "screenshot_heights": screenshot_heights, |
| "actions": actions, |
| "step_instructions": step_instructions, |
| "accessibility_trees": [] |
| } |
|
|
| |
| for i, img in enumerate(screenshots): |
| path = os.path.join(output_dir, f"screenshot_{episode_id}_{i}.png") |
| with open(path, "wb") as f: |
| f.write(img) |
| data["screenshots"].append(path) |
|
|
| |
| for tree in accessibility_trees: |
| json_dict = json.loads(MessageToJson(tree)) |
| tree_output = parse_tree(json_dict) |
| data["accessibility_trees"].append(tree_output) |
|
|
| |
| all_data.append(data) |
| processed_episodes.add(episode_id) |
|
|
| |
| |
| |
|
|
| |
| with open(intermediate_json_path_new, "a+", encoding="utf-8") as fout: |
| fout.write(json.dumps(data, ensure_ascii=False) + "\n") |
|
|
| print(f"✅ 已处理 episode_id={episode_id}") |
|
|
| except Exception as e: |
| print(f"❌ 处理失败,跳过该记录:{e}") |
| continue |
|
|
|
|