Ai
1 Star 0 Fork 1

王晓鹏/cv_yolov8

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
3_check_images.py 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
王晓鹏 提交于 2024-07-04 16:19 +08:00 . update
import os
from PIL import Image
import xml.etree.ElementTree as ET
import re
import shutil
ANNOTATIONS_PATH = "/Annotations"
IMAGE_PATH = "/images"
LABEL_PATH = "/labels"
def remove_xml_image_by_name(data_dir, filename):
xml_directory = data_dir + ANNOTATIONS_PATH
images_directory = data_dir + IMAGE_PATH
# 删除对应的xml和image
xml_file_path = os.path.join(xml_directory, f'{filename}.xml')
if os.path.exists(xml_file_path): # 检查对应的XML文件是否存在
print(f'正在删除对应的Annotations文件: {xml_file_path}')
os.remove(xml_file_path) # 删除对应的XML文件
else:
print(f"警告:对应的图像文件 {xml_file_path} 不存在,无法删除")
image_file_path = os.path.join(images_directory, f'{filename}.jpg')
if os.path.exists(image_file_path): # 检查对应的XML文件是否存在
print(f'正在删除对应的Image文件: {image_file_path}')
os.remove(image_file_path) # 删除对应的XML文件
else:
print(f"警告:对应的图像文件 {image_file_path} 不存在,无法删除")
def check_images(data_dir):
print(f"\n------------检查数据集{data_dir}目录下的所有图片文件是否正确------------")
images_directory = data_dir + IMAGE_PATH
for root, dirs, files in os.walk(images_directory):
for file in files:
if file.endswith('.jpg') or file.endswith('.jpeg'):
file_path = os.path.join(root, file)
try:
img = Image.open(file_path) # Open the image file
img.verify() # Verify that it's a valid image
except (IOError, SyntaxError) as e:
print(f'发现损坏的图片: {file_path}')
remove_xml_image_by_name(data_dir, os.path.splitext(file)[0])
if __name__ == '__main__':
current_path = os.getcwd()
# 检查图片文件
check_images(current_path)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chris118/cv_yolov8.git
git@gitee.com:chris118/cv_yolov8.git
chris118
cv_yolov8
cv_yolov8
master

搜索帮助