yolov5半自動化標注圖片,生成圖片資料的xml,標簽格式為VOC格式
1.訓練自己的yolov5 模型
2.執行 yolov5 的detect.py 檔案,(里面的權重檔案路徑和jpg路徑需要改為自己的路徑)后面加 --save-txt ,命令如下:
sudo python detect.py --save-txt
3.txt檔案轉換xml. 轉化的代碼如下,
# 將 txt 標簽 檔案轉換為 xml 標簽檔案, 修改dict中的類,以及xml txt 和jpg 路徑,
from xml.dom.minidom import Document
import os
import cv2
def makexml(txtPath,xmlPath,picPath): #讀取txt路徑,xml保存路徑,資料集圖片所在路徑
dict = {'0': " person", #字典對型別進行轉換,自己的標簽的類,
'1': "dog ",
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation") # 創建annotation標簽
xmlBuilder.appendChild(annotation)
txtFile=open(txtPath+name)
txtList = txtFile.readlines()
img = cv2.imread(picPath+name[0:-4]+".jpg")
Pheight,Pwidth,Pdepth=img.shape
for i in txtList:
oneline = i.strip().split(" ")
folder = xmlBuilder.createElement("folder")#folder標簽
folderContent = xmlBuilder.createTextNode("VOC2007")
folder.appendChild(folderContent)
annotation.appendChild(folder)
filename = xmlBuilder.createElement("filename")#filename標簽
filenameContent = xmlBuilder.createTextNode(name[0:-4]+".png")
filename.appendChild(filenameContent)
annotation.appendChild(filename)
size = xmlBuilder.createElement("size") # size標簽
width = xmlBuilder.createElement("width") # size子標簽width
widthContent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthContent)
size.appendChild(width)
height = xmlBuilder.createElement("height") # size子標簽height
heightContent = xmlBuilder.createTextNode(str(Pheight))
height.appendChild(heightContent)
size.appendChild(height)
depth = xmlBuilder.createElement("depth") # size子標簽depth
depthContent = xmlBuilder.createTextNode(str(Pdepth))
depth.appendChild(depthContent)
size.appendChild(depth)
annotation.appendChild(size)
object = xmlBuilder.createElement("object")
picname = xmlBuilder.createElement("name")
nameContent = xmlBuilder.createTextNode(dict[oneline[0]])
picname.appendChild(nameContent)
object.appendChild(picname)
pose = xmlBuilder.createElement("pose")
poseContent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(poseContent)
object.appendChild(pose)
truncated = xmlBuilder.createElement("truncated")
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated)
difficult = xmlBuilder.createElement("difficult")
difficultContent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultContent)
object.appendChild(difficult)
bndbox = xmlBuilder.createElement("bndbox")
xmin = xmlBuilder.createElement("xmin")
mathData=int(((float(oneline[1]))*Pwidth+1)-(float(oneline[3]))*0.5*Pwidth)
xminContent = xmlBuilder.createTextNode(str(mathData))
xmin.appendChild(xminContent)
bndbox.appendChild(xmin)
ymin = xmlBuilder.createElement("ymin")
mathData = int(((float(oneline[2]))*Pheight+1)-(float(oneline[4]))*0.5*Pheight)
yminContent = xmlBuilder.createTextNode(str(mathData))
ymin.appendChild(yminContent)
bndbox.appendChild(ymin)
xmax = xmlBuilder.createElement("xmax")
mathData = int(((float(oneline[1]))*Pwidth+1)+(float(oneline[3]))*0.5*Pwidth)
xmaxContent = xmlBuilder.createTextNode(str(mathData))
xmax.appendChild(xmaxContent)
bndbox.appendChild(xmax)
ymax = xmlBuilder.createElement("ymax")
mathData = int(((float(oneline[2]))*Pheight+1)+(float(oneline[4]))*0.5*Pheight)
ymaxContent = xmlBuilder.createTextNode(str(mathData))
ymax.appendChild(ymaxContent)
bndbox.appendChild(ymax)
object.appendChild(bndbox)
annotation.appendChild(object)
f = open(xmlPath+name[0:-4]+".xml", 'w')
xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.close()
makexml("/home/wenzen/project/V5/yolov5-fall/runs_/labels/", #txt檔案夾
"/home/wenzen/project/V5/yolov5-fall/runs_/xml_02/", #xml檔案夾
"/home/wenzen/下載/fall/all/") #圖片資料檔案夾
有問題的歡迎流言哈 ,^^ ,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279591.html
標籤:python
上一篇:面向物件編程之繼承、多型、封裝、抽象類、介面、包-下(實戰圖書管理系統)
下一篇:python3 + opencv +pyzbar實時檢測二維碼 / 定位二維碼,并繪制出二維碼的框和提取二維碼內容
