赞
踩
进入虚拟环境再打开labelimg即可
yolo模式的情况
1.进入labelimg文件夹
2.将该文件修改为你所需的标签(记住此处顺序!三者从上至下分别为0 1 2)
3.打开labelimg后,选择图片文件夹,及保存文件夹,选择yolo模式,打开自动保存
图片文件夹和保存文件夹,分别命名为images,labels
4.打开labels文件夹,新建一classes.txt文件,填写标签信息,如之前顺序一样
(从上至下分别为0 1 2)
5.开始标注,w标注,标注完第一张后,检查labels文件夹内对应文件
这里开头的0 1 2即代表标签信息,后面为标签框坐标
若要修改标签,在txt文件内修改!不要在labelimg内修改,很容易出错
6.在yolo训练时,yaml文件内标签名也要与上述两个标签名顺序一致,否则训练结果会混乱
7.最后还是建议使用voc模式训练
xml转txt
记得修改类别及路径,路径使用绝对路径
参考链接:python批量实现labelImg标注的 xml格式数据转换成 txt格式保存_labelimg保存txt_深度学习菜鸟的博客-CSDN博客
- # -*- coding: utf-8 -*-
- import os
- import xml.etree.ElementTree as ET
-
- dirpath = 'raw_data/xml_raw' # 原来存放xml文件的目录
- newdir = 'raw_data/txt' # 修改label后形成的txt目录
-
- if not os.path.exists(newdir):
- os.makedirs(newdir)
-
- dict_info = {'1': 0, '2': 1, '0': 2} # 有几个 属性 填写几个label names
-
- for fp in os.listdir(dirpath):
- if fp.endswith('.xml'):
- root = ET.parse(os.path.join(dirpath, fp)).getroot()
-
- xmin, ymin, xmax, ymax = 0, 0, 0, 0
- sz = root.find('size')
- width = float(sz[0].text)
- height = float(sz[1].text)
- filename = root.find('filename').text
- for child in root.findall('object'): # 找到图片中的所有框
-
- sub = child.find('bndbox') # 找到框的标注值并进行读取
- label = child.find('name').text
- label_ = dict_info.get(label)
- if label_:
- label_ = label_
- else:
- label_ = 0
- xmin = float(sub[0].text)
- ymin = float(sub[1].text)
- xmax = float(sub[2].text)
- ymax = float(sub[3].text)
- try: # 转换成yolov3的标签格式,需要归一化到(0-1)的范围内
- x_center = (xmin + xmax) / (2 * width)
- x_center = '%.6f' % x_center
- y_center = (ymin + ymax) / (2 * height)
- y_center = '%.6f' % y_center
- w = (xmax - xmin) / width
- w = '%.6f' % w
- h = (ymax - ymin) / height
- h = '%.6f' % h
- except ZeroDivisionError:
- print(filename, '的 width有问题')
- with open(os.path.join(newdir, fp.split('.xml')[0] + '.txt'), 'a+') as f:
- f.write(' '.join([str(label_), str(x_center), str(y_center), str(w), str(h) + '\n']))
- print('ok')
txt转xml
记得修改类别
- from xml.dom.minidom import Document
- import os
- import cv2
-
-
- def makexml(txtPath, xmlPath, picPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
- """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
- 在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
- """
- dic = {'0': "C_1", # 创建字典用来对类型进行转换
- '1': "B_5", # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
- '2': "A_10",
- }
- 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
-
- folder = xmlBuilder.createElement("folder") # folder标签
- foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
- folder.appendChild(foldercontent)
- annotation.appendChild(folder) # folder标签结束
-
- filename = xmlBuilder.createElement("filename") # filename标签
- filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
- filename.appendChild(filenamecontent)
- annotation.appendChild(filename) # filename标签结束
-
- size = xmlBuilder.createElement("size") # size标签
- width = xmlBuilder.createElement("width") # size子标签width
- widthcontent = xmlBuilder.createTextNode(str(Pwidth))
- width.appendChild(widthcontent)
- size.appendChild(width) # size子标签width结束
-
- height = xmlBuilder.createElement("height") # size子标签height
- heightcontent = xmlBuilder.createTextNode(str(Pheight))
- height.appendChild(heightcontent)
- size.appendChild(height) # size子标签height结束
-
- depth = xmlBuilder.createElement("depth") # size子标签depth
- depthcontent = xmlBuilder.createTextNode(str(Pdepth))
- depth.appendChild(depthcontent)
- size.appendChild(depth) # size子标签depth结束
-
- annotation.appendChild(size) # size标签结束
-
- for j in txtList:
- oneline = j.strip().split(" ")
- object = xmlBuilder.createElement("object") # object 标签
- picname = xmlBuilder.createElement("name") # name标签
- namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
- picname.appendChild(namecontent)
- object.appendChild(picname) # name标签结束
-
- pose = xmlBuilder.createElement("pose") # pose标签
- posecontent = xmlBuilder.createTextNode("Unspecified")
- pose.appendChild(posecontent)
- object.appendChild(pose) # pose标签结束
-
- truncated = xmlBuilder.createElement("truncated") # truncated标签
- truncatedContent = xmlBuilder.createTextNode("0")
- truncated.appendChild(truncatedContent)
- object.appendChild(truncated) # truncated标签结束
-
- difficult = xmlBuilder.createElement("difficult") # difficult标签
- difficultcontent = xmlBuilder.createTextNode("0")
- difficult.appendChild(difficultcontent)
- object.appendChild(difficult) # difficult标签结束
-
- bndbox = xmlBuilder.createElement("bndbox") # bndbox标签
- xmin = xmlBuilder.createElement("xmin") # 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) # xmin标签结束
-
- ymin = xmlBuilder.createElement("ymin") # 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) # ymin标签结束
-
- xmax = xmlBuilder.createElement("xmax") # 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) # xmax标签结束
-
- ymax = xmlBuilder.createElement("ymax") # 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) # ymax标签结束
-
- object.appendChild(bndbox) # bndbox标签结束
-
- annotation.appendChild(object) # object标签结束
-
- f = open(xmlPath + name[0:-4] + ".xml", 'w')
- xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
- f.close()
-
-
- makexml("D:/A_coin/yolov5-master/CoinData/labels/", "D:/A_coin/yolov5-master/CoinData/xml/",
- "D:/A_coin/yolov5-master/CoinData/Images/") # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
-
-
- # if __name__ == "__main__":
- # picPath = "D:/A_coin/yolov5-master/CoinData/Images/" # 图片所在文件夹路径,后面的\\一定要带上
- # txtPath = "D:/A_coin/yolov5-master/CoinData/labels/" # txt所在文件夹路径,后面的\\一定要带上
- # xmlPath = "D:/A_coin/yolov5-master/CoinData/xml/" # xml文件保存路径,后面的\\一定要带上
- # makexml(picPath, txtPath, xmlPath)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。