合并yolo标注label
输入两个路径的labels,可以特定的32类别的标注合并到target_dir 目录中的txt中
# -*- coding: utf-8 -*-
# @Time : 2024/6/19 17:57
# @Author : sjh
# @Site :
# @File : python_txt.py
# @Comment :
import os
# 定义源目录和目标目录
source_dir = r"E:\Download\Datasets\football\Football Analyzer.v1i.yolov8\train\labels"
target_dir = r"E:\Download\Datasets\football\Football Analyzer.v1i.yolov8\train\predict\labels"
# 确保目标目录存在
os.makedirs(target_dir, exist_ok=True)
# 遍历源目录下的所有文件
for filename in os.listdir(source_dir):
if filename.endswith(".txt"): # 只处理txt文件
source_path = os.path.join(source_dir, filename)
target_path = os.path.join(target_dir, filename)
with open(source_path, 'r') as src_file:
lines = src_file.readlines()
# 筛选出标签序号为0的行
filtered_lines = [line for line in lines if line.split()[0] == '32']
# 将筛选出的行追加到目标文件中
with open(target_path, 'a') as tgt_file:
tgt_file.writelines(filtered_lines)
更改标注的分类
输入路径,可以将类别为0的改为32
#更改数据集的分类
#将数据集txt的分类从1改为0
import os
label_source = r'E:\Download\Datasets\labels/'
label_dir = os.listdir(label_source)
for i in label_dir:
label_path = label_source + i
ans = ''
with open(label_path) as f:
line = f.readline()
while line:
cls = int(line[:2])
if cls == 0: ##todo
ans = ans + '32'+ ' '+line[1:]
line = f.readline()
print(ans)
print('___________________________________-')
with open(label_path, 'w') as outfile:
outfile.write(ans)
生成train.txt
输入图像的路径,然后设置输出的txt文件路径,最后会保存一个train.txt
import os
from pathlib import Path
# 设置图像文件夹的相对路径(相对于脚本运行的当前目录)
# 假设脚本与图像文件夹在同一个父目录下
relative_image_folder = r'E:\\Download\\Datasets\\football\\coco2017labels\\coco\\images\\val2017'
# 将字符串路径转换为Path对象
path = Path(relative_image_folder)
# 获取路径的所有部分
parts = list(path.parts)
# 获取当前工作目录
current_dir = os.getcwd()
# 将相对路径转换为绝对路径
absolute_image_folder = os.path.abspath(os.path.join(current_dir, relative_image_folder))
# 设置输出的txt文件路径(相对于当前工作目录)
output_txt = r'E:\Download\Datasets\football\coco2017labels\coco\\'+ parts[-1] + '.txt'
# 获取所有jpg文件的列表,并转换为相对路径
image_files = [os.path.relpath(os.path.join(absolute_image_folder, f), start=absolute_image_folder) for f in os.listdir(absolute_image_folder) if f.endswith('.jpg')]
# 按文件名排序,确保索引的顺序
image_files.sort()
# 写入txt文件
with open(output_txt, 'w') as f:
for image_path in image_files:
# 确保每个路径都是以'./'开头,表示相对当前目录
f.write('.' + '/'+parts[-2]+'/' + parts[-1] + '/' + image_path + '\n') # 使用os.path.sep保证跨平台兼容性
print('索引文件已生成。')
移动与txt同名的图像
作用可以去掉没有标注txt的图像
# -*- coding: utf-8 -*-
# @Time : 2024/6/19 18:20
# @Author : sjh
# @Site :
# @File : copy_images_for_annotations.py
# @Comment :
import os
import shutil
from pathlib import Path
def copy_images_for_annotations(annotation_dir, image_dir, target_image_dir):
# 确保目标目录存在
os.makedirs(target_image_dir, exist_ok=True)
# 遍历标注文件目录中的所有txt文件
for filename in os.listdir(annotation_dir):
if filename.endswith(".txt"):
base_name = os.path.splitext(filename)[0]
# 查找同名图像文件,不论后缀
for ext in ['.jpg', '.jpeg', '.png', '.bmp']:
image_path = os.path.join(image_dir, base_name + ext)
if os.path.exists(image_path):
target_path = os.path.join(target_image_dir, base_name + ext)
shutil.copy(image_path, target_path)
print(f"Copied {image_path} to {target_path}")
break
else:
print(f"No matching image found for {filename}")
# 示例用法
annotation_dir = r"E:\Download\Datasets\football\Football Analyzer.v1i.yolov8\train\predict\labels"# 已存在的txt文件路径
image_dir = r"E:\Download\Datasets\football\Football Analyzer.v1i.yolov8\train\images" # 所有的图像路径
target_image_dir = r"E:\Download\Datasets\football\Football Analyzer.v1i.yolov8\train\predict\images" # 目标移动后的图像路径
copy_images_for_annotations(annotation_dir, image_dir, target_image_dir)