YOLO数据转化

YOLO数据转化

1、将labelimg转化为xml

2、将xml转化为txt


1、code(python)

来源见网址:

https://blog.csdn.net/kapok_lalala/article/details/78990996

#! /usr/bin/python

import os, sys
import glob
from PIL import Image

#ICDAR image path
src_img_dir = "./JPEGImages"
# ICDAR ground truth path
src_txt_dir = "./gt"
src_ann_dir = "./Annotations"
img_Lists = glob.glob(src_img_dir + '/*.jpg')

img_basenames = [] # e.g. 100.jpg
for item in img_Lists:
    img_basenames.append(os.path.basename(item))

img_names = [] # e.g. 100
for item in img_basenames:
    temp1, temp2 = os.path.splitext(item)
    img_names.append(temp1)

for img in img_names:
    im = Image.open((src_img_dir + '/' + img + '.jpg'))
    width, height = im.size
    # open the crospronding txt file
    gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines()

    # write in xml file
    #os.mknod(src_txt_dir + '/' + img + '.xml')
    xml_file = open((src_ann_dir + '/' + img + '.xml'), 'w')
    xml_file.write('\n')
    xml_file.write('\tsimple\n')
    xml_file.write('\t' + str(img) + '.jpg' + '\n')
    xml_file.write('\t\n')
    xml_file.write('\t\t' +'The simple Database' + '\n')
    xml_file.write('\t\t' +'simple' + '\n')
    xml_file.write('\t\tflickr\n')
    xml_file.write('\t\t325991873\n')
    xml_file.write('\t\n')
    xml_file.write('\t\n')
    xml_file.write('\t\tarchin\n')
    xml_file.write('\t\t?\n')
    xml_file.write('\t
\n')
    xml_file.write('\t\n')
    xml_file.write('\t\t' + str(width) + '\n')
    xml_file.write('\t\t' + str(height) + '\n')
    xml_file.write('\t\t3\n')
    xml_file.write('\t
\n')
    xml_file.write('\t0\n')
    # write the region of text on xml file
    for img_each_label in gt:
        spt = img_each_label.split(',')
        xml_file.write('\t\n')
        xml_file.write('\t\ttext\n')
        xml_file.write('\t\tUnspecified\n')
        xml_file.write('\t\t0\n')
        xml_file.write('\t\t0\n')
        xml_file.write('\t\t\n')
        xml_file.write('\t\t\t' + str(spt[0]) + '\n')
        xml_file.write('\t\t\t' + str(spt[1]) + '\n')
        xml_file.write('\t\t\t' + str(spt[2]) + '\n')
        xml_file.write('\t\t\t' + str(spt[3]) + '\n')
        xml_file.write('\t\t
\n')
        xml_file.write('\t
\n')

    xml_file.write('')

2、code(python)

来源见网址:https://blog.csdn.net/lucky__ing/article/details/78321178?locationNum=2&fps=1

#
import os
import re
import cv2
import sys
#the source folder that you will read the .jpg and .xml from
src0='/home/lucky/open/wires/1017wire'
#the destation folder that you will save the .jpg files and .txt files, which are rename and transformed
dst0='/home/lucky/open/wires/picture_voc/10.21'
#the num of the class that your cutting picture described in your .xml file belong to
voc_name0='/home/lucky/yolo/labelImg-master/data/predefined_classes.txt'
if __name__=='__main__':
    if len(sys.argv)<4:
        print('yolo-voc program need 5 argv.you must input command like\n   python 123.py [start_num] [source folder name] [deststion folder name] [voc name file]')
    else:
        p=int(sys.argv[1])
        src=sys.argv[2]+'/'
        dst=sys.argv[3]+'/'
        if len(sys.argv)>4:
            voc_name=sys.argv[4]
            voc_flag=True
        else:
            voc_name=''
            voc_flag=False
        '''src=input('please input the path of source:\n')
        if src=='':
            src=src0
            dst=dst0
            voc_name=voc_name0
        else:
            dst=input('please input the path of destination:\n')
            if dst=='':
                dst = dst0
                voc_name = voc_name0
            else:
                voc_name = input('please input the class_name file:\n')
                if dst=='':
                    voc_name = voc_name0'''
        listd=os.listdir(src)
        txt=re.compile(r'.*(xml)')
        listdir=[]
        for i in listd:
            if re.findall(txt,i):
                listdir.append(i)

        cc=r'(.*)'

        xxmin=re.compile(r'(.*)')
        yymin=re.compile(r'(.*)')
        xxmax=re.compile(r'(.*)')
        yymax=re.compile(r'(.*)')
        wwidth=re.compile(r'(.*)')
        hheight=re.compile(r'(.*)')
        nname=re.compile(r'(.*)')
        if voc_flag==True:
            f=open(voc_name)
            listname=f.read()
            f.close()

        p=0
        for i in listdir:
            i_file=open(src+i,'r')
            i_content=i_file.read()
            i_file.close()
            print(i)
            #print(i_content)
            name = (re.findall(nname, i_content)[0])
            if voc_flag==True:
                index = listname.index(name)
            else:
                index = 0
            txtname0 = dst + str(p).zfill(4) + '.txt'
            f = open(txtname0, 'w')
            width=int(re.findall(wwidth,i_content)[0])
            height=int(re.findall(hheight,i_content)[0])
            for j in range(re.findall(xxmin,i_content).__len__()):

                xmin=int(re.findall(xxmin,i_content)[j])
                ymin=int(re.findall(yymin,i_content)[j])
                xmax=int(re.findall(xxmax,i_content)[j])
                ymax=int(re.findall(yymax,i_content)[j])


            #txtname=dst+i
            #txtname0=txtname.replace('xml','txt')

                if j!=0:
                    f.write('\n')
                f.write(str(index)+' '+'%0.4f'%((float(xmin)+float(xmax))/float(width)/2)+' '+'%0.4f'%((float(ymin)+float(ymax))/float(height)/2)+' '+'%0.4f'%(float(xmax-xmin)/float(width))+' '+'%0.4f'%(float(ymax-ymin)/float(height)))
            f.close()
            imgsource=src+i
            imgname0 = imgsource.replace('xml', 'jpg')
            img=cv2.imread(imgname0)
            #print(img)

            if img is None:
                imgname0=imgname0.replace('jpg','JPG')
                img = cv2.imread(imgname0)
            if img is None:
                imgname0 = imgname0.replace('JPG', 'png')
                img = cv2.imread(imgname0)
            imgdst=dst+str(p).zfill(4)+'.jpg'
            #imgname1 = imgdst.replace('xml', 'jpg')
            cv2.imwrite(imgdst,img)
            p+=1




你可能感兴趣的:(算法学习)