Download androidcontrol_data_extract.py from GD-ML/AndroidCode: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/datasets/GD-ML/AndroidCode/resolve/13a39215794449c693e0c275fcf2bccccb1f2d87/androidcontrol_data_extract.py
- Command line
-
hf download hf://datasets/GD-ML/AndroidCode@13a39215794449c693e0c275fcf2bccccb1f2d87/androidcontrol_data_extract.py
-
curl -L -o androidcontrol_data_extract.py https://huggingface.co/datasets/GD-ML/AndroidCode/resolve/13a39215794449c693e0c275fcf2bccccb1f2d87/androidcontrol_data_extract.py
3.67 kB
| import tensorflow as tf | |
| import os | |
| import glob | |
| from tqdm import tqdm | |
| # ================= 配置区域 ================= | |
| INPUT_DIR = "./downloads" # 下载数据的目录 | |
| OUTPUT_DIR = "./images" # 结果保存目录 | |
| # 设置为 None 表示提取所有数据 | |
| # 设置为 具体数字(如 100)用于测试 | |
| MAX_EPISODES = None | |
| # =========================================== | |
| # 定义解析格式 | |
| feature_description = { | |
| 'episode_id': tf.io.FixedLenFeature([], tf.int64), | |
| 'screenshots': tf.io.VarLenFeature(tf.string), | |
| } | |
| def parse_func(example_proto): | |
| return tf.io.parse_single_example(example_proto, feature_description) | |
| def main(): | |
| # 屏蔽掉那些烦人的 TensorFlow 日志(只显示 Error) | |
| os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' | |
| try: | |
| tf.config.set_visible_devices([], 'GPU') | |
| except: | |
| pass | |
| # 1. 扫描文件 | |
| tfrecord_files = sorted(glob.glob(os.path.join(INPUT_DIR, "android_control-*-of-00020"))) | |
| if not tfrecord_files: | |
| print("❌ 未找到数据文件。") | |
| return | |
| print(f"📂 找到 {len(tfrecord_files)} 个 TFRecord 文件。") | |
| print(f"📂 图片将保存至: {OUTPUT_DIR}") | |
| # 统计变量 | |
| total_images_extracted = 0 | |
| global_episode_count = 0 | |
| # 初始化进度条 | |
| # 如果 MAX_EPISODES 是 None,进度条只显示计数,不显示百分比(因为不知道总数) | |
| pbar = tqdm(total=MAX_EPISODES, unit="ep", desc="提取进度") | |
| # 2. 遍历所有文件 | |
| for file_path in tfrecord_files: | |
| if MAX_EPISODES is not None and global_episode_count >= MAX_EPISODES: | |
| break | |
| # 加载数据集 | |
| raw_dataset = tf.data.TFRecordDataset(file_path, compression_type='GZIP') | |
| parsed_dataset = raw_dataset.map(parse_func) | |
| # 3. 遍历每一个 Episode | |
| for features in parsed_dataset: | |
| if MAX_EPISODES is not None and global_episode_count >= MAX_EPISODES: | |
| break | |
| ep_id = features['episode_id'].numpy() | |
| # 创建文件夹 | |
| ep_dir = os.path.join(OUTPUT_DIR, str(ep_id)) | |
| # 断点续传检查 | |
| if os.path.exists(ep_dir): | |
| # 如果跳过,是否要算在进度条里?建议算上,以免看起来像卡住了 | |
| # 但如果不读取文件,我们不知道这个跳过的 Episode 里有几张图 | |
| # 所以这里简单更新一下 episode 计数 | |
| # pbar.update(1) | |
| continue | |
| os.makedirs(ep_dir, exist_ok=True) | |
| # 获取图片数据 | |
| screenshots = features['screenshots'].values.numpy() | |
| # 4. 保存图片 | |
| for i, img_bytes in enumerate(screenshots): | |
| img_name = f"{ep_id}_{i}.png" | |
| save_path = os.path.join(ep_dir, img_name) | |
| with open(save_path, "wb") as f: | |
| f.write(img_bytes) | |
| # --- 更新统计数据 --- | |
| current_imgs_count = len(screenshots) | |
| total_images_extracted += current_imgs_count | |
| global_episode_count += 1 | |
| # 更新进度条 | |
| pbar.update(1) | |
| # 在进度条后面实时显示总图片数 | |
| pbar.set_postfix({"Total Images": total_images_extracted}) | |
| pbar.close() | |
| print(f"\n✅ 任务结束!") | |
| print(f" 共处理 Episodes: {global_episode_count}") | |
| print(f" 共提取 Images: {total_images_extracted}") | |
| if __name__ == "__main__": | |
| main() |