voc 에 필요 한 분류 추출 및 클래스 이름 변경

4767 단어 코드
1. 필요 한 카 테 고리 추출
https://blog.csdn.net/qq_35239859/article/details/88787554

import os
import shutil
ann_filepath='C:\\Users\\Administrator\\Desktop\\VOC2007\\Annotations\\'
img_filepath='C:\\Users\\Administrator\\Desktop\\VOC2007\\JPEGImages\\'
img_savepath='C:\\Users\\Administrator\\Desktop\\VOC2007\\JPEGImages_person\\'
ann_savepath='C:\\Users\\Administrator\\Desktop\\VOC2007\\Annotations_person\\'

if not os.path.exists(img_savepath):
    os.mkdir(img_savepath)
 
if not os.path.exists(ann_savepath):
    os.mkdir(ann_savepath)
names = locals()
classes = ['aeroplane','bicycle','bird', 'boat', 'bottle',
           'bus', 'car', 'cat', 'chair', 'cow','diningtable',
           'dog', 'horse', 'motorbike', 'pottedplant',
           'sheep', 'sofa', 'train', 'tvmonitor', 'person']
 
 
for file in os.listdir(ann_filepath):
    print(file)
    fp = open(ann_filepath + '\\' + file)
    ann_savefile=ann_savepath+file
    fp_w = open(ann_savefile, 'w')
    lines = fp.readlines()
 
    ind_start = []
    ind_end = []
    lines_id_start = lines[:]
    lines_id_end = lines[:]
 
    classes = '\t\tperson
' # xml object , while "\t
" in lines_id_start: a = lines_id_start.index("\t
") ind_start.append(a) lines_id_start[a] = "delete" while "\t

" in lines_id_end: b = lines_id_end.index("\t

") ind_end.append(b) lines_id_end[b] = "delete" #names object i = 0 for k in range(0, len(ind_start)): names['block%d' % k] = [] for j in range(0, len(classes)): if classes[j] in lines[ind_start[i] + 1]: a = ind_start[i] for o in range(ind_end[i] - ind_start[i] + 1): names['block%d' % k].append(lines[a + o]) break i += 1 #xml string_start = lines[0:ind_start[0]] #xml string_end = [lines[len(lines) - 1]] # , , object a = 0 for k in range(0, len(ind_start)): if classes in names['block%d' % k]: a += 1 string_start += names['block%d' % k] string_start += string_end for c in range(0, len(string_start)): fp_w.write(string_start[c]) fp_w.close() # , xml, if a == 0: os.remove(ann_savepath+file) else: name_img = img_filepath + os.path.splitext(file)[0] + ".jpg" shutil.copy(name_img, img_savepath) fp.close()

질문:
Traceback (most recent call last):   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\xml_name.py", line 31, in     changexml(inputpath)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\xml_name.py", line 10, in changexml     tree = ET.parse(file)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\xml\etree\ElementTree.py", line 1195, in parse     tree.parse(source, parser)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\xml\etree\ElementTree.py", line 596, in parse     self._root = parser._parse_whole(source)   File "", line None xml.etree.ElementTree.ParseError: mismatched tag: line 53, column 3
원인:
클래스 의 시작 과 끝 탭 에 문제 가 발생 했 습 니 다. (탭 은 쌍 으로 나타 나 지 않 습 니 다)
2. xml 의 특정한 종류의 이름 을 수정 합 니 다.
import os
import xml.etree.ElementTree as ET

#    :    VOC    xml         
def changelabelname(inputpath):
    listdir = os.listdir(inputpath)
    for file in listdir:
        if file.endswith('xml'):
            file = os.path.join(inputpath,file)
            tree = ET.parse(file)
            root = tree.getroot()
            for object1 in root.findall('object'):
                for sku in object1.findall('name'):           #         
                    if (sku.text == 'person'):               #‘preName’       
                        sku.text = 'headshoulder'                 #‘TESTNAME’       
                        tree.write(file,encoding='utf-8')     #     xml       xml      
                    else:
                        pass
        else:
            pass

if __name__ == '__main__':
    inputpath = 'C:\\Users\\Administrator\\Desktop\\Annotations_person\\'  #          
    changelabelname(inputpath)

좋은 웹페이지 즐겨찾기