YOLOv5系列 1、制作自己的数据集

YOLOv5系列 1、制作自己的数据集
YOLOv5系列 2、使用yolov5识别自己的数据

文章目录

前言

本文所使用的Yolov5为6.1版本,所用为GPU版(亲测CPU也一样能跑,只是速度会慢很多),使用的环境是torch1.7.1+cuda10.1。

一、下载Labelme

Labelme的安装很简单,如果想直接安装就直接在命令行中:

pip install labelme

如果是想把labelme安装在Anaconda的虚拟环境虚拟环境中,则需要先进入虚拟环境中,再安装:

activate 自己的想安装的环境名称
pip install labelme

如果想用Anaconda单独给Labelme装个虚拟环境则:

conda create -n labelme python=3.6
activate labelme
pip install labelme

二、Labelme使用步骤

1.打开Labelme

根据第一步中安装的位置,使用命令行在相应的位置中直接输入labelme,就可打开:

labelme

YOLOv5系列 1、制作自己的数据集
因为我是直接安装的,所以没有进虚拟环境。打开后就会自动跳出下面这个界面:
YOLOv5系列 1、制作自己的数据集
上面标注的是我们标记主要使用的功能,先使用Open dir打开我们图像数据所在文件夹:
YOLOv5系列 1、制作自己的数据集

2.Labelme标记数据集

这边是从kaggle上下载的一个检测口罩的数据集,然后点击Create Polygons,因为是使用Yolov5做的,所以在图像上点击右键,选择create Rectangle,再将口罩框起来。

YOLOv5系列 1、制作自己的数据集
然后点击ok,继续标记下一个口罩,全部标记好后,点击下一张或者点击保存:
YOLOv5系列 1、制作自己的数据集

; 3.保存为json格式

YOLOv5系列 1、制作自己的数据集
直接进行保存为.json格式即可(这个保存的名字名字是和照片的名字一样的),依照这样一直标记完。

三、json格式转换为txt格式

因为yolov5需要的是照片和txt格式,所以我们得到的json文件不能直接使用,需要先将json转换为txt格式,下面的代码就是将 json转为txt

import json
import os

name2id = {'Mask': 0,}

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

def decode_json(json_floder_path,json_name):

    txt_name = 'E:\\BaiduNetdiskDownload\\PyTorch-YOLOv3\\data\\custom\\labels\\' + json_name[0:-5] + '.txt'
    txt_file = open(txt_name, 'w')

    json_path = os.path.join(json_floder_path, json_name)
    data = json.load(open(json_path, 'r', encoding='gb2312'))

    img_w = data['imageWidth']
    img_h = data['imageHeight']

    for i in data['shapes']:

        label_name = i['label']
        if (i['shape_type'] == 'rectangle'):

            x1 = int(i['points'][0][0])
            y1 = int(i['points'][0][1])
            x2 = int(i['points'][1][0])
            y2 = int(i['points'][1][1])

            bb = (x1,y1,x2,y2)
            bbox = convert((img_w,img_h),bb)
            txt_file.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n')

if __name__ == "__main__":

    json_floder_path = 'E:\\BaiduNetdiskDownload\\PyTorch-YOLOv3\\data\\custom\\json\\'
    json_names = os.listdir(json_floder_path)
    for json_name in json_names:
        decode_json(json_floder_path,json_name)

这边我们就已经得到了yolov5所需要的txt格式的标签文件。

四、建立自己的Yolov5数据集

我这边建立的数据集文件夹的位置如下:

--MaskDataSet
    --train
        --images
        --labels
    --test
        --images
        --labels
    --valid
        --images
        --labels
    --data.yaml
--yolov5源码位置

其中images放入图片,labels则放入相应的txt标签文件;data.yaml文件内容如下所示:

train: ../MaskDataSet/train/images
test: ../MaskDataSet/test/images
val: ../MaskDataSet/valid/images

nc: 1
names: ['Mask',]

最后,数据集建立成功!下一步,可以开始训练我们标记的数据集啦~

Original: https://blog.csdn.net/fjlaym/article/details/123992962
Author: 冯璆鸣
Title: YOLOv5系列 1、制作自己的数据集

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/615017/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球