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

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

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

tt.png

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

代码如下:

import sys
import os.path as osp
import io
from labelme.logger import logger
from labelme import PY2
from labelme import QT4
import PIL.Image
import base64
from labelme import utils
import os
import cv2
import xml.etree.ElementTree as ET
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)
import json
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
imageroot = 'RSOD/'
listDir = ['aircraft', 'oiltank']//数据的类别
def load_image_file(filename):
    try:
        image_pil = PIL.Image.open(filename)
    except IOError:
        logger.error('Failed opening image file: {}'.format(filename))
        return
    # apply orientation to image according to exif
    image_pil = utils.apply_exif_orientation(image_pil)
    with io.BytesIO() as f:
        ext = osp.splitext(filename)[1].lower()
        if PY2 and QT4:
            format = 'PNG'
        elif ext in ['.jpg', '.jpeg']:
            format = 'JPEG'
        else:
            format = 'PNG'
        image_pil.save(f, format=format)
        f.seek(0)
        return f.read()
def dict_json(flags, imageData, shapes, imagePath, fillColor=None, lineColor=None, imageHeight=100, imageWidth=100):
    '''
    :param imageData: str
    :param shapes: list
    :param imagePath: str
    :param fillColor: list
    :param lineColor: list
    :return: dict
    '''
    return {"version": "3.16.4", "flags": flags, "shapes": shapes, 'lineColor': lineColor, "fillColor": fillColor,
            'imagePath': imagePath.split('/')[-1], "imageData": imageData, 'imageHeight': imageHeight,
            'imageWidth': imageWidth}
data = json.load(open('1.json'))
for subPath in listDir:
    xmlpathName = imageroot + subPath + '/Annotation/xml'
    imagepath = imageroot + subPath + '/JPEGImages'
    resultFile = os.listdir(xmlpathName)
    for file in resultFile:
        print(file)
        imagePH = imagepath + '/' + file.split('.')[0] + '.jpg'
        print(imagePH)
        tree = ET.parse(xmlpathName + '/' + file)
        image = cv2.imread(imagePH)
        shapes = data["shapes"]
        version = data["version"]
        flags = data["flags"]
        lineColor = data["lineColor"]
        fillColor = data['fillColor']
        newshapes = []
        for elem in tree.iter():
            if 'object' in elem.tag:
                name = ''
                xminNode = 0
                yminNode = 0
                xmaxNode = 0
                ymaxNode = 0
                for attr in list(elem):
                    if 'name' in attr.tag:
                        name = attr.text
                    if 'bndbox' in attr.tag:
                        for dim in list(attr):
                            if 'xmin' in dim.tag:
                                xminNode = int(round(float(dim.text)))
                            if 'ymin' in dim.tag:
                                yminNode = int(round(float(dim.text)))
                            if 'xmax' in dim.tag:
                                xmaxNode = int(round(float(dim.text)))
                            if 'ymax' in dim.tag:
                                ymaxNode = int(round(float(dim.text)))
                line_color = None
                fill_color = None
                newPoints = [[float(xminNode), float(yminNode)], [float(xmaxNode), float(ymaxNode)]]
                shape_type = 'rectangle'
                flags = flags
                newshapes.append(
                    {"label": name, "line_color": line_color, "fill_color": fill_color, "points": newPoints,
                     "shape_type": shape_type, "flags": flags})
        imageData_90 = load_image_file(imagePH)
        imageData_90 = base64.b64encode(imageData_90).decode('utf-8')
        imageHeight = image.shape[0]
        imageWidth = image.shape[1]
        data_90 = dict_json(flags, imageData_90, newshapes, imagePH, fillColor, lineColor, imageHeight, imageWidth)
        json_file = imagePH[:-4] + '.json'
        json.dump(data_90, open(json_file, 'w'))


目录
相关文章
|
3月前
|
存储 XML JSON
开集目标检测-标签提示目标检测大模型(吊打YOLO系列-自动化检测标注)
开集目标检测-标签提示目标检测大模型(吊打YOLO系列-自动化检测标注)
96 2
|
5月前
|
XML 数据格式 Python
Labelimg标注自己的数据集,及如何划分训练集和验证集,应用于Yolov5
Labelimg标注自己的数据集,及如何划分训练集和验证集,应用于Yolov5
379 0
|
人工智能 数据可视化 数据处理
快速在 PaddleLabel 标注的花朵分类数据集上展示如何应用 PaddleX 训练 MobileNetV3_ssld 网络
快速在 PaddleLabel 标注的花朵分类数据集上展示如何应用 PaddleX 训练 MobileNetV3_ssld 网络
603 0
快速在 PaddleLabel 标注的花朵分类数据集上展示如何应用 PaddleX 训练 MobileNetV3_ssld 网络
【yolo训练数据集】标注好的垃圾分类数据集共享
【yolo训练数据集】标注好的垃圾分类数据集共享
1208 63
【yolo训练数据集】标注好的垃圾分类数据集共享
|
XML JSON 数据中心
目标检测VOC数据集标注XML文件转EasyDL数据集标注Json格式
目标检测VOC数据集标注XML文件转EasyDL数据集标注Json格式
目标检测VOC数据集标注XML文件转EasyDL数据集标注Json格式
|
2月前
|
XML 机器学习/深度学习 算法
目标检测算法训练数据准备——Penn-Fudan数据集预处理实例说明(附代码)
目标检测算法训练数据准备——Penn-Fudan数据集预处理实例说明(附代码)
46 1
|
4月前
安装并使用labelImg标注数据集,yolo,VOC格式
安装并使用labelImg标注数据集,yolo,VOC格式
89 0
|
XML 数据可视化 数据格式
【数据集显示标注】VOC文件结构+数据集标注可视化+代码实现
【数据集显示标注】VOC文件结构+数据集标注可视化+代码实现
247 0
|
XML 机器学习/深度学习 数据处理
|
XML 数据处理 数据格式
目标检测之——labelImg标注工具使用方法(二)
目标检测之——labelImg标注工具使用方法(二)
目标检测之——labelImg标注工具使用方法(二)