| import os |
| import sys |
| import shutil |
| from PIL import Image |
|
|
| big_images_path = "/data/vjuicefs_ai_camera_llm/intern_data/minxing/datasets/UltraLens/200mm" |
| Lens="85mm" |
| ORIGINAL_IMAGES_PATH = f'/data/vjuicefs_ai_camera_llm/intern_data/minxing/datasets/UltraLens_final/{Lens}/val/gt_1024' |
| ANNOTATIONS_PATH = '/data/vjuicefs_ai_camera_llm/intern_data/minxing/datasets/UltraLens/Anno' |
| OUTPUT_JSON_PATH = f'/data/vjuicefs_ai_camera_llm/intern_data/minxing/datasets/UltraLens_final/{Lens}/val/Anno_below1024' |
|
|
| |
| COPY_ANNOTATIONS = True |
|
|
| IMG_EXTS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'} |
| ANN_EXT = '_res.json' |
|
|
| def is_image_file(fname: str) -> bool: |
| return os.path.splitext(fname.lower())[1] in IMG_EXTS |
|
|
| def get_image_size(img_path: str): |
| try: |
| with Image.open(img_path) as im: |
| w, h = im.size |
| return w, h |
| except Exception as e: |
| print(f'[WARN] 打开图像失败: {img_path} -> {e}') |
| return None |
|
|
| def ensure_dir(path: str): |
| os.makedirs(path, exist_ok=True) |
|
|
| def collect_filenames_from_dir(root_dir: str): |
| """ |
| 返回:set [不含扩展名的文件名] 和 dict 映射 [basename(含扩展) -> 相对路径] |
| """ |
| names_no_ext = set() |
| rel_map = {} |
| for root, _, files in os.walk(root_dir): |
| for f in files: |
| if is_image_file(f): |
| rel = os.path.relpath(os.path.join(root, f), root_dir) |
| rel_map[f] = rel |
| names_no_ext.add(os.path.splitext(f)[0]) |
| return names_no_ext, rel_map |
|
|
| def index_images_by_basename(root_dir: str): |
| """ |
| 构建 big_images_path 的索引:basename(含扩展) -> [绝对路径列表] |
| """ |
| idx = {} |
| for root, _, files in os.walk(root_dir): |
| for f in files: |
| if is_image_file(f): |
| idx.setdefault(f, []).append(os.path.join(root, f)) |
| return idx |
|
|
| def build_ann_index(ann_root: str): |
| """ |
| 标注文件名(含扩展) -> [绝对路径列表] |
| """ |
| idx = {} |
| for root, _, files in os.walk(ann_root): |
| for f in files: |
| if f.lower().endswith(ANN_EXT): |
| idx.setdefault(f, []).append(os.path.join(root, f)) |
| return idx |
|
|
| def main(): |
| ensure_dir(OUTPUT_JSON_PATH) |
| |
| names_no_ext, original_rel_map = collect_filenames_from_dir(ORIGINAL_IMAGES_PATH) |
| print(f'[INFO] ORIGINAL 中图像基名数量:{len(names_no_ext)};示例若干:{list(sorted(list(names_no_ext))[:5])}') |
|
|
| |
| big_idx = index_images_by_basename(big_images_path) |
| print(f'[INFO] big_images_path 中索引的文件名数:{len(big_idx)}') |
|
|
| |
| ann_idx = build_ann_index(ANNOTATIONS_PATH) if COPY_ANNOTATIONS else {} |
|
|
| total_to_check = 0 |
| checked = 0 |
| below_1024 = 0 |
| copied_ann = 0 |
| missing_in_big = 0 |
| missing_ann = 0 |
|
|
| |
| |
| for base_no_ext in names_no_ext: |
| |
| candidates = [] |
| for ext in IMG_EXTS: |
| fname = base_no_ext + ext |
| if fname in big_idx: |
| candidates.extend(big_idx[fname]) |
|
|
| if not candidates: |
| missing_in_big += 1 |
| continue |
|
|
| total_to_check += len(candidates) |
| for img_path in candidates: |
| size = get_image_size(img_path) |
| if size is None: |
| continue |
| checked += 1 |
| w, h = size |
| if max(w, h) <= 1024: |
| below_1024 += 1 |
|
|
| if COPY_ANNOTATIONS: |
| ann_name = base_no_ext + ANN_EXT |
| |
| ann_candidates = ann_idx.get(ann_name, []) |
| if not ann_candidates: |
| missing_ann += 1 |
| continue |
| |
| |
| ann_src = ann_candidates[0] |
| |
| if ann_src.startswith(os.path.abspath(ANNOTATIONS_PATH)): |
| rel = os.path.relpath(ann_src, ANNOTATIONS_PATH) |
| ann_dst = os.path.join(OUTPUT_JSON_PATH, rel) |
| else: |
| |
| ann_dst = os.path.join(OUTPUT_JSON_PATH, ann_name) |
|
|
| ensure_dir(os.path.dirname(ann_dst)) |
| try: |
| shutil.copy2(ann_src, ann_dst) |
| copied_ann += 1 |
| except Exception as e: |
| print(f'[ERROR] 复制失败: {ann_src} -> {ann_dst} | {e}') |
|
|
| print('---- 统计 ----') |
| print(f'ORIGINAL 中基名数:{len(names_no_ext)}') |
| print(f'在 big_images_path 中缺失的基名数:{missing_in_big}') |
| print(f'需要检查的同名文件数:{total_to_check}') |
| print(f'成功读取尺寸的文件数:{checked}') |
| print(f'最长边<=1024 的文件数:{below_1024}') |
| if COPY_ANNOTATIONS: |
| print(f'成功复制标注数:{copied_ann}') |
| print(f'缺失标注数:{missing_ann}') |
| print(f'输出标注目录:{OUTPUT_JSON_PATH}') |
|
|
| if __name__ == '__main__': |
| try: |
| from PIL import Image |
| except ImportError: |
| print('请先安装 Pillow: pip install pillow') |
| sys.exit(1) |
| main() |
|
|