labelme 转 VOC2007 目标检测矩形框

# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 14:51:00 2019

@author: Andrea
"""

import os
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split
#1.标签路径
labelme_path = "F:\\fayuan1018"              #原始labelme标注数据路径
saved_path = "F:\\VOC2007-fayuan1018\\"                #保存路径

#2.创建要求文件夹
if not os.path.exists(saved_path + "Annotations"):
    os.makedirs(saved_path + "Annotations")
if not os.path.exists(saved_path + "JPEGImages/"):
    os.makedirs(saved_path + "JPEGImages/")
if not os.path.exists(saved_path + "ImageSets/Main/"):
    os.makedirs(saved_path + "ImageSets/Main/")
    
##3.获取待处理文件
#files = glob(labelme_path + "*.json")
#print(files)
#files = [i.split("/")[-1].split(".json")[0] for i in files]

#4.读取标注信息并写入 xml
#for json_file_ in files:
for json_file_ in os.listdir(labelme_path):
    if('.json' not in json_file_):
        continue
    else:
        json_file_ = json_file_.split('.json')[0]
    print(json_file_)
    json_filename = os.path.join(labelme_path , json_file_ + ".json")
    print(json_filename)
    json_file = json.load(open(json_filename,"r",encoding="utf-8"))
    print(os.path.join(labelme_path , json_file_ +".jpg"))
    height, width, channels = cv2.imread(os.path.join(labelme_path , json_file_ +".jpg")).shape
    with codecs.open(saved_path + "Annotations/"+json_file_ + ".xml","w","utf-8") as xml:
        xml.write('\n')
        xml.write('\t' + 'UAV_data' + '\n')
        xml.write('\t' + json_file_ + ".jpg" + '\n')
        xml.write('\t\n')
        xml.write('\t\tThe UAV autolanding\n')
        xml.write('\t\tUAV AutoLanding\n')
        xml.write('\t\tflickr\n')
        xml.write('\t\tNULL\n')
        xml.write('\t\n')
        xml.write('\t\n')
        xml.write('\t\tNULL\n')
        xml.write('\t\tYuanyiqin\n')
        xml.write('\t\n')
        xml.write('\t\n')
        xml.write('\t\t'+ str(width) + '\n')
        xml.write('\t\t'+ str(height) + '\n')
        xml.write('\t\t' + str(channels) + '\n')
        xml.write('\t\n')
        xml.write('\t\t0\n')
        for multi in json_file["shapes"]:
            points = np.array(multi["points"])
            xmin = min(points[:,0])
            xmax = max(points[:,0])
            ymin = min(points[:,1])
            ymax = max(points[:,1])
            label = multi["label"]
            if xmax <= xmin:
                pass
            elif ymax <= ymin:
                pass
            else:
                xml.write('\t\n')
                xml.write('\t\t'+str(label)+'\n')
                xml.write('\t\tUnspecified\n')
                xml.write('\t\t1\n')
                xml.write('\t\t0\n')
                xml.write('\t\t\n')
                xml.write('\t\t\t' + str(xmin) + '\n')
                xml.write('\t\t\t' + str(ymin) + '\n')
                xml.write('\t\t\t' + str(xmax) + '\n')
                xml.write('\t\t\t' + str(ymax) + '\n')
                xml.write('\t\t\n')
                xml.write('\t\n')
                print(json_filename,xmin,ymin,xmax,ymax,label)
        xml.write('')
        
#5.复制图片到 VOC2007/JPEGImages/下
image_files = glob(labelme_path + "*.jpg")
print("copy image files to VOC007/JPEGImages/")
for image in image_files:
    shutil.copy(image,saved_path +"JPEGImages/")
    
#6.split files for txt
txtsavepath = saved_path + "ImageSets/Main/"
ftrainval = open(txtsavepath+'/trainval.txt', 'w')
ftest = open(txtsavepath+'/test.txt', 'w')
ftrain = open(txtsavepath+'/train.txt', 'w')
fval = open(txtsavepath+'/val.txt', 'w')
total_files = glob("./VOC2007/Annotations/*.xml")
total_files = [i.split("/")[-1].split(".xml")[0] for i in total_files]
#test_filepath = ""
for file in total_files:
    ftrainval.write(file + "\n")
#test
#for file in os.listdir(test_filepath):
#    ftest.write(file.split(".jpg")[0] + "\n")
#split
train_files,val_files = train_test_split(total_files,test_size=0.15,random_state=42)
#train
for file in train_files:
    ftrain.write(file + "\n")
#val
for file in val_files:
    fval.write(file + "\n")

ftrainval.close()
ftrain.close()
fval.close()
#ftest.close()

 

你可能感兴趣的:(labelme 转 VOC2007 目标检测矩形框)