File size: 5,895 Bytes
df0005c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 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)
# 1) 从 ORIGINAL_IMAGES_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])}')
# 2) 建立 big_images_path 的 basename 索引
big_idx = index_images_by_basename(big_images_path)
print(f'[INFO] big_images_path 中索引的文件名数:{len(big_idx)}')
# 3) 可选:建立标注索引
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
# 统计可能的扩展名对应关系(相同基名不同扩展)
# 对 ORIGINAL 中的每个基名,尝试在 big_images_path 中匹配任意同名基名文件(不论扩展)
for base_no_ext in names_no_ext:
# 找 big_images_path 中所有与该基名匹配的文件(不同扩展)
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
# 目标路径:保留 ANNOTATIONS_PATH 下的相对结构(如果可解析),否则直接平铺到输出根或使用图像相对路径
# 尝试用第一个候选
ann_src = ann_candidates[0]
# 如果候选在 ANNOTATIONS_PATH 下,使用相对路径保存
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 # 确认 Pillow 已安装
except ImportError:
print('请先安装 Pillow: pip install pillow')
sys.exit(1)
main()
|