Summer comprehensive training record - 2023

1. The code used

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)

Function: Convert <class name> , <xmin> , <ymin> , <xmax> , <ymax> type tags to <object-class>, <x_center>, <y_center> , <width>, <height> type tags

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)

Function: Extract the names of all pictures and txt files in two folders, and output different names between the two, for some pictures without corresponding labels, so that the training cannot be started

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):
    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)

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

        if not os.path.exists(label_folder):
            os.makedirs(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 = './annotations.json'  # 替换为实际的JSON文件路径
img_width = 2048  # 替换为实际图像的宽度
img_height = 2048  # 替换为实际图像的高度
convert_to_txt(json_file, img_width, img_height)

Function: It is used to convert the data in the json file into the label format used for yolov5 training. Note that the normalization operation is done in it. If the labels in your json are normalized, please do not use this code. will be normalized again.

2. Problems encountered (continuously updated)

yolov5 reported an error ValueError: not enough values ​​to unpack (expected 3, got 0) when running train

 My problem is that the label coordinates are not normalized, and it can be run after normalization

Guess you like

Origin blog.csdn.net/weixin_55504804/article/details/131523776