读入并写入xml文件,制作voc-转coco数据集

1.读入并写入xml文件

annotations_dir='./VOC2007/Annotations'#标注信息
out_path_train='/home/zuo/PycharmProjects/untitled/voc2coco-pattern-master/train_xml'
out_path_test='/home/zuo/PycharmProjects/untitled/voc2coco-pattern-master/test_xml'
out_path_val='/home/zuo/PycharmProjects/untitled/voc2coco-pattern-master/val_xml'

import os
if os.path.exists(out_path_train):
    print('true')
else:
    os.mkdir(out_path_train)

if os.path.exists(out_path_test):
    print('true')
else:
    os.mkdir(out_path_test)

if os.path.exists(out_path_val):
    print('true')
else:
    os.mkdir(out_path_val)
from xml.etree.ElementTree import ElementTree, Element
o=[]
for filename in os.listdir(annotations_dir):
    o.append(filename[:-4])
print(len(o))
n=len(o)
#print(o)
def read_xml(in_path):
    '''''读取并解析xml文件
       in_path: xml路径
       return: ElementTree'''
    tree = ElementTree()
    tree.parse(in_path)
    return tree
def write_xml(tree, out_path):
    '''''将xml文件写出
       tree: xml树
       out_path: 写出路径'''
    tree.write(out_path, encoding="utf-8", xml_declaration=True)
#o=[]#存储所有的标注信息
T=os.listdir(annotations_dir)
print(os.listdir(annotations_dir))
for i in range(len(os.listdir(annotations_dir))):
    while i<=0.8 * n:
        print(T[i])
        tree=read_xml(annotations_dir+'/'+T[i])
        print(tree)
        write_xml(tree, out_path_train+'/'+T[i])
        break
    while 0.9 * n>i>0.8*n:
        print(T[i])
        tree = read_xml(annotations_dir + '/' + T[i])
        write_xml(tree, out_path_test+'/'+T[i])
        break
    while i >=0.9 * n:
        print(T[i])
        tree = read_xml(annotations_dir + '/' + T[i])
        write_xml(tree, out_path_val+'/'+T[i])
        break
    #o.append(filename[:-4])
#print(len(o))
#print(o)
'''
for i in range(len(o)):
    print('{}\n'.format(o[i][:-4]))#\n换行
def write_txt(txt,o):#写入文本信息,参数分别为:路径,即将写入的列表
    with open(txt,'w') as f:
        for i in range(len(o)):
            f.write('{}\n'.format(o[i]))#\n换行
def read_txt(txt):#阅读文本信息
    lines = []
    with open(txt, 'r') as f:
        for eachline in f:
            eachline = eachline.strip('\n')
            eachline = str(eachline)
            #line = eachline + '.bmp'
            #print(line)
            lines.append(eachline)
    #splitlines = [x.strip().split(' ') for x in lines]
    return lines#返回带类列表
#quan=read_txt(r'D:\research\ce_Faster-RCNN-TensorFlow-Python3-master\data\VOCdevkit2007\VOC2007\ImageSets\Main\quan.txt')
#print(quan,len(quan))
quan=o
n=len(quan)
n1=int(0.2*n)#安装8:2分配数据集进行交叉验证
test1=quan[0:n1]
train1=[k for k in quan if k not in test1]#取补集合
print(test1)
print(train1)
test2=quan[n1:2*n1]
train2=[k for k in quan if k not in test2]
print(test2)
print(train2)
test3=quan[2*n1:3*n1]
train3=[k for k in quan if k not in test3]
print(test3)
print(train3)
test4=quan[3*n1:4*n1]
train4=[k for k in quan if k not in test4]
print(test4)
print(train4)
test5=quan[4*n1:]
train5=[k for k in quan if k not in test5]
print(test5)
print(train5)
write_txt('./trainval1.txt',train1)
write_txt('./test1.txt',test1)
write_txt('./trainval2.txt',train2)
write_txt('./test2.txt',test2)
write_txt('./trainval3.txt',train3)
write_txt('./test3.txt',test3)
write_txt('./trainval4.txt',train4)
write_txt('./test4.txt',test4)
write_txt('./trainval5.txt',train5)
write_txt('./test5.txt',test5)
quan1=len(read_txt('./test1.txt'))+len(read_txt('./trainval1.txt'))
quan2=len(read_txt('./test2.txt'))+len(read_txt('./trainval2.txt'))
quan3=len(read_txt('./test3.txt'))+len(read_txt('./trainval3.txt'))
quan4=len(read_txt('./test4.txt'))+len(read_txt('./trainval4.txt'))
quan5=len(read_txt('./test5.txt'))+len(read_txt('./trainval5.txt'))
print(quan1,quan2,quan3,quan4,quan5)
'''

2.xml-json,VOC-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
import os, sys
class PascalVOC2coco(object):
    def __init__(self, xml=[], save_json_path='./new.json'):
        '''
        :param xml: 所有Pascal VOC的xml文件路径组成的列表
        :param save_json_path: json保存位置
        '''
        self.xml = xml
        self.save_json_path = save_json_path
        self.images = []
        self.categories = []
        self.annotations = []
        # self.data_coco = {}
        self.label = []
        self.annID = 1
        self.height = 0
        self.width = 0
        self.ob = []

        self.save_json()

    def data_transfer(self):
        for num, json_file in enumerate(self.xml):

            # 进度输出
            sys.stdout.write('\r>> Converting image %d/%d' % (
                num + 1, len(self.xml)))
            sys.stdout.flush()

            self.json_file = json_file
            # print("self.json", self.json_file)
            self.num = num
            # print(self.num)
            path = os.path.dirname(self.json_file)
            # print(path)
            path = os.path.dirname(path)
            # print(path)
            # path=os.path.split(self.json_file)[0]
            # path=os.path.split(path)[0]
            obj_path = glob.glob(os.path.join(path, 'SegmentationObject', '*.png'))
            # print(obj_path)
            with open(json_file, 'r',encoding='utf8') as fp:#,encoding='utf8'
                #print('fp',fp)
                flag = 0
                for p in fp:
                    #print('p',p)
                    # if 'folder' in p:
                    #     folder =p.split('>')[1].split('<')[0]
                    f_name = 1
                    if 'filename' in p:
                        self.filen_ame = p.split('>')[1].split('<')[0]
                        # print(self.filen_ame)
                        f_name = 0

                        self.path = os.path.join(path, 'SegmentationObject', self.filen_ame.split('.')[0] + '.png')
                        # if self.path not in obj_path:
                        #    break

                    if 'width' in p:
                        self.width = int(p.split('>')[1].split('<')[0])
                        # print(self.width)
                    if 'height' in p:
                        self.height = int(p.split('>')[1].split('<')[0])

                        self.images.append(self.image())
                        # print(self.image())

                    if flag == 1:
                        self.supercategory = self.ob[0]
                        if self.supercategory not in self.label:
                            self.categories.append(self.categorie())
                            self.label.append(self.supercategory)

                        # 边界框
                        x1 = int(self.ob[1]);
                        y1 = int(self.ob[2]);
                        x2 = int(self.ob[3]);
                        y2 = int(self.ob[4])
                        self.rectangle = [x1, y1, x2, y2]
                        self.bbox = [x1, y1, x2 - x1, y2 - y1]  # COCO 对应格式[x,y,w,h]

                        self.annotations.append(self.annotation())
                        self.annID += 1
                        self.ob = []
                        flag = 0
                    elif f_name == 1:
                        if 'name' in p:
                            self.ob.append(p.split('>')[1].split('<')[0])

                        if 'xmin' in p:
                            self.ob.append(p.split('>')[1].split('<')[0])

                        if 'ymin' in p:
                            self.ob.append(p.split('>')[1].split('<')[0])

                        if 'xmax' in p:
                            self.ob.append(p.split('>')[1].split('<')[0])

                        if 'ymax' in p:
                            self.ob.append(p.split('>')[1].split('<')[0])
                            flag = 1

                    '''
                    if '<object>' in p:
                        # 类别
                        print(next(fp))
                        d = [next(fp).split('>')[1].split('<')[0] for _ in range(7)]
                        self.supercategory = d[0]
                        if self.supercategory not in self.label:
                            self.categories.append(self.categorie())
                            self.label.append(self.supercategory)
                        # 边界框
                        x1 = int(d[-4]);
                        y1 = int(d[-3]);
                        x2 = int(d[-2]);
                        y2 = int(d[-1])
                        self.rectangle = [x1, y1, x2, y2]
                        self.bbox = [x1, y1, x2 - x1, y2 - y1]  # COCO 对应格式[x,y,w,h]
                        self.annotations.append(self.annotation())
                        self.annID += 1
                     '''

        sys.stdout.write('\n')
        sys.stdout.flush()

    def image(self):
        image = {}
        image['height'] = self.height
        image['width'] = self.width
        image['id'] = self.num + 1
        image['file_name'] = self.filen_ame
        return image

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

    def annotation(self):
        annotation = {}
        # annotation['segmentation'] = [self.getsegmentation()]
        annotation['segmentation'] = [list(map(float, self.getsegmentation()))]
        annotation['iscrowd'] = 0
        annotation['image_id'] = self.num + 1
        # annotation['bbox'] = list(map(float, self.bbox))
        annotation['bbox'] = self.bbox
        annotation['category_id'] = self.getcatid(self.supercategory)
        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 getsegmentation(self):

        try:
            mask_1 = cv2.imread(self.path, 0)
            mask = np.zeros_like(mask_1, np.uint8)
            rectangle = self.rectangle
            mask[rectangle[1]:rectangle[3], rectangle[0]:rectangle[2]] = mask_1[rectangle[1]:rectangle[3],
                                                                         rectangle[0]:rectangle[2]]

            # 计算矩形中点像素值
            mean_x = (rectangle[0] + rectangle[2]) // 2
            mean_y = (rectangle[1] + rectangle[3]) // 2

            end = min((mask.shape[1], int(rectangle[2]) + 1))
            start = max((0, int(rectangle[0]) - 1))

            flag = True
            for i in range(mean_x, end):
                x_ = i;
                y_ = mean_y
                pixels = mask_1[y_, x_]
                if pixels != 0 and pixels != 220:  # 0 对应背景 220对应边界线
                    mask = (mask == pixels).astype(np.uint8)
                    flag = False
                    break
            if flag:
                for i in range(mean_x, start, -1):
                    x_ = i;
                    y_ = mean_y
                    pixels = mask_1[y_, x_]
                    if pixels != 0 and pixels != 220:
                        mask = (mask == pixels).astype(np.uint8)
                        break
            self.mask = mask

            return self.mask2polygons()

        except:
            return [0]

    def mask2polygons(self):
        contours = cv2.findContours(self.mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  # 找到轮廓线
        bbox = []
        for cont in contours[1]:
            [bbox.append(i) for i in list(cont.flatten())]
            # map(bbox.append,list(cont.flatten()))
        return bbox  # list(contours[1][0].flatten())

    # '''
    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 更加美观显示
xml_file = glob.glob('./val_xml/*.xml')
# xml_file=['./Annotations/000032.xml']
# xml_file=['00000007_05499_d_0000037.xml']
PascalVOC2coco(xml_file, 'val.json')
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页