# 删除无关的XML文件和jpg文件 #所有的类型 # 需要识别的类型包括 bicycle,bus,car,person import os import re import shutil items=['aeroplane','bird','boat','bottle','cat','chair','cow','diningtable','dog','horse','motorbike','pottedplant','sheep','sofa','train','tvmonitor'] years=['VOC2007','VOC2012'] for item in items: pattern=r'[sS]*<name>'+item+'</name>[sS]*' for year in years: # imagedir=os.listdir('./VOCdevkit/'+year+'/JPEGImages') xmldirs=os.listdir('./VOCdevkit/'+year+'/Annotations') for xmldir in xmldirs: f=open('./VOCdevkit/'+year+'/Annotations/'+xmldir) xmlhasperson=False lines=f.readlines() for line in lines: if re.match(pattern, line,flags=0): xmlhasperson=True f.close() if xmlhasperson: os.remove('./VOCdevkit/'+year+'/Annotations/'+xmldir) os.remove('./VOCdevkit/'+year+'/JPEGImages/'+xmldir[:-4]+'.jpg') # person_list.append(xmldir[:-4]) # shutil.copyfile('./VOCdevkit/'+year+'/Annotations/'+xmldir,'./result/VOCdevkit/'+year+'/Annotations/'+xmldir) # shutil.copyfile('./VOCdevkit/'+year+'/JPEGImages/'+xmldir[:-4]+'.jpg','./result/VOCdevkit/'+year+'/JPEGImages/'+xmldir[:-4]+'.jpg')