forked from lozuwa/impy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageAnnotation.py
executable file
·83 lines (73 loc) · 2.16 KB
/
ImageAnnotation.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
74
75
76
77
78
79
80
81
82
83
"""
Author: Rodrigo Loza
Email: [email protected]
Description: Decomposes the information contained in an image
annotation with the VOC format.
"""
import os
from interface import implements
import xml.etree.ElementTree as ET
class ImageAnnotation(object):
def __init__(self, path = None):
super(ImageAnnotation, self).__init__()
# Assertions
if (path == None):
raise ValueError("Path parameter cannot be empty.")
if (not os.path.isfile(path)):
raise ValueError("Path parameter does not exist: ".format(path))
# Class variables
self.path = path
self.root = self.readImageAnnotation(self.path)
self.size = self.getSize(self.root)
self.objects = self.getObjects(self.root)
self.names = self.getNames(self.objects)
self.boundingBoxes = self.getBoundingBoxes(self.objects)
@property
def propertySize(self):
return self.size
@property
def propertyObjects(self):
return self.objects
@property
def propertyNames(self):
return self.names
@property
def propertyBoundingBoxes(self):
return self.boundingBoxes
def readImageAnnotation(self, path = None):
tree = ET.parse(path)
root = tree.getroot()
return root
def getObjects(self, root = None):
if (root.find("object")):
objects = root.findall("object")
return objects
else:
print("WARNING: No objects found.")
return []
def getNames(self, objects = None):
names = []
for obj in objects:
names.append(obj.find("name").text)
return names
def getBoundingBoxes(self, objects = None):
boundingBoxes = []
for i in range(len(objects)):
# Find bndbox
coordinates = objects[i].find("bndbox")
# Get coordinates
xmin = int(coordinates.find("xmin").text)
xmax = int(coordinates.find("xmax").text)
ymin = int(coordinates.find("ymin").text)
ymax = int(coordinates.find("ymax").text)
boundingBoxes.append([xmin, ymin, xmax, ymax])
return boundingBoxes
def getSize(self, root = None):
if (root.find("size")):
size = root.find("size")
height = int(size.find("height").text)
width = int(size.find("width").text)
depth = int(size.find("depth").text)
return [height, width, depth]
else:
raise Exception("No size found in {}".format(self.path))