生活随笔
收集整理的這篇文章主要介紹了
Yolov5 使用精灵标注助手制作数据集
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
精靈標(biāo)記助手下載:http://www.jinglingbiaozhu.com/
首先點(diǎn)擊菜單文件->新建,然后選擇位置標(biāo)注,選擇圖片文件夾,點(diǎn)擊創(chuàng)建按鈕,軟件會自動加載文件夾下的圖片(png,jpg,gif)并創(chuàng)建一個項(xiàng)目。
注意:每次標(biāo)記完一張圖后,要對圖片進(jìn)行保存,通過快捷鍵 ctrl + s 或者點(diǎn)擊圖片下方的 ?,
當(dāng)左下角彈出 √保存成功,則證明該張圖片的標(biāo)記已經(jīng)保存,如果標(biāo)記的圖片沒有進(jìn)行保存,在切換圖片的過程中,前一張的圖片標(biāo)記框會自動消失,此時需要對圖片重新標(biāo)記。
快捷鍵
- R:矩形框
- ←:前一張圖片
- →:后一張圖片
- 空格:移動
- 放大縮小圖片:ctrl + 鼠標(biāo)滾輪
- 保存標(biāo)記:ctrl + s
所有的數(shù)據(jù)集標(biāo)注完成后,將標(biāo)注的信息導(dǎo)出。
注:選擇導(dǎo)出格式時候必須選擇pascal—voc導(dǎo)出XML直接選擇XML會在后面無法讀取到標(biāo)注
的信息
導(dǎo)出后,會在目錄中生成一個 outputs 文件夾,導(dǎo)出的文件就是pascal-voc的xml,效果如下:
輸出的標(biāo)注文件XML保存在Annotations中
數(shù)據(jù)集標(biāo)記好后,將原始圖片數(shù)據(jù)集放到images文件夾中,如圖所示
以下操作是對導(dǎo)出的XML文件和原圖片進(jìn)行解析。
makeTxt.py
創(chuàng)建 makeTxt.py 文件
makeTxt.py主要是將數(shù)據(jù)集分類成訓(xùn)練數(shù)據(jù)集和測試數(shù)據(jù)集,默認(rèn)train,val,test按照8:1:1
的比例進(jìn)行隨機(jī)分類。
import os
import randomtrainval_percent
= 0.9
train_percent
= 0.9
xmlfilepath
= './datasets/Annotations'
txtsavepath
= './datasets/images'
tmage_sets_path
= './datasets/ImageSets'
total_xml
= os
.listdir
(xmlfilepath
)num
= len(total_xml
)
list = range(num
)
tv
= int(num
* trainval_percent
)
tr
= int(tv
* train_percent
)
trainval
= random
.sample
(list, tv
)
train
= random
.sample
(trainval
, tr
)
if not os
.path
.exists
(tmage_sets_path
):os
.makedirs
(tmage_sets_path
)ftrainval
= open('datasets/ImageSets/trainval.txt', 'w')
ftest
= open('datasets/ImageSets/test.txt', 'w')
ftrain
= open('datasets/ImageSets/train.txt', 'w')
fval
= open('datasets/ImageSets/val.txt', 'w')for i
in list:name
= total_xml
[i
][:-4] + '\n'if i
in trainval
:ftrainval
.write
(name
)if i
in train
:ftrain
.write
(name
)else:fval
.write
(name
)else:ftest
.write
(name
)ftrainval
.close
()
ftrain
.close
()
fval
.close
()
ftest
.close
()
voc_label.py
創(chuàng)建 voc_label.py 文件
代碼如下:
classes=[……] 中填入的一定要是自己在數(shù)據(jù)集中所標(biāo)注的類別名稱,標(biāo)記了幾個類別就填寫幾個類別名,填寫錯誤的話會造成讀取不出xml文件里的標(biāo)注信息。
import xml
.etree
.ElementTree
as ET
import pickle
import os
from os
import listdir
, getcwd
from os
.path
import joinsets
= ['train', 'test', 'val']
classes
= ['杯子', '鍵盤', '鼠標(biāo)', '手機(jī)']
def convert(size
, box
): dw
= 1. / size
[0] dh
= 1. / size
[1] x
= (box
[0] + box
[1]) / 2.0 y
= (box
[2] + box
[3]) / 2.0 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
):'''將對應(yīng)文件名的xml文件轉(zhuǎn)化為label文件,xml文件包含了對應(yīng)的bunding框以及圖片長款大小等信息,通過對其解析,然后進(jìn)行歸一化最終讀到label文件中去,也就是說一張圖片文件對應(yīng)一個xml文件,然后通過解析和歸一化,能夠?qū)?yīng)的信息保存到唯一一個label文件中去labal文件中的格式:calss x y w h 同時,一張圖片對應(yīng)的類別有多個,所以對應(yīng)的bunding的信息也有多個'''in_file
= open('datasets/Annotations/%s.xml' % (image_id
), encoding
='utf-8')out_file
= open('datasets/labels/%s.txt' % (image_id
), 'w', encoding
='utf-8')tree
= ET
.parse
(in_file
)root
= tree
.getroot
()size
= root
.find
('size')if size
!= None:w
= int(size
.find
('width').text
)h
= int(size
.find
('height').text
)for obj
in root
.iter('object'):difficult
= obj
.find
('difficult').textcls
= obj
.find
('name').text
if cls
not in classes
or int(difficult
) == 1:continuecls_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
))print(image_id
, cls
, b
)bb
= convert
((w
, h
), b
)out_file
.write
(str(cls_id
) + " " + " ".join
([str(a
) for a
in bb
]) + '\n')
wd
= getcwd
()
print(wd
)for image_set
in sets
:'''對所有的文件數(shù)據(jù)集進(jìn)行遍歷做了兩個工作:1.將所有圖片文件都遍歷一遍,并且將其所有的全路徑都寫在對應(yīng)的txt文件中去,方便定位2.同時對所有的圖片文件進(jìn)行解析和轉(zhuǎn)化,將其對應(yīng)的bundingbox 以及類別的信息全部解析寫到label 文件中去最后再通過直接讀取文件,就能找到對應(yīng)的label 信息'''if not os
.path
.exists
('datasets/labels/'):os
.makedirs
('datasets/labels/')image_ids
= open('datasets/ImageSets/%s.txt' % (image_set
)).read
().strip
().split
()list_file
= open('datasets/%s.txt' % (image_set
), 'w')for image_id
in image_ids
:list_file
.write
('datasets/images/%s.jpg\n' % (image_id
))convert_annotation
(image_id
)list_file
.close
()
- 分別運(yùn)行makeTxt.py和voc_label.py。
- makeTxt.py主要是將數(shù)據(jù)集分類成訓(xùn)練數(shù)據(jù)集和測試數(shù)據(jù)集,默認(rèn)train,val,test按照8:1:1
的比例進(jìn)行隨機(jī)分類,運(yùn)行后ImagesSets文件夾中會出現(xiàn)四個文件,主要是生成的訓(xùn)練數(shù)據(jù)集和測
試數(shù)據(jù)集的圖片名稱,同時data目錄下也會出現(xiàn)這四個文件,內(nèi)容是訓(xùn)練數(shù)據(jù)集和測試數(shù)據(jù)集的圖
片路徑。
labels文件夾下 txt文件的內(nèi)容如下:
根據(jù)數(shù)據(jù)集會寫一些對圖片文件和txt文件處理的腳本,
例如:以下是在標(biāo)記數(shù)據(jù)集的過程中并沒有對一些數(shù)據(jù)集進(jìn)行標(biāo)記然后,將已經(jīng)標(biāo)記的數(shù)據(jù)集進(jìn)行分開
存放在另一個文件夾中。
import os
, shutil
'''匹配訓(xùn)練的圖片 和 標(biāo)記 文件名字是否對應(yīng)
把對應(yīng)的名字復(fù)制到其它文件中
'''
def mycopyfile(srcfile
, dstfile
): if not os
.path
.isfile
(srcfile
):print("%s not exist!" % (srcfile
))else:fpath
= os
.path
.dirname
(dstfile
) if not os
.path
.exists
(fpath
): os
.makedirs
(fpath
) shutil
.copyfile
(srcfile
, dstfile
) print("copy %s -> %s" % (srcfile
, os
.path
.join
(fpath
, dstfile
)))def operation(images_path
, txt_path
, copy_images_path
, copy_txt_path
):os
.chdir
(images_path
) print("開始復(fù)制....")cout
= 0for images_file
in os
.listdir
(images_path
):images_name
= os
.path
.splitext
(images_file
)[0] images_suffix
= os
.path
.splitext
(images_file
)[1] for txt_file
in os
.listdir
(txt_path
):txt_name
= os
.path
.splitext
(txt_file
)[0] txt_suffix
= os
.path
.splitext
(txt_file
)[1] if images_name
== txt_name
:mycopyfile
(images_path
+ images_name
+ images_suffix
, copy_images_path
+ images_name
+ images_suffix
)mycopyfile
(txt_path
+ images_name
+ txt_suffix
, copy_txt_path
+ images_name
+ txt_suffix
)print("images文件復(fù)制%s個,txt文件復(fù)制%s個,總共復(fù)制%s個文件" % (len(os
.listdir
(copy_images_path
)), len(os
.listdir
(copy_txt_path
)), len(os
.listdir
(copy_images_path
)) + len(os
.listdir
(copy_txt_path
))))if __name__
== '__main__':images_path
= 'C:\\Users\\vvcat\\Desktop\\yolov5\\total\\images\\'txt_path
= 'C:\\Users\\vvcat\\Desktop\\yolov5\\total\\labels\\'copy_images_path
= 'C:\\Users\\vvcat\\Desktop\\yolov5\\neaten\\images\\'copy_txt_path
= 'C:\\Users\\vvcat\\Desktop\\yolov5\\neaten\\labels\\'operation
(images_path
, txt_path
, copy_images_path
, copy_txt_path
)
總結(jié)
以上是生活随笔為你收集整理的Yolov5 使用精灵标注助手制作数据集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。