当前位置: 首页 > news >正文

YOLO脚本合集

1. 批量移除xml标注中的某一个类别标签

#  批量移除xml标注中的某一个类别标签
import xml.etree.cElementTree as ET
import os

# xml文件路径
xml_path = r'./Annotations'
xml_files = os.listdir(xml_path)
# 需要删除的类别名称
CLASSES = ["person"]

for axml in xml_files:
    path_xml = os.path.join(xml_path, axml)
    tree = ET.parse(path_xml)
    root = tree.getroot()
    for child in root.findall('object'):
        name = child.find('name').text
        if name in CLASSES:
            root.remove(child)
    tree.write(os.path.join(xml_path, axml))



# import xml.etree.cElementTree as ET
# import os
# import shutil
# import logging

# # 配置日志
# logging.basicConfig(filename='xml_process.log', level=logging.INFO,
#                     format='%(asctime)s - %(levelname)s - %(message)s')

# def process_xml(xml_path, classes_to_remove):
#     backup_path = os.path.join(xml_path, '_backup')
#     os.makedirs(backup_path, exist_ok=True)
    
#     for filename in os.listdir(xml_path):
#         if not filename.endswith('.xml'):
#             continue
            
#         src_path = os.path.join(xml_path, filename)
#         dest_path = os.path.join(backup_path, filename)
        
#         try:
#             # 备份原始文件
#             shutil.copy2(src_path, dest_path)
#             logging.info(f"已备份文件: {filename}")
            
#             # 解析XML
#             tree = ET.parse(src_path, parser=ET.XMLParser(encoding='utf-8'))
#             root = tree.getroot()
            
#             # 查找并删除目标节点
#             removed_count = 0
#             for obj in root.findall('.//{*}object'):  # 处理命名空间
#                 name_elem = obj.find('.//{*}name')
#                 if name_elem is not None and name_elem.text.lower() in [c.lower() for c in classes_to_remove]:
#                     root.remove(obj)
#                     removed_count += 1
            
#             # 保存修改
#             tree.write(src_path, encoding='utf-8', xml_declaration=True)
#             logging.info(f"已处理文件: {filename}, 删除 {removed_count} 个标注")
            
#         except ET.ParseError as e:
#             logging.error(f"XML解析错误 {filename}: {str(e)}")
#         except Exception as e:
#             logging.error(f"处理文件 {filename} 时发生错误: {str(e)}")

# if __name__ == "__main__":
#     xml_folder = r'./Annotations'
#     target_classes = ["person"]
#     process_xml(xml_folder, target_classes)
#     print("批量处理完成,请检查日志文件 xml_process.log")

2. 修改标签的label名字

"""
使用python xml解析树解析xml文件,批量修改xml文件里object节点下name节点的text
"""


import glob
import xml.etree.ElementTree as ET
path = r'./Annotations'    # xml文件夹路径
i = 0
for xml_file in glob.glob(path + '/*.xml'):
    # print(xml_file)
    tree = ET.parse(xml_file) 
    obj_list = tree.getroot().findall('object') 
    for per_obj in obj_list:
        if per_obj[0].text == 'dangerous-behavior':    # 找到错误的标签“ dangerous-behavior ”
            per_obj[0].text = 'climbing'    # 修改成“自己想要的标签名”
            i = i+1

    tree.write(xml_file)    # 将改好的文件重新写入,会覆盖原文件
print('共完成了{}处替换'.format(i))

3. 划分YOLOv11数据集

# 该代码用于划分yolov11数据集
import os
import shutil
import random
 
# random.seed(0)  #随机种子,可自选开启
def split_data(file_path, label_path, new_file_path, train_rate, val_rate, test_rate):
	images = os.listdir(file_path)
	labels = os.listdir(label_path)
	images_no_ext = {os.path.splitext(image)[0]: image for image in images}
	labels_no_ext = {os.path.splitext(label)[0]: label for label in labels}
	matched_data = [(img, images_no_ext[img], labels_no_ext[img]) for img in images_no_ext if img in labels_no_ext]
 
	unmatched_images = [img for img in images_no_ext if img not in labels_no_ext]
	unmatched_labels = [label for label in labels_no_ext if label not in images_no_ext]
	if unmatched_images:
		print("未匹配的图片文件:")
		for img in unmatched_images:
			print(images_no_ext[img])
	if unmatched_labels:
		print("未匹配的标签文件:")
		for label in unmatched_labels:
			print(labels_no_ext[label])
 
	random.shuffle(matched_data)
	total = len(matched_data)
	train_data = matched_data[:int(train_rate * total)]
	val_data = matched_data[int(train_rate * total):int((train_rate + val_rate) * total)]
	test_data = matched_data[int((train_rate + val_rate) * total):]
 
	# 处理训练集
	for img_name, img_file, label_file in train_data:
		old_img_path = os.path.join(file_path, img_file)
		old_label_path = os.path.join(label_path, label_file)
		new_img_dir = os.path.join(new_file_path, 'train', 'images')
		new_label_dir = os.path.join(new_file_path, 'train', 'labels')
		os.makedirs(new_img_dir, exist_ok=True)
		os.makedirs(new_label_dir, exist_ok=True)
		shutil.copy(old_img_path, os.path.join(new_img_dir, img_file))
		shutil.copy(old_label_path, os.path.join(new_label_dir, label_file))
	# 处理验证集
	for img_name, img_file, label_file in val_data:
		old_img_path = os.path.join(file_path, img_file)
		old_label_path = os.path.join(label_path, label_file)
		new_img_dir = os.path.join(new_file_path, 'val', 'images')
		new_label_dir = os.path.join(new_file_path, 'val', 'labels')
		os.makedirs(new_img_dir, exist_ok=True)
		os.makedirs(new_label_dir, exist_ok=True)
		shutil.copy(old_img_path, os.path.join(new_img_dir, img_file))
		shutil.copy(old_label_path, os.path.join(new_label_dir, label_file))
	# 处理测试集
	for img_name, img_file, label_file in test_data:
		old_img_path = os.path.join(file_path, img_file)
		old_label_path = os.path.join(label_path, label_file)
		new_img_dir = os.path.join(new_file_path, 'test', 'images')
		new_label_dir = os.path.join(new_file_path, 'test', 'labels')
		os.makedirs(new_img_dir, exist_ok=True)
		os.makedirs(new_label_dir, exist_ok=True)
		shutil.copy(old_img_path, os.path.join(new_img_dir, img_file))
		shutil.copy(old_label_path, os.path.join(new_label_dir, label_file))
	print("数据集已划分完成")
 
if __name__ == '__main__':
	file_path = r"./data/JPEGImages"  # 图片文件夹
	label_path = r'./data/labels'  # 标签文件夹
	new_file_path = r"./VOCdevkit"  # 新数据存放位置
	split_data(file_path, label_path, new_file_path, train_rate=0.8, val_rate=0.1, test_rate=0.1)

本文代码皆来自于CSDN博主,本文是为了方便自己使用做了汇总,参考:

笑脸惹桃花-CSDN博客

voc xml标签批量修改,更改、删除指定类别-CSDN博客

python批量修改labelme标注的json文件中的标签名_labelme批量修改标签-CSDN博客

相关文章:

  • 基于SIMP算法的材料结构拓扑优化matlab仿真
  • 【32期获取股票数据API接口】如何用Python、Java等五种主流语言实例演示获取股票行情api接口之沪深A股融资融券历史走势股数据及接口API说明文档
  • Pseduo LiDAR(CVPR2019)
  • 【玩泰山派】2、制作buildroot镜像,并烧录
  • SQL Server 数据库实验报告
  • CAD插件实现:所有文字显示到列表、缩放、编辑——CAD-c#二次开发
  • 虚拟机第十三章-垃圾回收
  • 大咖访谈:Ftrans飞驰云联王泽瑞:安全即效率:数据交换新范式
  • 嵌入式Linux驱动开发基础知识(三)
  • Vue的学习总结-day01
  • 3.字节输出流FileOutputStream写出数据的三种方式
  • (C语言)单链表(1.0)(单链表教程)(数据结构,指针)
  • Python爬虫教程007:scrapy结合实际案例的简单使用
  • 深入讲解:智能合约中的读写方法
  • NumPy性能调优实战:从调试到性能分析的完整指南
  • Linux进阶——shell脚本语言
  • Bash详解
  • 轻量化大模型微调工具XTuner指令微调实战(下篇)
  • 如何创建一个自行设计的nginx的Docker Image
  • 后端开发常见的面试问题
  • 本溪 网站建设 做网站/做网络推广需要多少钱
  • 个人网站设计模版html/百度推广服务
  • c2b平台都有哪些网站/seo高级教程
  • 房地产设计部岗位职责/seo月薪
  • 基于dijango的网站开发/网站怎么做优化排名
  • 哈尔滨网站关键字优化/湖南专业seo优化