暑期综合实训记录——2023

一、用到的代码

import os

def convert_labels(input_folder, output_folder):
    # 创建输出文件夹
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 遍历输入文件夹中的文件
    for filename in os.listdir(input_folder):
        if filename.endswith('.txt'):
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, filename)

            with open(input_path, 'r') as f:
                lines = f.readlines()

            output_lines = []
            for line in lines:
                # 解析原始标签
                label_info = line.strip().split()
                class_name = label_info[0]
                xmin, ymin, xmax, ymax = map(float, label_info[1:])

                # 计算中心坐标和宽度、高度
                width = xmax - xmin
                height = ymax - ymin
                x_center = xmin + width / 2.0
                y_center = ymin + height / 2.0

                # 构建转换后的标签行
                converted_line = f"{class_name} {x_center} {y_center} {width} {height}\n"
                output_lines.append(converted_line)

            with open(output_path, 'w') as f:
                f.writelines(output_lines)


# 设置输入文件夹路径和输出文件夹路径
input_folder = r'D:\yolov5-master\car_data\labels\val'
output_folder = r'D:\label_val'

# 调用函数进行转换
convert_labels(input_folder, output_folder)

功能:将<class name> , <xmin> , <ymin> , <xmax> , <ymax>型的标签转化为<object-class>, <x_center>, <y_center> ,<width>, <height>型

import os
import fnmatch

def extract_file_names(folder):
    file_names = []
    for root, dirnames, filenames in os.walk(folder):
        for filename in filenames:
            file_names.append(os.path.splitext(filename)[0])  # 提取文件名称,不包含后缀
    return file_names

# 定义两个文件夹路径
folder1 = r'D:\综合实训\data\test'
folder2 = r'D:\pythonProject\实训\test'

# 提取文件夹1下的文件名称
file_names1 = extract_file_names(folder1)

# 提取文件夹2下的文件名称
file_names2 = extract_file_names(folder2)

# 找出两个文件夹中不相同的名称
different_names = set(file_names1) ^ set(file_names2)

# 输出不相同的名称
for name in different_names:
    print(name)

功能:提取两个文件夹下所有图片和txt文件名称,并且输出两者之间不同的名称,用于有的图片没有对应的标签导致不能开始训练

import json
import os

category_names = ['ph2', 'p11', 'pl5', 'pne', 'pb', 'il60', 'pl80', 'il70', 'pl100', 'il80', 'po', 'w55', 'pl40', 'pn',
				  'pm55', 'pa14', 'w32', 'pl20', 'ph3', 'p27', 'p26', 'p12', 'i5', 'p25', 'pl120', 'pl60', 'pl30',
				  'pl15', 'pl70', 'pl50', 'ip', 'w30', 'pg', 'p10', 'io', 'pr40', 'p5', 'p3', 'i2', 'i4', 'w46', 'ph4',
				  'il50', 'wo', 'pm30', 'pa13', 'ps', 'ph5', 'p23', 'pm20', 'pa10', 'w57', 'w13', 'p19', 'w59', 'il90',
				  'il100', 'pl90', 'pl10', 'p1', 'i1', 'w58', 'p9', 'p6', 'ph4.2', 'ph4.5', 'p22', 'pm10', 'w63', 'w22',
				  'i10', 'pl0', 'ph3.5', 'ph2.8', 'pr80', 'il110', 'pr60', 'pa12', 'pm35', 'pl110', 'p18', 'p16', 'p2',
				  'p17', 'w3', 'p14', 'w45', 'i13', 'pw3.2', 'ph2.2', 'p8', 'w21', 'ph4.8', 'pm2.5', 'pr20', 'w47',
				  'w35', 'i11', 'pr45', 'w42', 'pw4', 'pr30', 'w8', 'pr50', 'p28', 'ph5.3', 'pl35', 'ph2.9', 'ph4.3',
				  'pm2', 'pr70', 'w41', 'i12', 'w18', 'ph3.2', 'ph2.5', 'p15', 'p21', 'p20', 'i3', 'i14', 'pa8', 'pw3',
				  'pw4.5', 'w28', 'ph2.1', 'w20', 'pm5', 'w16', 'w15', 'pl25', 'w66', 'pm15', 'p24', 'p4', 'ph1.5',
				  'i15', 'w12', 'w37', 'pr100', 'w34', 'pw3.5', 'pm40', 'pm50', 'w5', 'pw2.5', 'p13', 'w24', 'pm8',
				  'pl3', 'w43', 'w56', 'w38', 'pr10', 'w10', 'ph2.4', 'pm13', 'pw4.2', 'pw2', 'ph5.5', 'w2', 'ph3.8',
				  'ph3.3', 'w26', 'w44', 'pm46', 'w50', 'w31', 'pm25', 'p7', 'pc', 'pl4', 'pm1.5', 'w49', 'ph2.6',
				  'pn40', 'w48', 'pl65', 'w60', 'w1', 'w62', 'ph4.4']


def convert_to_txt(json_file, img_width, img_height, output_dir):
	with open(json_file) as f:
		data = json.load(f)

	for img_id, img_info in data['imgs'].items():
		img_path = img_info['path']
		img_name = os.path.basename(img_path).split('.')[0]  # 提取图片名称(假设路径为Unix格式)
		label_folder = os.path.dirname(img_path)

		if label_folder.startswith('test'):
			output_subdir = 'test'
		elif label_folder.startswith('train'):
			output_subdir = 'train'
		else:
			output_subdir = 'other'

		txt_file = os.path.join(output_dir, output_subdir, label_folder, img_name + '.txt')

		if not os.path.exists(os.path.join(output_dir, output_subdir, label_folder)):
			os.makedirs(os.path.join(output_dir, output_subdir, label_folder))

		with open(txt_file, 'w') as f:
			objects = img_info['objects']
			for obj in objects:
				category = obj['category']
				bbox = obj['bbox']
				xmin = bbox['xmin']
				ymin = bbox['ymin']
				xmax = bbox['xmax']
				ymax = bbox['ymax']

				if category not in category_names:
					category_names.append(category)

				category_idx = category_names.index(category)

				line = f"{category_idx} {xmin / img_width} {ymin / img_height} {xmax / img_width} {ymax / img_height}\n"
				f.write(line)

		print(f"Converted {img_path} to {txt_file}")


# 调用函数进行转换
json_file = 'D:/annotations.json'  # 替换为实际的JSON文件路径
img_width = 2048  # 替换为实际图像的宽度
img_height = 2048  # 替换为实际图像的高度
output_dir = 'D:/result'  # 替换为实际输出文件夹路径
convert_to_txt(json_file, img_width, img_height, output_dir)

功能:用于将json文件里的数据转化为yolov5训练所用的标签格式,注意其中做啦归一化操做,如果你的json里的标签是归一化好的,那么请勿使用,该代码会再次归一化。

二、遇到的问题(持续更新中)

yolov5在运行train 时报错ValueError: not enough values to unpack (expected 3, got 0)

 我的问题是标签坐标未归一化,归一化完之后即可运行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值