日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Yolov5 使用精灵标注助手制作数据集

發(fā)布時間:2025/5/22 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Yolov5 使用精灵标注助手制作数据集 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

精靈標(biāo)記助手下載:http://www.jinglingbiaozhu.com/

首先點(diǎn)擊菜單文件->新建,然后選擇位置標(biāo)注,選擇圖片文件夾,點(diǎn)擊創(chuàng)建按鈕,軟件會自動加載文件夾下的圖片(png,jpg,gif)并創(chuàng)建一個項(xiàng)目。

  • 項(xiàng)目名稱:根據(jù)自己的需求命名

  • 圖片文件夾:圖片文件所在電腦磁盤的路徑

  • 分類值:根據(jù)對圖片劃分的類別,對不同類的框命名

    可以選擇 曲線框,多邊形框,矩形框等對圖片進(jìn)行標(biāo)注。

    注:所選框不能超出圖片大小,如圖下

    為了有更好的識別效果,上面的圖片應(yīng)該進(jìn)行如下標(biāo)記:

注意:每次標(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' # 數(shù)據(jù)集位置 txtsavepath = './datasets/images' # 圖片位置 tmage_sets_path = './datasets/ImageSets' # 將數(shù)據(jù)集分為 訓(xùn)練數(shù)據(jù)集和測試數(shù)據(jù)集進(jìn)行存放的位置 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)# 先找ImageSets文件夾如果不存在則創(chuàng)建 if not os.path.exists(tmage_sets_path):os.makedirs(tmage_sets_path)ftrainval = open('datasets/ImageSets/trainval.txt', 'w') # 以只寫方式打開文件。如果文件存在會被覆蓋。如果文件不存在,創(chuàng)建新文件 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)注信息。

# xml解析包 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ī)'] #填入剛剛標(biāo)記數(shù)據(jù)集的類別名稱# 進(jìn)行歸一化操作 def convert(size, box): # size:(原圖w,原圖h) , box:(xmin,xmax,ymin,ymax)dw = 1. / size[0] # 1/wdh = 1. / size[1] # 1/hx = (box[0] + box[1]) / 2.0 # 物體在圖中的中心點(diǎn)x坐標(biāo)y = (box[2] + box[3]) / 2.0 # 物體在圖中的中心點(diǎn)y坐標(biāo)w = box[1] - box[0] # 物體實(shí)際像素寬度h = box[3] - box[2] # 物體實(shí)際像素高度x = x * dw # 物體中心點(diǎn)x的坐標(biāo)比(相當(dāng)于 x/原圖w)w = w * dw # 物體寬度的寬度比(相當(dāng)于 w/原圖w)y = y * dh # 物體中心點(diǎn)y的坐標(biāo)比(相當(dāng)于 y/原圖h)h = h * dh # 物體寬度的寬度比(相當(dāng)于 h/原圖h)return (x, y, w, h) # 返回 相對于原圖的物體中心點(diǎn)的x坐標(biāo)比,y坐標(biāo)比,寬度比,高度比,取值范圍[0-1]# year ='2012', 對應(yīng)圖片的id(文件名) 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的信息也有多個'''# 對應(yīng)的通過year 找到相應(yīng)的文件夾,并且打開相應(yīng)image_id的xml文件,其對應(yīng)bund文件in_file = open('datasets/Annotations/%s.xml' % (image_id), encoding='utf-8')# 準(zhǔn)備在對應(yīng)的image_id 中寫入對應(yīng)的label,分別為# <object-class> <x> <y> <width> <height>out_file = open('datasets/labels/%s.txt' % (image_id), 'w', encoding='utf-8')# 解析xml文件tree = ET.parse(in_file)# 獲得對應(yīng)的鍵值對root = tree.getroot()# 獲得圖片的尺寸大小size = root.find('size')# 如果xml內(nèi)的標(biāo)記為空,增加判斷條件if size != None:# 獲得寬w = int(size.find('width').text)# 獲得高h = int(size.find('height').text)# 遍歷目標(biāo)objfor obj in root.iter('object'):# 獲得difficult ??difficult = obj.find('difficult').text# 獲得類別 =string 類型cls = obj.find('name').text# 如果類別不是對應(yīng)在我們預(yù)定好的class文件中,或difficult==1則跳過if cls not in classes or int(difficult) == 1:continue# 通過類別名稱找到idcls_id = classes.index(cls)# 找到bndbox 對象xmlbox = obj.find('bndbox')# 獲取對應(yīng)的bndbox的數(shù)組 = ['xmin','xmax','ymin','ymax']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)# 帶入進(jìn)行歸一化操作# w = 寬, h = 高, b= bndbox的數(shù)組 = ['xmin','xmax','ymin','ymax']bb = convert((w, h), b)# bb 對應(yīng)的是歸一化后的(x,y,w,h)# 生成 calss x y w h 在label文件中out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') # 返回當(dāng)前工作目錄 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 信息'''# 先找labels文件夾如果不存在則創(chuàng)建if not os.path.exists('datasets/labels/'):os.makedirs('datasets/labels/')# 讀取在ImageSets/Main 中的train、test..等文件的內(nèi)容# 包含對應(yīng)的文件名稱image_ids = open('datasets/ImageSets/%s.txt' % (image_set)).read().strip().split()# 打開對應(yīng)的2012_train.txt 文件對其進(jìn)行寫入準(zhǔn)備list_file = open('datasets/%s.txt' % (image_set), 'w')# 將對應(yīng)的文件_id以及全路徑寫進(jìn)去并換行for image_id in image_ids:list_file.write('datasets/images/%s.jpg\n' % (image_id))# 調(diào)用 year = 年份 image_id = 對應(yīng)的文件名_idconvert_annotation(image_id)# 關(guān)閉文件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): # 復(fù)制文件函數(shù)if not os.path.isfile(srcfile):print("%s not exist!" % (srcfile))else:fpath = os.path.dirname(dstfile) # 獲取文件路徑if not os.path.exists(fpath): # 如果沒有復(fù)制的文件目錄就創(chuàng)建復(fù)制到那里的文件目錄os.makedirs(fpath) # 沒有就創(chuàng)建路徑shutil.copyfile(srcfile, dstfile) # 復(fù)制文件到默認(rèn)路徑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) # 用于改變當(dāng)前工作目錄到指定的路徑。 相當(dāng)于 cd #當(dāng)操作的文件不在當(dāng)前目錄時必須先進(jìn)入目錄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文件路徑txt_path = 'C:\\Users\\vvcat\\Desktop\\yolov5\\total\\labels\\'# 圖片復(fù)制路徑copy_images_path = 'C:\\Users\\vvcat\\Desktop\\yolov5\\neaten\\images\\'# txt文件復(fù)制路徑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)容還不錯,歡迎將生活随笔推薦給好友。