upwon
12/6/2018 - 3:58 PM

xml2txt 使用labelimg打标签后产生xml文件,使用以下代码可以将xml解析生成YOLO使用的txt文件

xml2txt 使用labelimg打标签后产生xml文件,使用以下代码可以将xml解析生成YOLO使用的txt文件

'''

用法:
python pic2txt.py  Z:/porject_wxw/image_train  Z:/porject_wxw/image_train
python pic2txt.py是指使用python 运行pic2txt文件。
Z:/porject_wxw/image_train是我们之前整理后的图片和txt文件所在的路径 切记不要在后面加/。
Z:/porject_wxw/image_train是 保存train.txt的路径

--------------------- 

'''
import os
import re
import sys
src='/home/lucky/open/tuoxie_VOC/yanming/'
path='/home/lucky/open/tuoxie_VOC/yanming/'
if __name__=='__main__':
    if len(sys.argv)<2:
        print('piclist2txt program need 1 argv.you must input command like\n   python 123.py [source folder name] [changed path]')
    else:
        src=sys.argv[1]+'/'
        if len(sys.argv)>2:
            path=sys.argv[2]+'/'
        else:
            path=src

        filedir=os.listdir(src)
        imgfile=re.compile(r'.*(jpg|jpeg|JPEG)')
        listdir=[]
        for i in filedir:
            if re.match(imgfile,i):
                listdir.append(i)
        #print(listdir)
        f=open(src+'train.txt','w')
        for i in listdir:
            print(path+i)
            #f.write(dst+i+'\n')
            f.write(path+i+'\n')
            #f.write((i).replace('.jpg','') + '\n')
        f.close()
import os
import re
import cv2
import sys
'''
python xml2txt.py 0001 Z:/porject_wxw/image_train   Z:/porject_wxw/image_train   Z:/porject_wxw/image_train/name/obj.names
python xml2txt.py,使用python(一般是指python2)运行xlm2txt
0001的意思是文件重命名的开始序列,例如,我写的是0001,则本次处理的图片的名字都是从0001.jpg名字开始增加的,如果你写的是647,则第一张被处理的图片则从0647.jpg开始增加。
Z:/porject_wxw/image_train 是指之前所有图片和xml文件所在的路径,切记不要在后面加/。
Z:/porject_wxw/image_train 是整理之后的图片和txt文件保存的地址路径,切记不要在后面加/。
Z:/porject_wxw/image_train/name/obj.names 是指我们上面提到的xml文件内标注的是标注物体属于某一类,但是yolo并不识别类别,只是识别编号,所以这里要把类名转为编号。该文件里面就是所有的类名,每个类名为一行,程序会根据顺序对他们进行编号,例如

我的有两种类别,分别是box和bad,那么如果xml文件里面里的box那么转换之后的txt文件内的编号就是0,bad就是1。如果只有一个类别的可以不用填写这个参数,默认就为0。 

obj.name里面的写法为
box
bad

'''

#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'<xmin>(.*)</xmin>'

        xxmin=re.compile(r'<xmin>(.*)</xmin>')
        yymin=re.compile(r'<ymin>(.*)</ymin>')
        xxmax=re.compile(r'<xmax>(.*)</xmax>')
        yymax=re.compile(r'<ymax>(.*)</ymax>')
        wwidth=re.compile(r'<width>(.*)</width>')
        hheight=re.compile(r'<height>(.*)</height>')
        nname=re.compile(r'<name>(.*)</name>')
        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