逆向将物体检测数据集生成labelme标注的数据

举报
AI浩 发表于 2021/12/22 23:21:51 2021/12/22
【摘要】 对一些现有的数据集进行反推,生成labelme标注的格式。生成的效果如下图: 使用了 RSOD部分数据,将VOC数据集反推为labelme的标注数据。 代码如下: import sysimport os.path as ospimport iofrom labelme.logger import loggerfrom labe...

对一些现有的数据集进行反推,生成labelme标注的格式。生成的效果如下图:

使用了 RSOD部分数据,将VOC数据集反推为labelme的标注数据。

代码如下:


  
  1. import sys
  2. import os.path as osp
  3. import io
  4. from labelme.logger import logger
  5. from labelme import PY2
  6. from labelme import QT4
  7. import PIL.Image
  8. import base64
  9. from labelme import utils
  10. import os
  11. import cv2
  12. import xml.etree.ElementTree as ET
  13. module_path = os.path.abspath(os.path.join('..'))
  14. if module_path not in sys.path:
  15. sys.path.append(module_path)
  16. import json
  17. from PIL import Image
  18. Image.MAX_IMAGE_PIXELS = None
  19. imageroot = 'RSOD/'
  20. listDir = ['aircraft', 'oiltank']//数据的类别
  21. def load_image_file(filename):
  22. try:
  23. image_pil = PIL.Image.open(filename)
  24. except IOError:
  25. logger.error('Failed opening image file: {}'.format(filename))
  26. return
  27. # apply orientation to image according to exif
  28. image_pil = utils.apply_exif_orientation(image_pil)
  29. with io.BytesIO() as f:
  30. ext = osp.splitext(filename)[1].lower()
  31. if PY2 and QT4:
  32. format = 'PNG'
  33. elif ext in ['.jpg', '.jpeg']:
  34. format = 'JPEG'
  35. else:
  36. format = 'PNG'
  37. image_pil.save(f, format=format)
  38. f.seek(0)
  39. return f.read()
  40. def dict_json(flags, imageData, shapes, imagePath, fillColor=None, lineColor=None, imageHeight=100, imageWidth=100):
  41. '''
  42. :param imageData: str
  43. :param shapes: list
  44. :param imagePath: str
  45. :param fillColor: list
  46. :param lineColor: list
  47. :return: dict
  48. '''
  49. return {"version": "3.16.4", "flags": flags, "shapes": shapes, 'lineColor': lineColor, "fillColor": fillColor,
  50. 'imagePath': imagePath.split('/')[-1], "imageData": imageData, 'imageHeight': imageHeight,
  51. 'imageWidth': imageWidth}
  52. data = json.load(open('1.json'))
  53. for subPath in listDir:
  54. xmlpathName = imageroot + subPath + '/Annotation/xml'
  55. imagepath = imageroot + subPath + '/JPEGImages'
  56. resultFile = os.listdir(xmlpathName)
  57. for file in resultFile:
  58. print(file)
  59. imagePH = imagepath + '/' + file.split('.')[0] + '.jpg'
  60. print(imagePH)
  61. tree = ET.parse(xmlpathName + '/' + file)
  62. image = cv2.imread(imagePH)
  63. shapes = data["shapes"]
  64. version = data["version"]
  65. flags = data["flags"]
  66. lineColor = data["lineColor"]
  67. fillColor = data['fillColor']
  68. newshapes = []
  69. for elem in tree.iter():
  70. if 'object' in elem.tag:
  71. name = ''
  72. xminNode = 0
  73. yminNode = 0
  74. xmaxNode = 0
  75. ymaxNode = 0
  76. for attr in list(elem):
  77. if 'name' in attr.tag:
  78. name = attr.text
  79. if 'bndbox' in attr.tag:
  80. for dim in list(attr):
  81. if 'xmin' in dim.tag:
  82. xminNode = int(round(float(dim.text)))
  83. if 'ymin' in dim.tag:
  84. yminNode = int(round(float(dim.text)))
  85. if 'xmax' in dim.tag:
  86. xmaxNode = int(round(float(dim.text)))
  87. if 'ymax' in dim.tag:
  88. ymaxNode = int(round(float(dim.text)))
  89. line_color = None
  90. fill_color = None
  91. newPoints = [[float(xminNode), float(yminNode)], [float(xmaxNode), float(ymaxNode)]]
  92. shape_type = 'rectangle'
  93. flags = flags
  94. newshapes.append(
  95. {"label": name, "line_color": line_color, "fill_color": fill_color, "points": newPoints,
  96. "shape_type": shape_type, "flags": flags})
  97. imageData_90 = load_image_file(imagePH)
  98. imageData_90 = base64.b64encode(imageData_90).decode('utf-8')
  99. imageHeight = image.shape[0]
  100. imageWidth = image.shape[1]
  101. data_90 = dict_json(flags, imageData_90, newshapes, imagePH, fillColor, lineColor, imageHeight, imageWidth)
  102. json_file = imagePH[:-4] + '.json'
  103. json.dump(data_90, open(json_file, 'w'))

文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。

原文链接:wanghao.blog.csdn.net/article/details/105738682

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。