基于YOLOv5的钢材表面缺陷检测

配置环境

conda create -n yolo
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple # 亲测华为云最稳定

# 如果在下载时提示timeout,试试这句命令:
pip --default-timeout=100 install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple

1、将xml文件转为符合yolo的txt文件,代码如下:

import xml.etree.ElementTree as ET
import os


# box [xmin,ymin,xmax,ymax]
def convert(size, box):
    x_center = (box[2] + box[0]) / 2.0
    y_center = (box[3] + box[1]) / 2.0
    # 归一化
    x = x_center / size[0]
    y = y_center / size[1]
    # 求宽高并归一化
    w = (box[2] - box[0]) / size[0]
    h = (box[3] - box[1]) / size[1]
    return (x, y, w, h)


# xml2yolo
def convert_annotation(xml_paths, yolo_paths, classes):
    xml_files = os.listdir(xml_paths)
    # 生成无序文件列表
    for file in xml_files:

        xml_file_path = os.path.join(xml_paths, file)
        yolo_txt_path = os.path.join(yolo_paths, file.split(".")[0]
                                     + ".txt")
        tree = ET.parse(xml_file_path)
        root = tree.getroot()
        size = root.find("size")
        # 获取xml的width和height的值
        w = int(size.find("width").text)
        h = int(size.

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 10:38:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 10:38:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 10:38:06       82 阅读
  4. Python语言-面向对象

    2024-06-16 10:38:06       91 阅读

热门阅读

  1. RDF 简介

    2024-06-16 10:38:06       28 阅读
  2. python字符串篇进阶练习

    2024-06-16 10:38:06       30 阅读
  3. 数据结构之B树

    2024-06-16 10:38:06       43 阅读
  4. [xmake]构建静态库和动态库

    2024-06-16 10:38:06       29 阅读
  5. 什么是 WebXR Device API?

    2024-06-16 10:38:06       32 阅读
  6. LeetCode538.把二叉搜索树转换为累加树

    2024-06-16 10:38:06       32 阅读
  7. Linux权限提升四

    2024-06-16 10:38:06       27 阅读
  8. Android Service学习笔记

    2024-06-16 10:38:06       22 阅读
  9. Oracle中如何定义定时器

    2024-06-16 10:38:06       30 阅读
  10. 实战tcpdump4.99.4交叉编译

    2024-06-16 10:38:06       32 阅读