清洗VOC格式數據集代碼示例
import os
import xml.etree.ElementTree as ETdef process_annotations(image_folder, annotation_folder):# 遍歷標簽文件夾中的所有XML文件for xml_file in os.listdir(annotation_folder):if not xml_file.endswith('.xml'):continuexml_path = os.path.join(annotation_folder, xml_file)tree = ET.parse(xml_path)root = tree.getroot()# 標記是否保留該文件keep_file = False# 遍歷所有<object>標簽for obj in root.findall('object'):name = obj.find('name').textif name == 'person': # 需修改,保留哪個類別就寫哪個類別keep_file = Trueelse:root.remove(obj) # 移除非Pedestrian的<object># 如果沒有Pedestrian類別,刪除對應的圖片和標簽if not keep_file:image_name = root.find('filename').textimage_path = os.path.join(image_folder, image_name)if os.path.exists(image_path):os.remove(image_path)os.remove(xml_path)else:# 保存修改后的XML文件tree.write(xml_path)# 示例用法
image_folder = r'D:\BaiduNetdiskDownload\VOCdevkit\VOCdevkit\VOC2007\JPEGImages' # 替換為圖片文件夾路徑
annotation_folder = r'D:\BaiduNetdiskDownload\VOCdevkit\VOCdevkit\VOC2007\Annotations' # 替換為標簽文件夾路徑
process_annotations(image_folder, annotation_folder)
?需根據自己的數據集修改name及文件路徑!!!
清洗YOLO格式數據集代碼示例
import osdef process_labels(image_folder, label_folder):# 遍歷標簽文件夾中的所有標簽文件for label_file in os.listdir(label_folder):if not label_file.endswith('.txt'):continuelabel_path = os.path.join(label_folder, label_file)image_name = os.path.splitext(label_file)[0] + '.png'image_path = os.path.join(image_folder, image_name)# 讀取標簽文件內容with open(label_path, 'r') as f:lines = f.readlines()# 需修改!!!根據自己想要的類別保留!篩選類別為0的行filtered_lines = [line for line in lines if line.strip().split()[0] == '0']# 如果沒有類別為0的行,刪除對應的圖片和標簽if not filtered_lines:if os.path.exists(image_path):os.remove(image_path)os.remove(label_path)else:# 保存修改后的標簽文件with open(label_path, 'w') as f:f.writelines(filtered_lines)# 示例用法
label_folder = r'D:\BaiduNetdiskDownload\annotations_trainval2017\txt' # 替換為圖片文件夾路徑
image_folder = r'D:\BaiduNetdiskDownload\val2017\val2017' # 替換為標簽文件夾路徑
process_labels(image_folder, label_folder)
?需根據自己的數據集修改line及文件路徑!!!