forked from RonaldsonBellande/ML_object_classification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlParser.py
More file actions
executable file
·34 lines (32 loc) · 1.4 KB
/
xmlParser.py
File metadata and controls
executable file
·34 lines (32 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import csv
import xml.etree.ElementTree as ET
def parseDir(directory):
annotationDir = os.path.join(directory, 'Annotations')
imagesDir = os.path.join(directory, 'JPEGImages')
csvFile = os.path.join(directory, 'simpleAnnotations.csv')
with open(csvFile, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['FileName','Class'])
for filename in os.listdir(annotationDir):
if filename.endswith(".xml"):
currentFile = os.path.join(annotationDir, filename)
print(currentFile)
tree = ET.parse(currentFile)
root = tree.getroot()
imageName = root.find('filename').text
obj = root.find('object')
className = obj.find('name').text
currentImage = os.path.join(imagesDir, imageName)
newDir = os.path.join(imagesDir, className)
newImageName = os.path.join(newDir, imageName)
if not os.path.exists(newDir):
os.makedirs(newDir)
os.rename(currentImage, newImageName)
writer.writerow([imageName,className])
else:
continue
directory1 = r'PascalVOC2012/voc2012/VOC2012'
directory2 = r'PascalVOC2012/VOC2012'
parseDir(directory1)
parseDir(directory2)