qq_60601377 2022-04-12 17:23 采纳率: 0%
浏览 418

【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上的博主指点~非常感谢!
  • 写回答

2条回答 默认 最新

  • 缔宇diyu 2022-04-12 17:57
    关注

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

    评论

报告相同问题?

问题事件

  • 修改了问题 4月12日
  • 修改了问题 4月12日
  • 创建了问题 4月12日

悬赏问题

  • ¥15 easypoi导出复杂excel
  • ¥20 C++初高中竞赛题,devc++可以通过的
  • ¥60 二次元手游日常任务自动化代肝(相关搜索:自动化)
  • ¥15 mysql将查询的结果作为动态列名怎么实现
  • ¥50 python自动地图截图脚本
  • ¥15 悬赏一本书(内含Matlab代码)的书名、作者
  • ¥20 瑞萨RA4M1芯片刷写为arduino r4 minima
  • ¥15 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上