【FileNotFoundError】想要使用yolov5算法训练自己数据,在制作训练集时出现的文件找不到的问题

在使用YOLOv5算法,想要训练自己的数据集,在跟随其他博客教程中,在制作自己的训练集时出现以下问题:在text_to_yolo.py运行时,发生这个错误:FileNotFoundError

这个是text_to_yolo.py文件

import xml.etree.ElementTree as ET
import os
from os import getcwd

sets = ['train', 'val', 'test']
classes = ["have mask", "no mask"]  # 改成自己的类别
abs_path = os.getcwd()
print(abs_path)

def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return x, y, w, h


def convert_annotation(image_id):
    in_file = open('D:/desk/yolov5-5.0/yolov5-5.0/VOCData/Annotations/%s.xml' % (image_id), encoding='UTF-8')
    out_file = open('D:/desk/yolov5-5.0/yolov5-5.0/labels/%s.txt' % (image_id), 'w')
    # 如果使用相对路径,即
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)
    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        # difficult = obj.find('Difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult) == 1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        b1, b2, b3, b4 = b
        # 标注越界修正
        if b2 > w:
            b2 = w
        if b4 > h:
            b4 = h
        b = (b1, b2, b3, b4)
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')


wd = getcwd()
for image_set in sets:
    if not os.path.exists('D:/desk/yolov5-5.0/yolov5-5.0/VOCData/labels/'):
        os.makedirs('D:/desk/yolov5-5.0/yolov5-5.0/VOCData/labels/')
    image_ids = open(r'D:/desk/yolov5-5.0/yolov5-5.0/VOCData/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()

    if not os.path.exists('D:/Yolov5/yolov5/VOCData/dataSet_path/'):
        os.makedirs('D:/desk/yolov5-5.0/yolov5-5.0/VOCData/dataSet_path/')

    list_file = open('dataSet_path/%s.txt' % (image_set), 'w')
    # 这行路径不需更改,这是相对路径
    for image_id in image_ids:
        list_file.write('D:/desk/yolov5-5.0/yolov5-5.0/VOCData/images/%s.jpg\n' % (image_id))
        convert_annotation(image_id)
    list_file.close()


img

这是运行结果的bug提示

Traceback (most recent call last):
  File "D:/desk/yolov5-5.0/yolov5-5.0/VOCDate/text_to_yolo.py", line 60, in <module>
    image_ids = open(r'D:/desk/yolov5-5.0/yolov5-5.0/VOCData/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
FileNotFoundError: [Errno 2] No such file or directory: 'D:/desk/yolov5-5.0/yolov5-5.0/VOCData/ImageSets/Main/train.txt'

进程已结束,退出代码1

希望得到CSDN上的博主指点~非常感谢!

你的第60行代码路径没有改,改成你的。

检查一下D:/desk/yolov5-5.0/yolov5-5.0/VOCData/ImageSets/Main/train.txt 有没有这个文件