import os import hashlib import datasets root_path = './Datasets/' data = datasets.load_dataset("allenai/pixmo-points", split="train", cache_dir=root_path) len_data = len(data) image_folder = os.path.join(root_path,"pixmo_images") for i,example in enumerate(data): print(i,len_data,f"{(i / len_data) * 100:.3f}%") # break image_hash = example["image_sha256"] image_path = os.path.join(image_folder, f"{image_hash}.jpg") if not os.path.exists(image_path): print('image does not exist, skip') continue # 图片不存在,跳过 with open(image_path, "rb") as img_file: image_bytes = img_file.read() file_hash = hashlib.sha256(image_bytes).hexdigest() print('hash matches') if file_hash != image_hash: os.remove(image_path) # 哈希不匹配,删除图片 print('hash mismatch, delete') # break