forked from giovannicimolin/PascalVOC-to-Images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (60 loc) · 2.21 KB
/
main.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import glob
import os
import xmltodict
import json
import pprint
from PIL import Image
# Printing results
pp = pprint.PrettyPrinter(indent=4)
# Look for XML files and parses then as if they were Pascal VOC Files
def process():
# Finds all XML files on data/ and append to list
pascal_voc_contents = []
os.chdir("data")
print("Found {} files in data directory!".format(
str(len(glob.glob("*.xml")))))
for file in glob.glob("*.xml"):
f_handle = open(file, 'r')
print("Parsing file '{}'...".format(file))
pascal_voc_contents.append(xmltodict.parse(f_handle.read()))
# Process each file individually
for index in pascal_voc_contents:
image_file = index['annotation']['filename']
# If there's a corresponding file in the folder,
# process the images and save to output folder
if os.path.isfile(image_file):
extractDataset(index['annotation'])
else:
print("Image file '{}' not found, skipping file...".format(image_file))
# Extract image samples and save to output dir
def extractDataset(dataset):
print("Found {} objects on image '{}'...".format(
len(dataset['object']), dataset['filename']))
# Open image and get ready to process
img = Image.open(dataset['filename'])
# Create output directory
save_dir = dataset['filename'].split('.')[0]
try:
os.mkdir(save_dir)
except:
pass
# Image name preamble
sample_preamble = save_dir + "/" + dataset['filename'].split('.')[0] + "_"
# Image counter
i = 0
# Run through each item and save cut image to output folder
for item in dataset['object']:
# Convert str to integers
bndbox = dict([(a, int(b)) for (a, b) in item['bndbox'].items()])
# Crop image
im = img.crop((bndbox['xmin'], bndbox['ymin'],
bndbox['xmax'], bndbox['ymax']))
# Save
im.save(sample_preamble + str(i) + '.jpg')
i = i + 1
if __name__ == '__main__':
print("\n------------------------------------")
print("----- PascalVOC-to-Images v0.1 -----")
print("Created by Giovanni Cimolin da Silva")
print("------------------------------------\n")
process()