1. labelImg安裝
1.工具下載下傳:label工具的github下載下傳位址:https://github.com/tzutalin/labelImg/tree/bf8cc1d5756184b9af8e412929efdaa6ae0ba4f1
在此感謝大神的貢獻,工具真的很好用,一目了然。
2. 如果電腦還沒有安裝anaconda,則先安裝anaconda軟體。然後進入到下載下傳好的labelImg目錄下,輸入cmd,進入控制台。
3.安裝qt,我安裝的是QT5,指令行輸入
conda install pyqt=5
linux下qt5的安裝:同樣進入到labelImg目錄下,輸入以下指令:
sudo apt-get install pyqt5-dev-tools
等待安裝完成後在輸入:“make qt5py3” 若出現“pyrcc5 -o libs/resources.py resources.qrc”則表示安裝成功。
4.等待安裝完後,在指令行輸入
pyrcc5 -o libs/resources.py resources.qrc
5.再輸入
python labelImg.py
即打開标注工具。
标注工具使用就不作說明了,一目了然,設定圖檔路徑和标注好的xml檔案路徑即可,圖檔标注好後會自動在所設定的目錄下生成對應的xml檔案,且名稱與圖檔相同。圖像路徑和标注儲存的xml路徑中最好不要有中文名,否則後面生成訓練要用的txt檔案時可能會不識别。
2. VOC2007格式資料制作
1. 在工程目錄中建立檔案夾命名為“VOC2007”;
2.網上下載下傳一個voc2007格式的資料集,将該資料集下的Annotations以及JPEGImages清空,将自己标注好的圖檔放入JPEGImages,生成的xml檔案放入Annotations,将SegmentationClass和SegmentationObject檔案夾也清空,ImageSets下面三個子檔案夾的内容也清空,但保留三個檔案夾,名字不要改動。
(提供voc2007資料格式的csdn資源https://download.csdn.net/download/feng1479580895/10320841)
使用以下代碼生成Main中的四個檔案
import os
import random
trainval_percent = 0.7
train_percent = 0.5
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
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)
ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/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()
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL1ITOzAzMyATMwMjMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
這一步的詳細步驟也可以參考這篇部落格:https://blog.csdn.net/jx232515/article/details/78680724
這樣,VOC2007的資料格式制作完了;
2.1. 位置文本資訊生成腳本
直接上腳本代碼:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
classes = ["car","dog", "person"]#類型自己設定,不同類型用逗号隔開
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
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):
in_file = open('labelimg儲存的xml路徑/%s.xml'%(image_id), "r", encoding="utf-8")#讀取标注資料,并支援中文字元
out_file = open('txt文本輸出路徑/%s.txt'%(image_id), 'w')#輸出的标注訓練資料文本檔案
tree=ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_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))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()#擷取目前執行檔案夾的路徑
if not os.path.exists('輸出txt文本檔案夾路徑'):#判斷是否存在輸出檔案夾
os.makedirs('輸出txt文本檔案夾路徑')#不存在則建立輸出檔案夾
image_ids = open('voc資料路徑\ImageSets\Main/train.txt').read().strip().split()#分離文本中的imageID
list_file = open('2007_train.txt','w')
for image_id in image_ids:
list_file.write('voc資料路徑\images/%s.jpg\n'%(image_id))#将路徑寫入到文本檔案中
convert_annotation(image_id)
list_file.close()
附:将voc2007資料格式轉換為tensorflow資料格式
源碼github位址:https://github.com/balancap/SSD-Tensorflow
1. 在工程的根目錄下建立名稱為“tfrecords_”的檔案夾,帶下劃線的;
2. 打開指令視窗,根據balancap作者給出的tf_convert_data.py檔案的使用方式,輸入如下指令:
第一個參數是python,第二個參數是tf_convert_data.py檔案的路徑,第三個參數是“
--dataset_name=pascalvoc_2007
”
,第四個是你制作好的VOC2007格式的資料的路徑,第五個是“--output_name=voc_2007_train”,第六個數輸出的tfrecord格式的輸出目錄。
(注:目錄設定時,最後不要忘記加“\”)
若是成功運作,則會出現如下界面