当前位置: 首页> 文旅> 美景 > 外贸论坛福步_网上申报流程_网店如何推广自己的产品_中国刚刚发生8件大事

外贸论坛福步_网上申报流程_网店如何推广自己的产品_中国刚刚发生8件大事

时间:2025/8/6 6:57:09来源:https://blog.csdn.net/weixin_49824703/article/details/146361027 浏览次数:0次
外贸论坛福步_网上申报流程_网店如何推广自己的产品_中国刚刚发生8件大事

清洗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及文件路径!!!

关键字:外贸论坛福步_网上申报流程_网店如何推广自己的产品_中国刚刚发生8件大事

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: