File size: 3,306 Bytes
37dc778 |
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 |
import os
import os.path as osp
from PIL import Image
import json
from shutil import copyfile
def check_txt_files(directory):
corrupted_files = []
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.lower().endswith('.txt'):
file_path = os.path.join(dirpath, filename)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 可以添加内容验证逻辑,比如检查是否有乱码
except UnicodeDecodeError:
print(f"编码错误或损坏:{file_path}")
corrupted_files.append(file_path)
except Exception as e:
print(f"读取异常:{file_path},错误:{e}")
corrupted_files.append(file_path)
return corrupted_files
def check_images_files(root_directory):
corrupted_files = []
for dirpath, dirnames, filenames in os.walk(root_directory):
if 'cubemap' in dirpath:
continue
for filename in filenames:
file_path = os.path.join(dirpath, filename)
if file_path.endswith('.png'):
try:
with Image.open(file_path) as img:
img.verify() # 验证图片完整性
except (IOError, SyntaxError) as e:
print(f"损坏的图片:{file_path}")
corrupted_files.append(file_path)
return corrupted_files
if __name__ == '__main__':
CHECK_IMAGE_FILES = False
CHECK_TEXT_FILES = False
CHECK_IMAGE_FILES_SCENE_BY_SCENE = False
REMOVE_CORRUPTED_FILES = True
# 替换为你的根目录路径
root = '/mnt/xuyuanxiong/projects/DreamCube-Flux-Private/data/Structured3D/code'
data_dir = osp.join(root, 'Structured3D')
temp_dir = osp.join(root, 'TEMP')
json_file = osp.join(root, 'corrupted_images.json')
if CHECK_IMAGE_FILES:
corrupted_files = check_images_files(data_dir)
print("损坏的图片文件有:")
for file in corrupted_files:
print(file)
if CHECK_TEXT_FILES:
corrupted_txt_files = check_txt_files(data_dir)
print("损坏的文本文件有:")
for file in corrupted_txt_files:
print(file)
if CHECK_IMAGE_FILES_SCENE_BY_SCENE:
for i in range(3470, 3479 + 1):
scene_dir = osp.join(data_dir, f'scene_{i:05d}/2D_rendering/')
os.system(f'cd {scene_dir}; ls -lR | grep "^-" | wc -l')
corrupted_files = check_images_files(scene_dir)
print("损坏的图片文件有:")
for file in corrupted_files:
print(file)
if REMOVE_CORRUPTED_FILES:
with open(json_file, 'r') as fio:
filelist = json.load(fio)
for i, f in enumerate(filelist):
file_path = osp.join(root, f)
if osp.exists(file_path):
print(file_path)
if 'full' in f:
temp_path = osp.join(temp_dir, f)
os.makedirs(osp.dirname(temp_path), exist_ok=True)
copyfile(file_path, temp_path)
os.remove(file_path)
|