import os
import shutil
import xml.etree.ElementTree as ET
import pdb
#shutil.rmtree(os.getcwd()+"/VOC2007");
os.mkdir (os.getcwd()+"/VOC2007");
os.mkdir (os.getcwd()+"/VOC2007/Annotations_");
os.mkdir (os.getcwd()+"/VOC2007/Annotations");
os.mkdir (os.getcwd()+"/VOC2007/ImageSets");
os.mkdir (os.getcwd()+"/VOC2007/ImageSets/Main");
os.mkdir (os.getcwd()+"/VOC2007/JPEGImages");
def object_name( dirName, fileName ):
tree = ET.parse(dirName + fileName)
root = tree.getroot()
#print(root)
#filename = root.find('path').text
#filename = filename[:-4]
#print (filename)
for object in root.findall('object'): #找到root节点下的所有object节点
object.find('name').text = "obj"
#for files in my_files:
# if "lab" in root.find('object').find('name').text:
# root.find('object').find('name').text = "obj"
#obj = root.find('object').find('name').text
#obj = obj[:-4]
#print (obj)
#print('\n')
#root[0].text = "VOC2007"
#root[1].text = root[1].text + ".jpg"
tree.write(os.getcwd()+"/VOC2007/Annotations/" + fileName)
#print(root[1].text)
print('\n')
#print(root[6].text)
#pdb.set_trace()
my_files = os.listdir(os.getcwd()+"/");
for files in my_files:
if "xml" in files:
print(files)
print('\n')
print(os.getcwd()+"/")
object_name(os.getcwd()+"/", files)
my_files = os.listdir(os.getcwd()+"/");
for files in my_files:
if "jpg" in files:
print(os.getcwd() + "/" + files)
print('\n')
print(os.getcwd()+"/VOC2007/JPEGImages/" + files)
#pdb.set_trace()
shutil.copyfile(os.getcwd() + "/" + files, os.getcwd()+"/VOC2007/JPEGImages/" + files)
for files in my_files:
if "xml" in files:
shutil.copyfile(os.getcwd() + "/" + files, os.getcwd()+"/VOC2007/Annotations_/" + files)
#my_files = os.listdir("jpg");
#print(my_files)
listFiles = []
for files in my_files:
if "jpg" in files:
#print(files)
listFiles.append(files[:-4])
listFiles.sort()
fo = open(os.getcwd()+"/VOC2007/ImageSets/Main/" + "trainval.txt", "w")
for fileName in listFiles:
fo.write(fileName + "\n")
print(fileName)
fo.close()
fo = open(os.getcwd()+"/VOC2007/ImageSets/Main/" + "test.txt", "w")
for fileName in listFiles:
fo.write(fileName + "\n")
print(fileName)
fo.close()
转载请注明: http://blog.csdn.net/forest_world