File size: 903 Bytes
a03ed61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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