ReturnTmp's Blog ReturnTmp's Blog
首页
基础课程
编程语言
框架技术
运维笔记
人工智能
随笔摘录
  • 友链
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

ReturnTmp

分享有趣好玩的计算机知识
首页
基础课程
编程语言
框架技术
运维笔记
人工智能
随笔摘录
  • 友链
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 机器学习

  • 计算机视觉

    • 目标检测常用数据集及转换方式
    • YOLOv8 快速入门
    • YOLOv8『小目标』检测指南
    • YOLOv8 目标检测及图片裁剪
    • YOLOv8 断点续训
    • labelimg yolo 数据集生成及划分
    • 『LabelImg』使用小技巧
    • labelimg IndexError 错误
    • Flappy Bird AI 模型训练实战
    • YOLOv8 常用转换脚本
      • 前言
      • 重命名图片和标注文件
      • 标签种类序号批量修改
      • 标注文件转换脚本
    • YOLOv8
    • 中秋特别版『贪吃蛇』,但是『AI』
    • GPU 查询和抢占
    • 『SAHI』大图片小目标检测切片辅助超推理库
  • 软件体系架构

  • 基础课程
  • 计算机视觉
ReturnTmp
2023-09-17
目录

YOLOv8 常用转换脚本

# 前言

本文为 YOLOv8 常用转换脚本

# 重命名图片和标注文件

我们在进行 YOLOv8 训练的数据集合并的时候经常遇到文件重名问题,下面的脚本将会批量重命名图片和标注文件

注意:标注文件为 txt 格式,图片为 jpg 格式,同时标注文件和图片文件在同一目录,如果有偏差请自行微调代码

rename_with_timestamp.py

import os
import time
import argparse

parser = argparse.ArgumentParser(description='Batch rename files with timestamp')
parser.add_argument('--dir', type=str, help='Directory path')
args = parser.parse_args()

file_path = args.dir
file_list = os.listdir(file_path)

for file in file_list:
    if file.endswith(".txt"):
        print(file)
        base_name = os.path.splitext(file)[0]
        extension = os.path.splitext(file)[1]
        timestamp = str(round(time.time() * 1000))
        new_name = base_name + timestamp + extension
        os.rename(os.path.join(file_path, file), os.path.join(file_path, new_name))  # 重命名txt
        image_file = os.path.join(file_path, base_name + ".jpg")
        if os.path.exists(image_file):
            os.rename(image_file, os.path.join(file_path, base_name + timestamp + ".jpg"))  # 重命名jpg文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

使用方式

python ./rename_with_timestamp.py --dir ./
1

# 标签种类序号批量修改

txt_label_index_rename.py

import os
import re
import argparse

parser = argparse.ArgumentParser(description='Replace the label in text files')
parser.add_argument('--dir', type=str, help='Directory path')
args = parser.parse_args()

path = args.dir
files = []
for file in os.listdir(path):
    if file.endswith(".txt"):
        files.append(os.path.join(path, file))

for file in files:
    with open(file, 'r') as f:
        new_data = re.sub('^1', '0', f.read(), flags=re.MULTILINE)
    with open(file, 'w') as f:
        f.write(new_data)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

上面的代码会将文件中的所有 txt 标注文件中的标签 1 替换为 0

使用形式

python ./txt_label_index_rename.py --dir ./
1

# 标注文件转换脚本

三种不同格式标注文件转换之前的文章讲到过,此处不赘述

编辑 (opens new window)
上次更新: 2023/10/14, 10:01:13
Flappy Bird AI 模型训练实战
YOLOv8

← Flappy Bird AI 模型训练实战 YOLOv8→

最近更新
01
百度网盘加速
03-24
02
新版 PyCharm 设置 Conda 虚拟环境
03-24
03
腾讯云域名转到阿里云
03-24
更多文章>
Theme by Vdoing | Copyright © 2023-2024 ReturnTmp | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式