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

数据集,批量更新分类数值OR批量删除分类行数据

数据集批量更新分类OR删除分类行数据

import osdef remove_class_from_file(file_path, class_to_remove):"""从YOLO格式的标注文件中删除指定类别的行记录,并去除空行。:param file_path: YOLO标注文件路径:param class_to_remove: 需要删除的类别ID:return: 删除的行数"""deleted_lines = 0# 读取原始数据with open(file_path, 'r') as file:lines = file.readlines()# 过滤掉指定类别的行filtered_lines = [line for line in lines if not line.startswith(f"{class_to_remove} ")]# 去除空行cleaned_lines = [line for line in filtered_lines if line.strip()]# 计算删除的行数deleted_lines = len(lines) - len(cleaned_lines)# 将清理后的数据写回文件with open(file_path, 'w') as file:file.writelines(cleaned_lines)return deleted_linesdef update_class_in_file(file_path, old_class, new_class):"""将YOLO格式的标注文件中指定类别的数字更新为新的类别ID。:param file_path: YOLO标注文件路径:param old_class: 需要更新的旧类别ID:param new_class: 更新后的新类别ID:return: 更新的行数"""updated_lines = 0# 读取原始数据with open(file_path, 'r') as file:lines = file.readlines()# 更新类别IDupdated_lines = 0new_lines = []for line in lines:if line.startswith(f"{old_class} "):new_lines.append(line.replace(f"{old_class} ", f"{new_class} ", 1))updated_lines += 1else:new_lines.append(line)# 将更新后的数据写回文件with open(file_path, 'w') as file:file.writelines(new_lines)return updated_linesdef process_directory(directory_path, class_to_remove=None, old_class=None, new_class=None):"""处理指定目录下的所有YOLO标注文件,可以选择删除指定类别的记录或更新分类数字。:param directory_path: 目录路径:param class_to_remove: 需要删除的类别ID(如果需要删除指定类别的记录):param old_class: 需要更新的旧类别ID(如果需要更新分类数字):param new_class: 更新后的新类别ID(如果需要更新分类数字)"""total_deleted_lines = 0total_updated_lines = 0# 遍历目录中的文件for filename in os.listdir(directory_path):if filename.endswith('.txt'):  # 假设标注文件都是以 .txt 结尾的file_path = os.path.join(directory_path, filename)if class_to_remove is not None:deleted_lines = remove_class_from_file(file_path, class_to_remove)print(f"已从文件 {filename} 中删除类别 {class_to_remove} 的记录,共删除 {deleted_lines} 行。")total_deleted_lines += deleted_linesif old_class is not None and new_class is not None:updated_lines = update_class_in_file(file_path, old_class, new_class)print(f"已从文件 {filename} 中将类别 {old_class} 更新为 {new_class},共更新 {updated_lines} 行。")total_updated_lines += updated_linesif class_to_remove is not None:print(f"总共删除了 {total_deleted_lines} 行记录。")if old_class is not None and new_class is not None:print(f"总共更新了 {total_updated_lines} 行记录。")# 使用示例
directory_path = 'C:\\Users\\合格的牛马\\Desktop\\datasets\\test-delete\\labels'  # 替换为你的目录路径# 删除指定类别的记录
class_to_remove = 5  # 替换为你要删除的类别ID
process_directory(directory_path, class_to_remove=class_to_remove)# 更新指定类别的分类数字
old_class = 1  # 替换为旧类别ID
new_class = 100  # 替换为新类别ID
process_directory(directory_path, old_class=old_class, new_class=new_class)
http://www.lryc.cn/news/408268.html

相关文章:

  • 一款功能强大的视频编辑软件会声会影2023
  • 政安晨【零基础玩转各类开源AI项目】基于Ubuntu系统部署LivePortrait :通过缝合和重定向控制实现高效的肖像动画制作
  • 在Spring项目中使用Maven和BCrypt来实现修改密码功能
  • RedHat8安装Oracle19C
  • React系列面试题
  • C#:通用方法总结—第6集
  • Spark实时(一):StructuredStreaming 介绍
  • LangChain4j-RAG基础
  • git--本地仓库修改同步到远程仓库
  • 剑和沙盒 3 - 深度使用和解析Windows Sandbox
  • 深度学习loss
  • 编写一个Chrome插件,网页选择文字后,右键出现菜单“search with bing”,选择菜单后用bing搜索文字
  • 【算法】分割回文串
  • lua 游戏架构 之 游戏 AI (三)ai_attack
  • 大数据之Oracle同步Doris数据不一致问题
  • visual studio 问题总结
  • go-错误码的最佳实践
  • Python面试题:使用Matplotlib和Seaborn进行数据可视化
  • 模拟实现c++中的vector模版
  • uniapp安卓通过绝对路径获取文件
  • Known框架实战演练——进销存业务单据
  • 解决npm依赖树冲突的方法以及npm ERR! code ERESOLVE错误的解决方案
  • Spring Boot + Spring Batch + Quartz 整合定时批量任务
  • C++STL简介(二)
  • 嵌入式高频面试题100道及参考答案(3万字长文)
  • python爬虫-事件触发机制
  • LeetCode-day27-3106. 满足距离约束且字典序最小的字符串
  • C++中的static_cast函数
  • 从零开始学习网络安全渗透测试之基础入门篇——(二)Web架构前后端分离站Docker容器站OSS存储负载均衡CDN加速反向代理WAF防护
  • 2679. 矩阵中的和