The warning negative label/yolo label has a negative value?

The problem is shown in the figure above, and the scene appears: After the image is enhanced, the xml file is converted to a txt file.

Solution 1 (brainless and rude, effective in a second): directly convert the negative value into a positive value without affecting the labeling and training results, the code is as follows:

import os
def process_files_in_folder(folder_path):
    # 获取文件夹中的所有文件
    file_list = os.listdir(folder_path)

    for file_name in file_list:
        # 检查文件扩展名是否为txt
        if file_name.endswith('.txt'):
            file_path = os.path.join(folder_path, file_name)

            # 读取文件内容
            with open(file_path, 'r') as file:
                lines = file.readlines()

            # 处理文件中的负数
            processed_lines = []
            for line in lines:
                numbers = line.split()
                processed_numbers = []
                for number in numbers:
                    try:
                        number = float(number)
                        if number ==0:
                            number=int(number)
                        if number < 0:
                            number = abs(number)  # 将负数转换为正数
                    except ValueError:
                        pass
                    processed_numbers.append(str(number))

                processed_line = ' '.join(processed_numbers)
                processed_lines.append(processed_line)

            # 将处理后的内容写回文件
            with open(file_path, 'w') as file:
                file.write('\n'.join(processed_lines))
# 指定文件夹路径
folder_path = r'D:\datasets\oil\train\labels'#只需修改成你的txt路径
process_files_in_folder(folder_path)

Solution 2 (unraveling, fundamental solution): I read a csdn article : It is said that when the image is enhanced, the maximum value may be smaller than the minimum value, so there is a negative number, I think it makes sense, according to its modification method, convert it in xml txt file, add the following code:

 if xmlbox.find('xmin').text > xmlbox.find('xmax').text:
            # int.temp
            temp = xmlbox.find('xmin').text
            xmlbox.find('xmin').text = xmlbox.find('xmax').text
            xmlbox.find('xmax').text = temp
        if xmlbox.find('ymin').text > xmlbox.find('ymax').text:
            # int.temp
            temp = xmlbox.find('ymin').text
            xmlbox.find('ymin').text = xmlbox.find('ymax').text
            xmlbox.find('ymax').text = temp
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))

The interesting thing is that the txt file with negative numbers before is non-negative, but the txt file without negative numbers before has negative numbers again. After working for a long time, I found that another article by the big guy above found the problem . (However, it looks very troublesome, and I feel that it is not as good as my own solution haha)

Guess you like

Origin blog.csdn.net/dal1223/article/details/131406109