将voc数据集xml格式、labelme标注转为coco数据集json格式

coco转voc点这里:将COCO数据集json格式文件转为VOC数据集xml格式

1.划分数据集

voc数据集中用于训练、验证、测试的xml和jpg全在一个文件夹里,所以需要根据txt文件先将这些文件分成训练集验证集测试集。(注:划分前要先分别建立好文件夹)

#某一个txt文本中的数字存的是图片的名字,
#要把这些名字的图片保存到另一个文件夹中
#修改两处,注意自己建立文件
import shutil
#这个库复制文件比较省事
from PIL import Image
import os

f3 = open("F:/研究生/研一/计算机视觉/yolov4-keras/VOCdevkit/VOC2007/ImageSets/Main/train.txt",'r') #train文件所在路径
for line2 in f3.readlines():
    line3=line2[:-1] #读取所有数字  000000
    #xmldir = 'F:/研究生/研一/计算机视觉/yolov4-keras/VOCdevkit/VOC2007/Annotations/'
    imgdir = 'F:/研究生/研一/计算机视觉/yolov4-keras/VOCdevkit/VOC2007/JPEGImages/'
    #所有的xml/img文件绝对路径
    savedir = 'F:/研究生/研一/计算机视觉/yolov4-keras/VOCdevkit/train/'
    #将用于train的xml文件提取出来的绝对路径
    #xmllist = os.listdir(xmldir)
    imglist = os.listdir(imgdir)
    for img in imglist:
        if '.jpg' in img:
            if line3 in img:
                shutil.copy(imgdir + '/' + img, savedir + '/' + img)
                print(img)
    '''
    for xml in xmllist:
        # if '.xml' in xml:
        if '.xml' in xml:
            if line3 in xml:
                fo = open(savedir + '/' + '{}'.format(xml), 'w')
                print('{}'.format(xml))
                fi = open(xmldir + '/' + '{}'.format(xml), 'r')
                content = fi.readlines()
                for line in content:
                    fo.write(line)
                fo.close()
                print('替换成功')
    '''
f3.close()

2.voc转coco

这里需要注意的是voc直接转的coco数据集仅用于目标检测,如果要用于实例分割还是需要自己制作样本,如labelme标注。(labelme标注的数据集可以直接转为用于实例分割的coco数据集,见第3条)

# coding:utf-8
import sys
import os
import json
import xml.etree.ElementTree as ET

START_BOUNDING_BOX_ID = 1

# 注意下面的dict存储的是实际检测的类别,需要根据自己的实际数据进行修改
# 这里以自己的数据集person和hat两个类别为例,如果是VOC数据集那就是20个类别
# 注意类别名称和xml文件中的标注名称一致

PRE_DEFINE_CATEGORIES = {"normal": 0, "defect": 1, "norbolt": 2, "debolt": 3}


# 注意按照自己的数据集名称修改编号和名称

def get(root, name):
    vars = root.findall(name)
    return vars


def get_and_check(root, name, length):
    vars = root.findall(name)
    if len(vars) == 0:
        raise NotImplementedError('Can not find %s in %s.' % (name, root.tag))
    if length > 0 and len(vars) != length:
        raise NotImplementedError('The size of %s is supposed to be %d, but is %d.' % (name, length, len(vars)))
    if length == 1:
        vars = vars[0]
    return vars


def get_filename_as_int(filename):
    try:
        filename = os.path.splitext(filename)[0]
        return int(filename)
    except:
        raise NotImplementedError('Filename %s is supposed to be an integer.' % (filename))


def convert(xml_dir, json_file):
    xmlFiles = os.listdir(xml_dir)

    json_dict = {"images": [], "type": "instances", "annotations": [],
                 "categories": []}
    categories = PRE_DEFINE_CATEGORIES
    bnd_id = START_BOUNDING_BOX_ID
    num = 0
    for line in xmlFiles:
        #         print("Processing %s"%(line))
        num += 1
        if num % 50 == 0:
            print("processing ", num, "; file ", line)

        xml_f = os.path.join(xml_dir, line)
        tree = ET.parse(xml_f)
        root = tree.getroot()
        ## The filename must be a number
        filename = line[:-4]
        image_id = get_filename_as_int(filename)
        size = get_and_check(root, 'size', 1)
        width = int(get_and_check(size, 'width', 1).text)
        height = int(get_and_check(size, 'height', 1).text)
        # image = {'file_name': filename, 'height': height, 'width': width,
        #          'id':image_id}
        image = {'file_name': (filename + '.jpg'), 'height': height, 'width': width,
                 'id': image_id}
        json_dict['images'].append(image)
        ## Cruuently we do not support segmentation
        #  segmented = get_and_check(root, 'segmented', 1).text
        #  assert segmented == '0'
        for obj in get(root, 'object'):
            category = get_and_check(obj, 'name', 1).text
            if category not in categories:
                new_id = len(categories)
                categories[category] = new_id
            category_id = categories[category]
            bndbox = get_and_check(obj, 'bndbox', 1)
            xmin = int(get_and_check(bndbox, 'xmin', 1).text) - 1
            ymin = int(get_and_check(bndbox, 'ymin', 1).text) - 1
            xmax = int(get_and_check(bndbox, 'xmax', 1).text)
            ymax = int(get_and_check(bndbox, 'ymax', 1).text)
            assert (xmax > xmin)
            assert (ymax > ymin)
            o_width = abs(xmax - xmin)
            o_height = abs(ymax - ymin)
            ann = {'area': o_width * o_height, 'iscrowd': 0, 'image_id':
                image_id, 'bbox': [xmin, ymin, o_width, o_height],
                   'category_id': category_id, 'id': bnd_id, 'ignore': 0,
                   'segmentation': []}
            json_dict['annotations'].append(ann)
            bnd_id = bnd_id + 1

    for cate, cid in categories.items():
        cat = {'supercategory': 'none', 'id': cid, 'name': cate}
        json_dict['categories'].append(cat)
    json_fp = open(json_file, 'w')
    json_str = json.dumps(json_dict)
    json_fp.write(json_str)
    json_fp.close()


if __name__ == '__main__':
    folder_list = ["train", "val", "test"]
    # 注意更改base_dir为本地实际图像和标注文件路径
    base_dir = "F:/研究生/研一/计算机视觉/yolov4-keras/VOCdevkit/"
    # 修改为自己的路径

    for i in range(3):
        folderName = folder_list[i]
        xml_dir = base_dir + folderName
        json_dir = base_dir + folderName + "/instances_" + folderName + ".json"

        print("deal: ", folderName)
        print("xml dir: ", xml_dir)
        print("json file: ", json_dir)

        convert(xml_dir, json_dir)

3.labelme标注转coco

需要修改自己的标签信息以及相关路径

# -*- coding:utf-8 -*-
# !/usr/bin/env python

import argparse
import json
import matplotlib.pyplot as plt
import skimage.io as io
import cv2
from labelme import utils
import numpy as np
import glob
import PIL.Image
from shapely.geometry import Polygon


class labelme2coco(object):
    def __init__(self, labelme_json=[], save_json_path='./new.json'):
        '''
        :param labelme_json: 所有labelme的json文件路径组成的列表
        :param save_json_path: json保存位置
        '''
        self.labelme_json = labelme_json
        self.save_json_path = save_json_path
        self.images = []
        # self.categories=[]
        # self.categories = [{'supercategory': 'column', 'id': 1, 'name': 'column'}, {'supercategory': 'box', 'id': 2, 'name': 'box'}, {'supercategory': 'fruit', 'id': 3, 'name': 'fruit'}, {'supercategory': 'package', 'id': 4, 'name': 'package'}]
        self.categories = [{'supercategory': 'A', 'id': 1, 'name': 'aircraft'},
                           {'supercategory': 'B', 'id': 2, 'name': 'overpass'},
                           {'supercategory': 'C', 'id': 3, 'name': 'oiltank'},
                           {'supercategory': 'D', 'id': 4, 'name': 'playground'}]

        self.annotations = []
        # self.data_coco = {}
        self.label = []
        self.annID = 1
        self.height = 0
        self.width = 0

        self.save_json()

    def data_transfer(self):
        for num, json_file in enumerate(self.labelme_json):
            with open(json_file, 'r') as fp:
                data = json.load(fp)  # 加载json文件
                self.images.append(self.image(data, num))
                for shapes in data['shapes']:
                    # label=shapes['label'].split('_')
                    label = shapes['label']
                    # print(shapes['label'])
                    # print(label)
                    #                    if label not in self.label:
                    #                        self.categories.append(self.categorie(label))
                    #                        self.label.append(label)
                    points = shapes['points']
                    print(points)
                    self.annotations.append(self.annotation(points, label, num))
                    self.annID += 1
        print(self.annotations)

    def image(self, data, num):
        image = {}
        # img = utils.img_b64_to_arr(data['imageData'])  # 解析原图片数据
        img=io.imread(data['imagePath']) # 通过图片路径打开图片
        # img = cv2.imread(data['imagePath'], 0)
        height, width = img.shape[:2]
        img = None
        image['height'] = height
        image['width'] = width
        image['id'] = num + 1
        image['file_name'] = data['imagePath'].split('/')[-1]

        self.height = height
        self.width = width

        return image

    def categorie(self, label):
        categorie = {}
        categorie['supercategory'] = label
        categorie['id'] = len(self.label) + 1  # 0 默认为背景
        categorie['name'] = label
        # print(categorie)
        return categorie

    def annotation(self, points, label, num):
        annotation = {}
        annotation['segmentation'] = [list(np.asarray(points).flatten())]
        poly = Polygon(points)
        area_ = round(poly.area, 6)
        annotation['area'] = area_
        annotation['iscrowd'] = 0
        annotation['image_id'] = num + 1
        # annotation['bbox'] = str(self.getbbox(points)) # 使用list保存json文件时报错(不知道为什么)
        # list(map(int,a[1:-1].split(','))) a=annotation['bbox'] 使用该方式转成list
        annotation['bbox'] = list(map(float, self.getbbox(points)))

        annotation['category_id'] = self.getcatid(label)
        # print(label)
        # annotation['category_id'] = len(self.label) + 1
        # print(self.getcatid(label))
        annotation['id'] = self.annID
        return annotation

    def getcatid(self, label):
        for categorie in self.categories:
            if label == categorie['name']:
                return categorie['id']
        return -1

    def getbbox(self, points):
        # img = np.zeros([self.height,self.width],np.uint8)
        # cv2.polylines(img, [np.asarray(points)], True, 1, lineType=cv2.LINE_AA)  # 画边界线
        # cv2.fillPoly(img, [np.asarray(points)], 1)  # 画多边形 内部像素值为1
        polygons = points
        mask = self.polygons_to_mask([self.height, self.width], polygons)
        return self.mask2box(mask)

    def mask2box(self, mask):
        '''从mask反算出其边框
        mask:[h,w]  0、1组成的图片
        1对应对象,只需计算1对应的行列号(左上角行列号,右下角行列号,就可以算出其边框)
        '''
        # np.where(mask==1)
        index = np.argwhere(mask == 1)
        rows = index[:, 0]
        clos = index[:, 1]
        # 解析左上角行列号
        left_top_r = np.min(rows)  # y
        left_top_c = np.min(clos)  # x

        # 解析右下角行列号
        right_bottom_r = np.max(rows)
        right_bottom_c = np.max(clos)

        # return [(left_top_r,left_top_c),(right_bottom_r,right_bottom_c)]
        # return [(left_top_c, left_top_r), (right_bottom_c, right_bottom_r)]
        # return [left_top_c, left_top_r, right_bottom_c, right_bottom_r]  # [x1,y1,x2,y2]
        return [left_top_c, left_top_r, right_bottom_c - left_top_c,
                right_bottom_r - left_top_r]  # [x1,y1,w,h] 对应COCO的bbox格式

    def polygons_to_mask(self, img_shape, polygons):
        mask = np.zeros(img_shape, dtype=np.uint8)
        mask = PIL.Image.fromarray(mask)
        xy = list(map(tuple, polygons))
        PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
        mask = np.array(mask, dtype=bool)
        return mask

    def data2coco(self):
        data_coco = {}
        data_coco['images'] = self.images
        data_coco['categories'] = self.categories
        data_coco['annotations'] = self.annotations
        return data_coco

    def save_json(self):
        self.data_transfer()
        self.data_coco = self.data2coco()
        # 保存json文件
        json.dump(self.data_coco, open(self.save_json_path, 'w'), indent=4)  # indent=4 更加美观显示


labelme_json = glob.glob('F:/研究生/研一/计算机视觉/mask-rcnn-keras/before/*.json')
# labelme_json = glob.glob('./*.json')
# labelme_json=['./1.json']
labelme2coco(labelme_json, 'F:/研究生/研一/计算机视觉/医学图像处理/rsdata/annotations/instances_train.json')



你可能感兴趣的:(计算机视觉,深度学习,python,计算机视觉,目标检测)