当前位置:   article > 正文

关于labelimg使用的坑_labelimg只能标记第一张,标记第二张显示之前的标签

labelimg只能标记第一张,标记第二张显示之前的标签

1.闪退问题

进入虚拟环境再打开labelimg即可

2.标签乱序

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模式训练

 3.xml与txt互转

xml转txt

记得修改类别及路径,路径使用绝对路径

参考链接:python批量实现labelImg标注的 xml格式数据转换成 txt格式保存_labelimg保存txt_深度学习菜鸟的博客-CSDN博客

  1. # -*- coding: utf-8 -*-
  2. import os
  3. import xml.etree.ElementTree as ET
  4. dirpath = 'raw_data/xml_raw' # 原来存放xml文件的目录
  5. newdir = 'raw_data/txt' # 修改label后形成的txt目录
  6. if not os.path.exists(newdir):
  7. os.makedirs(newdir)
  8. dict_info = {'1': 0, '2': 1, '0': 2} # 有几个 属性 填写几个label names
  9. for fp in os.listdir(dirpath):
  10. if fp.endswith('.xml'):
  11. root = ET.parse(os.path.join(dirpath, fp)).getroot()
  12. xmin, ymin, xmax, ymax = 0, 0, 0, 0
  13. sz = root.find('size')
  14. width = float(sz[0].text)
  15. height = float(sz[1].text)
  16. filename = root.find('filename').text
  17. for child in root.findall('object'): # 找到图片中的所有框
  18. sub = child.find('bndbox') # 找到框的标注值并进行读取
  19. label = child.find('name').text
  20. label_ = dict_info.get(label)
  21. if label_:
  22. label_ = label_
  23. else:
  24. label_ = 0
  25. xmin = float(sub[0].text)
  26. ymin = float(sub[1].text)
  27. xmax = float(sub[2].text)
  28. ymax = float(sub[3].text)
  29. try: # 转换成yolov3的标签格式,需要归一化到(0-1)的范围内
  30. x_center = (xmin + xmax) / (2 * width)
  31. x_center = '%.6f' % x_center
  32. y_center = (ymin + ymax) / (2 * height)
  33. y_center = '%.6f' % y_center
  34. w = (xmax - xmin) / width
  35. w = '%.6f' % w
  36. h = (ymax - ymin) / height
  37. h = '%.6f' % h
  38. except ZeroDivisionError:
  39. print(filename, '的 width有问题')
  40. with open(os.path.join(newdir, fp.split('.xml')[0] + '.txt'), 'a+') as f:
  41. f.write(' '.join([str(label_), str(x_center), str(y_center), str(w), str(h) + '\n']))
  42. print('ok')

 txt转xml

记得修改类别

  1. from xml.dom.minidom import Document
  2. import os
  3. import cv2
  4. def makexml(txtPath, xmlPath, picPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
  5. """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
  6. 在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
  7. """
  8. dic = {'0': "C_1", # 创建字典用来对类型进行转换
  9. '1': "B_5", # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
  10. '2': "A_10",
  11. }
  12. files = os.listdir(txtPath)
  13. for i, name in enumerate(files):
  14. xmlBuilder = Document()
  15. annotation = xmlBuilder.createElement("annotation") # 创建annotation标签
  16. xmlBuilder.appendChild(annotation)
  17. txtFile = open(txtPath + name)
  18. txtList = txtFile.readlines()
  19. img = cv2.imread(picPath + name[0:-4] + ".jpg")
  20. Pheight, Pwidth, Pdepth = img.shape
  21. folder = xmlBuilder.createElement("folder") # folder标签
  22. foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
  23. folder.appendChild(foldercontent)
  24. annotation.appendChild(folder) # folder标签结束
  25. filename = xmlBuilder.createElement("filename") # filename标签
  26. filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
  27. filename.appendChild(filenamecontent)
  28. annotation.appendChild(filename) # filename标签结束
  29. size = xmlBuilder.createElement("size") # size标签
  30. width = xmlBuilder.createElement("width") # size子标签width
  31. widthcontent = xmlBuilder.createTextNode(str(Pwidth))
  32. width.appendChild(widthcontent)
  33. size.appendChild(width) # size子标签width结束
  34. height = xmlBuilder.createElement("height") # size子标签height
  35. heightcontent = xmlBuilder.createTextNode(str(Pheight))
  36. height.appendChild(heightcontent)
  37. size.appendChild(height) # size子标签height结束
  38. depth = xmlBuilder.createElement("depth") # size子标签depth
  39. depthcontent = xmlBuilder.createTextNode(str(Pdepth))
  40. depth.appendChild(depthcontent)
  41. size.appendChild(depth) # size子标签depth结束
  42. annotation.appendChild(size) # size标签结束
  43. for j in txtList:
  44. oneline = j.strip().split(" ")
  45. object = xmlBuilder.createElement("object") # object 标签
  46. picname = xmlBuilder.createElement("name") # name标签
  47. namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
  48. picname.appendChild(namecontent)
  49. object.appendChild(picname) # name标签结束
  50. pose = xmlBuilder.createElement("pose") # pose标签
  51. posecontent = xmlBuilder.createTextNode("Unspecified")
  52. pose.appendChild(posecontent)
  53. object.appendChild(pose) # pose标签结束
  54. truncated = xmlBuilder.createElement("truncated") # truncated标签
  55. truncatedContent = xmlBuilder.createTextNode("0")
  56. truncated.appendChild(truncatedContent)
  57. object.appendChild(truncated) # truncated标签结束
  58. difficult = xmlBuilder.createElement("difficult") # difficult标签
  59. difficultcontent = xmlBuilder.createTextNode("0")
  60. difficult.appendChild(difficultcontent)
  61. object.appendChild(difficult) # difficult标签结束
  62. bndbox = xmlBuilder.createElement("bndbox") # bndbox标签
  63. xmin = xmlBuilder.createElement("xmin") # xmin标签
  64. mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
  65. xminContent = xmlBuilder.createTextNode(str(mathData))
  66. xmin.appendChild(xminContent)
  67. bndbox.appendChild(xmin) # xmin标签结束
  68. ymin = xmlBuilder.createElement("ymin") # ymin标签
  69. mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
  70. yminContent = xmlBuilder.createTextNode(str(mathData))
  71. ymin.appendChild(yminContent)
  72. bndbox.appendChild(ymin) # ymin标签结束
  73. xmax = xmlBuilder.createElement("xmax") # xmax标签
  74. mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
  75. xmaxContent = xmlBuilder.createTextNode(str(mathData))
  76. xmax.appendChild(xmaxContent)
  77. bndbox.appendChild(xmax) # xmax标签结束
  78. ymax = xmlBuilder.createElement("ymax") # ymax标签
  79. mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
  80. ymaxContent = xmlBuilder.createTextNode(str(mathData))
  81. ymax.appendChild(ymaxContent)
  82. bndbox.appendChild(ymax) # ymax标签结束
  83. object.appendChild(bndbox) # bndbox标签结束
  84. annotation.appendChild(object) # object标签结束
  85. f = open(xmlPath + name[0:-4] + ".xml", 'w')
  86. xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
  87. f.close()
  88. makexml("D:/A_coin/yolov5-master/CoinData/labels/", "D:/A_coin/yolov5-master/CoinData/xml/",
  89. "D:/A_coin/yolov5-master/CoinData/Images/") # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
  90. # if __name__ == "__main__":
  91. # picPath = "D:/A_coin/yolov5-master/CoinData/Images/" # 图片所在文件夹路径,后面的\\一定要带上
  92. # txtPath = "D:/A_coin/yolov5-master/CoinData/labels/" # txt所在文件夹路径,后面的\\一定要带上
  93. # xmlPath = "D:/A_coin/yolov5-master/CoinData/xml/" # xml文件保存路径,后面的\\一定要带上
  94. # makexml(picPath, txtPath, xmlPath)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/93118
推荐阅读
  

闽ICP备14008679号