基于yolov5的数据集自动标注功能脚本工具【附代码】

近年来,随着深度学习的迅猛发展,计算机视觉领域取得了巨大的突破。其中,目标检测是计算机视觉中的一个重要任务,它在许多应用领域中起到了至关重要的作用。然而,目标检测所需的大量标注数据集的制作却是一项耗时且繁琐的工作

为了解决这个问题,本文实现了一个基于YOLOv5的自动数据集标注功能脚本。YOLOv5是当今目标检测领域的一种优秀模型,其高效准确的检测能力被广泛认可。

我们的自动数据集标注工具通过使用已经训练好的YOLOv5模型,可以快速而准确地对输入的图像进行目标检测,将检测结果存储为标签文件(xml格式),生成标注数据集。这样,用户无需手动进行标注,大大提高了数据集标注的效率和准确性,为研究者和开发者节省宝贵的时间和精力。

说明:该工具还是一个基线版本,后续会根据用户反馈不定时更新


该工具是在之前yolov5剪枝与知识蒸馏项目的基础上更新的脚本功能,所以直接clone该项目即可,或者将我的代码复制到你自己代码中。

使用说明

先下载完整项目代码

1.git clone https://github.com/YINYIPENG-EN/Knowledge_distillation_Pruning_Yolov5.git

2.cd tools

3.python auto_label.py --weights [权重路径] --source [需要标注的数据集路径] 

运行脚本后会自动对数据集进行目标检测,标注后的xml标签文件和检测效果图会自动保存在项目下的runs/ Datasets/DetectImages中。检测结果和标注结果如下图:

可以根据检测结果图像来看标注结果是否准确。

xml格式说明:<size>存放的是图像的大小,<name>标签存放的是类名,<bndbox>标签中存放的是box标签,分别对应标注框的左上角右下角以及检测结果的置信度。【这里并未转成center_x,center_y,w,h】。xyxy->xywh的部分在我项目voc_label.py中有,具体使用说明可以参考我项目readme.md训练自己的数据集。

如果需要指定类别进行标注,可以在运行代码的时候加上--classes 以指定特定的类。


注意事项:

自动标注的结果取决于训练好的YOLOv5模型的准确率。因此,我们建议在使用自动标注功能时,用户应该先对所使用的模型进行准确性检验,以确保标注结果的可靠性。对于那些标注结果不准确的样本,用户可以通过标注工具进行手动调整,以进一步提高数据集的准确性。

需要特别说明的是,虽然我们的自动标注功能可以大大减少标注工作量,但我们并不能保证所有的标注结果都是完全准确的。这是因为目标检测本身就具有一定的误差率,而且自动标注功能仍然处于发展阶段,还有待不断的改进和优化。

在未来的更新中,我们计划增加调整边界框的功能,以帮助用户更方便地对标注结果进行调整。我们会继续努力改进自动标注功能,提高其准确率和稳定性,以满足用户在目标检测任务中的需求。


标注代码如下(建议还是clone我项目代码中使用):

# 该脚本的功能是通过已训练好的模型自动标注数据集
# 2024.01.28
# 作者:yinyipeng
# 邮箱:15930920977@163.com
import os
import argparse
import torch
from pathlib import Path
import cv2
import xml.etree.cElementTree as ET
from models.experimental import attempt_load
from utils.datasets import LoadImages
from utils.general import set_logging, check_img_size, non_max_suppression, scale_coords, colorstr, increment_path, \
    print_args
from utils.plots import Annotator, colors
from utils.torch_utils import select_device, time_sync

FILE = Path(__file__).resolve()

def create_xml_file(image_name, img0, detections, save_path):
    '''
    image_name:图像名称
    img0:原始图像
    detections:检测结果。列表形式,每个列表中对应该图像所有目标(dict形式)
    '''
    h, w, c = img0.shape
    root = ET.Element('annotation')
    folder = ET.SubElement(root, "folder")
    folder.text = 'images'
    filename = ET.SubElement(root, "filename")
    filename.text = image_name
    size = ET.SubElement(root, "size")
    width = ET.SubElement(size, "width")
    width.text = str(w)
    height = ET.SubElement(size, "height")
    height.text = str(h)
    depth = ET.SubElement(size, "depth")
    depth.text = str(c)

    for detection in detections:
        object = ET.SubElement(root, "object")
        name = ET.SubElement(object, "name")
        name.text = detection["class"]  # 获得类名
        # boxes信息
        bndbox = ET.SubElement(object, "bndbox")
        xmin = ET.SubElement(bndbox, "xmin")
        xmin.text = str(detection["xmin"])
        ymin = ET.SubElement(bndbox, "ymin")
        ymin.text = str(detection["ymin"])
        xmax = ET.SubElement(bndbox, "xmax")
        xmax.text = str(detection["xmax"])
        ymax = ET.SubElement(bndbox, "ymax")
        ymax.text = str(detection["ymax"])
        conf = ET.SubElement(bndbox, "conf")
        conf.text = detection["conf"]
    tree = ET.ElementTree(root)
    xml_file_path = f"{image_name.split('.')[0]}.xml"  # 生成的XML文件名与图像文件名相同
    tree.write(os.path.join(save_path, xml_file_path))

@torch.no_grad()
def run(weights='yolov5s.pt',  # model.pt path(s)
        source='data/images',  # file/dir/URL/glob, 0 for webcam
        imgsz=640,  # inference size (pixels)
        conf_thres=0.25,  # confidence threshold
        iou_thres=0.45,  # NMS IOU threshold
        max_det=1000,  # maximum detections per image
        device='',  # cuda device, i.e. 0 or 0,1,2,3 or cpu
        nosave=False,  # do not save images/videos
        classes=None,  # filter by class: --class 0, or --class 0 2 3
        agnostic_nms=False,  # class-agnostic NMS
        augment=False,  # augmented inference
        project='runs/Datasets',  # save results to project/name
        name='DetectImages',  # save results to project/name
        line_thickness=3,  # bounding box thickness (pixels)
        hide_labels=False,  # hide labels
        hide_conf=False,  # hide confidences
        half=False,  # use FP16 half-precision inference
        exist_ok=False
        ):
    source = str(source)
    save_img = not nosave and not source.endswith('.txt')  # save inference images
    save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
    (save_dir).mkdir(parents=True, exist_ok=True)  # make dir
    # Initialize
    set_logging()
    device = select_device(device)
    half &= device.type != 'cpu'  # half precision only supported on CUDA

    # Load model
    w = str(weights[0] if isinstance(weights, list) else weights)
    _, _ = 64, [f'class{i}' for i in range(1000)]  # assign defaults

    model = torch.jit.load(w) if 'torchscript' in w else attempt_load(weights, map_location=device)
    stride = int(model.stride.max())  # model stride
    names = model.module.names if hasattr(model, 'module') else model.names  # get class names
    if half:
        model.half()  # to FP16
    imgsz = check_img_size(imgsz, s=stride)  # check image size

    # Dataloader
    dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=True)
    bs = 1  # batch_size

    # Run inference
    if device.type != 'cpu':  # 如果为pt文件且为GPU
        model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.parameters())))  # run once
    dt, seen = [0.0, 0.0, 0.0], 0
    for path, img, im0s, vid_cap in dataset:
        pred_list = []

        t1 = time_sync()
        img = torch.from_numpy(img).to(device)
        img = img.half() if half else img.float()  # uint8 to fp16/32
        img = img / 255.0  # 0 - 255 to 0.0 - 1.0
        if len(img.shape) == 3:
            img = img[None]  # expand for batch dim
        t2 = time_sync()
        dt[0] += t2 - t1  # 记录图像处理时间

        # Inference
        visualize = False
        pred = model(img, augment=augment, visualize=visualize)[0]
        t3 = time_sync()
        dt[1] += t3 - t2  # 记录推理时间(不含NMS)

        # NMS
        pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
        dt[2] += time_sync() - t3  # 记录含NMS时间

        # Process predictions
        for i, det in enumerate(pred):  # per image
            seen += 1
            p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)

            p = Path(p)  # to Path
            save_path = str(save_dir / p.name)  # img.jpg
            s += '%gx%g ' % img.shape[2:]  # print string
            annotator = Annotator(im0, line_width=line_thickness, example=str(names))
            if len(det):  # 获取目标数量
                # Rescale boxes from img_size to im0 size
                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
                # det shape [number of targets,6],6:x1y1x2y2,conf,cls
                # Print results
                for c in det[:, -1].unique():
                    n = (det[:, -1] == c).sum()  # detections per class
                    s += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string
                for i in range(len(det)):
                    # get class name
                    class_name = names[int(det[i, -1].cpu())]
                    pred_dict = {}
                    pred_dict['class'] = class_name
                    pred_dict['xmin'] = int(det[i, 0].cpu())
                    pred_dict['ymin'] = int(det[i, 1].cpu())
                    pred_dict['xmax'] = int(det[i, 2].cpu())
                    pred_dict['ymax'] = int(det[i, 3].cpu())
                    pred_dict['conf'] = f'{det[i, -2]:.2f}'
                    pred_list.append(pred_dict)
                # Write results
                for *xyxy, conf, cls in reversed(det):

                    if save_img:  # Add bbox to image
                        c = int(cls)  # integer class
                        label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
                        annotator.box_label(xyxy, label, color=colors(c, True))

            # Print time (inference-only)
            print(f'{s}Done. ({t3 - t2:.3f}s)')
            create_xml_file(p.name, im0, pred_list, save_dir)
            # Stream results
            im0 = annotator.result()
            # Save results (image with detections)
            if save_img:
                if dataset.mode == 'image':
                    cv2.imwrite(save_path, im0)



    # Print results
    t = tuple(x / seen * 1E3 for x in dt)  # speeds per image
    print(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
    if save_img:
        print(f"Results saved to {colorstr('bold', save_dir)}{''}")

def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', nargs='+', type=str, default='../yolov5s.pt', help='model path(s)')
    parser.add_argument('--source', type=str, default='../data/images', help='file/dir/')
    parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
    parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
    parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
    parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
    parser.add_argument('--device', default='0', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
    parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    parser.add_argument('--project', default='../runs/Datasets', help='save results to project/name')
    parser.add_argument('--name', default='DetectImages', help='save results to project/name')
    parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
    parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
    parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
    parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    opt = parser.parse_args()
    opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1  # expand
    print_args(FILE.stem, opt)
    return opt


def main(opt):
    run(**vars(opt))


if __name__ == "__main__":
    opt = parse_opt()
    main(opt)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/353931.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

FlashInternImage实战:使用 FlashInternImage实现图像分类任务(二)

文章目录 训练部分导入项目使用的库设置随机因子设置全局参数图像预处理与增强读取数据设置Loss设置模型设置优化器和学习率调整策略设置混合精度&#xff0c;DP多卡&#xff0c;EMA定义训练和验证函数训练函数验证函数调用训练和验证方法 运行以及结果查看测试完整的代码 在上…

mac配置L2TP连接公司内网

1. 打开系统设置 2. 打开网络 3. 点击网络页面其他服务右下角三个点&#xff0c;添加VPN配置中的L2TP 4. 配置VPN&#xff0c;服务器填写公司的服务器ip&#xff0c;共享密钥没有可以随便填写 5. 打开终端编辑文件 sudo vim /etc/ppp/opt…

Linux系统简介及发展历史

Linux的概况 Linux是自由软件 Linux是一种类UNIX操作系统。Linux内核由Linus Torvalds在1991年发布。在加上用户空间的应用程序之后&#xff0c;成为Linux操作系统 只要遵循GNU通用公共许可证&#xff08;GPL&#xff09;&#xff0c;任何个人和机构都可以自由地使用Linux的所…

网络安全02--负载均衡下的webshell连接

目录 一、环境准备 1.1ubentu虚拟机一台&#xff0c;docker环境&#xff0c;蚁剑 1.2环境压缩包&#xff08;文件已上传资源&#xff09;&#xff1a; 二、开始复原 2.1上传ubentu&#xff1a; 2.2解压缩 2.3版本20没有docker-compose手动下载&#xff0c;包已上传资源 …

Android双指缩放ScaleGestureDetector检测放大因子大图移动到双指中心点ImageView区域中心,Kotlin(2)

Android双指缩放ScaleGestureDetector检测放大因子大图移动到双指中心点ImageView区域中心&#xff0c;Kotlin&#xff08;2&#xff09; 在 Android ScaleGestureDetector检测双指缩放Bitmap基于Matrix动画移动到双指捏合中心点ImageView区域中心&#xff0c;Kotlin-CSDN博客 …

vue+Element UI实现省市区镇四级联动

shigen坚持更新文章的博客写手&#xff0c;擅长Java、python、vue、shell等编程语言和各种应用程序、脚本的开发。记录成长&#xff0c;分享认知&#xff0c;留住感动。 在一些需要填写地址的前端页面中&#xff0c;总是少不了需要填写地址的级联选择器&#xff0c;类似这样的&…

【Linux】第三十六站:信号

文章目录 一、信号的概念1.信号概念2.前台与后台进程3.信号的处理4.硬件层面5.信号与我们的代码是异步的 二、信号的产生1.产生的方式2.键盘组合键3.kill命令4.系统调用4.1 kill系统调用4.2 raise4.3 abort 5.异常软件条件5.1 异常产生信号5.2 alarm&#xff08;软件条件产生信…

【Linux】第三十七站:信号保存

文章目录 一、信号发送二、信号保存1.为什么要进行信号保存&#xff1f; 三、阻塞信号1.信号的一些相关概念2.在内核中的表示3.sigset_t4.信号集操作函数5.sigprocmask6.sigpending7. 总结 一、信号发送 如下所示&#xff0c;对于普通信号&#xff0c;它的编号是从1~31。这个是…

N-141基于springboot,vue网上拍卖平台

开发工具&#xff1a;IDEA 服务器&#xff1a;Tomcat9.0&#xff0c; jdk1.8 项目构建&#xff1a;maven 数据库&#xff1a;mysql5.7 系统分前后台&#xff0c;项目采用前后端分离 前端技术&#xff1a;vueelementUI 服务端技术&#xff1a;springbootmybatis-plusredi…

九州金榜|为什么鼓励式家庭教育?

鼓励式教育是一种积极的教育方式&#xff0c;它强调通过鼓励和肯定来激发孩子的积极性和自信心&#xff0c;帮助孩子更好地成长和发展。在家庭教育中&#xff0c;鼓励式教育同样具有重要意义。九州金榜家庭教育和大家一起探讨关于鼓励式教育的好处以及意义&#xff1a; 一.有助…

JRT实体比对

之前已经实现了JRT实体编译的菜单&#xff0c;用Linux指令编译放在网站下的实体代码。那么就有个问题&#xff0c;有人就是直接换实体jar文件不修改网站的实体代码。或者就只修改实体代码不编译搁置在那里&#xff0c;那么后面更新实体的人就得给别人背锅&#xff0c;后面人新编…

机器学习 | 如何使用 Seaborn 提升数据分析效率

Seaborn和Matplotlib都是Python可视化库&#xff0c;它们都可以用于创建各种类型的图表。但是&#xff0c;Seaborn 和Matplotlib在概念和设计上有一些不同。 Matplotlib虽然已经是比较优秀的绘图库了&#xff0c;但是它有个今人头疼的问题&#xff0c;那就是API使用过于复杂&am…

Blender教程(基础)-物体的移动、旋转与缩放-04

一、新建一个立方体 ShiftA新建一个立方体用来演示。 二、物体的移动 xyz轴移动 点击下图图左侧的移动选项后&#xff0c;选中要移动的物体&#xff0c;会出现三个箭头的方向&#xff0c;这分别代表沿着x、y、z轴移动。xyz平面移动 这个小正方体代表沿着某一个面移动&#…

04.领域驱动设计:了解聚合和聚合根,怎样设计聚合

目录 1、概述 2、聚合 3、聚合根 4、怎么设计聚合 4.1 聚合的构建过程主要步骤 第 1 步&#xff1a;采用事件风暴。 第 2 步&#xff1a;选出聚合根。 第 3 步&#xff1a;找出与聚合根关联的所有紧密依赖的实体和值对象。 第 4 步&#xff1a;画出对象的引用和依赖模型…

vue3框架基本使用

一、安装包管理工具 vite和vue-cli一样&#xff0c;都是脚手架。 1.node版本 PS E:\vuecode\vite1> node -v v18.12.12.安装yarn工具 2.1 yarn简单介绍 yarn是一个包管理工具&#xff0c;也是一个构建、打包工具 yarn需要借助npm进行安装&#xff1a;执行的命令行npm i…

找不同-《企业应用架构模式》2024典藏版

DDD领域驱动设计批评文集 做强化自测题获得“软件方法建模师”称号 《软件方法》各章合集 以下是2004年《企业应用架构模式》中译本和2024年《企业应用架构模式》典藏版译本的页面。 您能从中找出至少10处不同吗&#xff1f; 如何选择UMLChina服务 UMLChina公众号精选&…

Blender教程(基础)-面的细分与删除、挤出选区-07

一、Blender之面的细分 新建一个立方体&#xff0c;在编辑模式下、选中一个面。 在选中的面上单击右键弹出细分选项&#xff0c;选择细分。 在选中细分后、会默认细分1次。修改细分次数在左下角 二、Blender之面的删除 选择中需要操作的面&#xff0c;在英文状态下按X键弹…

VSCode 1.85.0更新的3个实用功能

1、单个文件可直接拖拽为独立窗口 当单文件过长&#xff0c;直接分成两个视图就不用上下频繁滚动 2、将终端移动到编辑器区域 此时&#xff0c;终端也可像文件一样拖拽为独立窗口 3、文件夹目录粘性头部 默认关闭&#xff0c;需要设置 "workbench.tree.enableStickyScro…

Linux权限的概念,shell命令以及运行原理

目录 1.shell命令以及运行原理2.Linux权限2.1Linux中的两类用户2.2Linux权限管理2.2.1文件访问者的分类&#xff08;人&#xff09;2.2.2文件类型和访问权限&#xff08;事物属性&#xff09;2.2.3文件的类型以及权限的缩写2.2.4文件权限值的表示方法2.2.5文件访问权限的相关设…

HTML 曲线图表特效

下面是代码 <!doctype html> <html> <head> <meta charset"utf-8"> <title>基于 ApexCharts 的 HTML5 曲线图表DEMO演示</title><style> body {background: #000524; }#wrapper {padding-top: 20px;background: #000524;b…
最新文章