作用:
????????? 1. 讀取圖片和標注后的xml文件,將標注信息寫到txt文件中。生成的txt文件在txt文件夾下面,可以直接用于yolo3等目標檢測算法的訓練。
??????????? 2. txt文件包含圖片的路徑,標注框的位置和大小,以及標注的類別。
??????????? 3. 分割標注的圖片,可以用于行人屬性識別。分割的圖片保存在segmentation文件夾下。
數(shù)據(jù)集準備工作:
?????????? 1. 需要將數(shù)據(jù)集放在dataset路徑下。
??????????? 2. Annotations存放的是xml文件,JPEGImages存放的是圖片。
修改:需要針對需要進行適當?shù)男薷?。下面進行說明:
第一:
class_tab={
"human":1? ##這個是類別,如果有多個類別,可以添加多條
}
第二:
params={"xml_prefix":"./dataset/Annotations/", ##這個是xml源文件的路徑前綴,注意末尾要有“/”
? ? ? ? "img_prefix":"./dataset/JPEGImages/",##這個是圖片源文件的路徑前綴,注意末尾要有“/”
? ? ? ? "seg_prefix":"./segmentation/",##這個是分割后圖片存放的位置前綴,注意末尾要有“/”
? ? ? }
第三:
f = open('./txt/train.txt','a')##這個是生成的txt文件的路徑。
?????????????????????????????? 下面附上所有源碼
第一: main.py
import sys
#sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import cv2
import os
from readxml import *
class_tab={
"humanimport sys
#sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import cv2
import os
from readxml import *
class_tab={
"human":1
}
params={"xml_prefix":"./dataset/Annotations/",
? ? ? ? "img_prefix":"./dataset/JPEGImages/",
? ? ? ? "seg_prefix":"./segmentation/",
? ? ? }
def write_line(f, img_path, boxes):
? ? f.write("%s "%(img_path))
? ? for box in boxes:
? ? ? f.write(str(box['cls'])+",")
? ? ? f.write(str(box['xmin']+","))
? ? ? f.write(str(box['ymin']+","))
? ? ? f.write(str(box['xmax']+","))
? ? ? f.write(str(box['ymax']))
? ? ? f.write(" ")
? ? f.write("\n")
? ? f.flush()
def segment(img_path, imgname, boxes):
? ? img=cv2.imread(img_path, cv2.IMREAD_COLOR)
? ? cnt=0
? ? for box in boxes:
? ? ? ? ? seg=img[int(box['ymin']):int(box["ymax"]), int(box["xmin"]):int(box["xmax"]),:]
? ? ? ? ? cv2.imwrite(params["seg_prefix"]+imgname+"_"+str(cnt)+".jpg", seg)
? ? ? ? ? cnt=cnt+1
def main():
? ? f = open('./txt/train.txt','a')
? ? img_list=os.listdir(params["img_prefix"])
? ? for il in img_list:
? ? ? ? img_path=params["img_prefix"]+il
? ? ? ? tokens=il.split(".")
? ? ? ? xml_path=params["xml_prefix"]+tokens[0]+".xml"
? ? ? ? if os.path.isfile(img_path)==False:
? ? ? ? ? ? raise ValueError("%s does not exits!"%img_path)
? ? ? ? else:
? ? ? ? ? ? print("reading:", img_path)
? ? ? ? ? ? boxes=xml_reader(xml_path, class_tab)
? ? ? ? ? ? write_line(f,img_path,boxes)
? ? ? ? ? ? segment(img_path, tokens[0], boxes)
? ? f.close()
if __name__ == '__main__':
? ? main()
":1
}
params={"xml_prefix":"./dataset/Annotations/",
? ? ? ? "img_prefix":"./dataset/JPEGImages/",
? ? ? ? "seg_prefix":"./segmentation/",
? ? ? }
def write_line(f, img_path, boxes):
? ? f.write("%s "%(img_path))
? ? for box in boxes:
? ? ? f.write(str(box['cls'])+",")
? ? ? f.write(str(box['xmin']+","))
? ? ? f.write(str(box['ymin']+","))
? ? ? f.write(str(box['xmax']+","))
? ? ? f.write(str(box['ymax']))
? ? ? f.write(" ")
? ? f.write("\n")
? ? f.flush()
def segment(img_path, imgname, boxes):
? ? img=cv2.imread(img_path, cv2.IMREAD_COLOR)
? ? cnt=0
? ? for box in boxes:
? ? ? ? ? seg=img[int(box['ymin']):int(box["ymax"]), int(box["xmin"]):int(box["xmax"]),:]
? ? ? ? ? cv2.imwrite(params["seg_prefix"]+imgname+"_"+str(cnt)+".jpg", seg)
? ? ? ? ? cnt=cnt+1
def main():
? ? f = open('./txt/train.txt','a')
? ? img_list=os.listdir(params["img_prefix"])
? ? for il in img_list:
? ? ? ? img_path=params["img_prefix"]+il
? ? ? ? tokens=il.split(".")
? ? ? ? xml_path=params["xml_prefix"]+tokens[0]+".xml"
? ? ? ? if os.path.isfile(img_path)==False:
? ? ? ? ? ? raise ValueError("%s does not exits!"%img_path)
? ? ? ? else:
? ? ? ? ? ? print("reading:", img_path)
? ? ? ? ? ? boxes=xml_reader(xml_path, class_tab)
? ? ? ? ? ? write_line(f,img_path,boxes)
? ? ? ? ? ? segment(img_path, tokens[0], boxes)
? ? f.close()
if __name__ == '__main__':
? ? main()
第二:readxml.py
from xml.dom.minidom import Document
import xml.etree.ElementTree as ET
def xml_reader(fname, cls_tab):###, classes_merge_list,classes_merged_name):
? ? in_file = open(fname)
? ? tree=ET.parse(in_file)
? ? root = tree.getroot()
? ? boxes=[]
? ? for item in root.iter('item'):
? ? ? ? cls = item.find('name').text
? ? ? ? if cls in cls_tab:
? ? ? ? ? ? bndbox = item.find('bndbox')
? ? ? ? ? ? b={
? ? ? ? ? ? ? ? "xmin": str(int(bndbox.find('xmin').text)),
? ? ? ? ? ? ? ? "ymin": str(int(bndbox.find('ymin').text)),
? ? ? ? ? ? ? ? "xmax": str(int(bndbox.find('xmax').text)),
? ? ? ? ? ? ? ? "ymax": str(int(bndbox.find('ymax').text)),
? ? ? ? ? ? ? ? "cls": cls_tab[cls]
? ? ? ? ? ? ? ? }
? ? ? ? ? ? boxes.append(b)
? ? ? ? else:
? ? ? ? ? ? continue
? ? return boxes
if __name__ == '__main__':
? cls_tab={"human":1}
? fname="/home/ta/Downloads/liqiming_label/dataset/Annotations/sa341.xml"
? box=xml_reader(fname, cls_tab)
? print(box)