-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphObjectOrientation.py
182 lines (160 loc) · 7.9 KB
/
graphObjectOrientation.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
import os
import sys
import re
from BeautifulSoup import BeautifulSoup as bsoup
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
"""
Parses through all the xml files to return a list of objects within the file.
Returns a dict of lists which contains the number of orientations an object has. [Left, Right, Frontal, Rear, Unspecified]
"""
def parseFiles(annotationsPath,objectType):
orientationDict = {'car':[0,0,0,0,0],'person':[0,0,0,0,0],'bicycle':[0,0,0,0,0]}
tempType=''
# Creates two lists and an object in one list corresponds to the orientation in the other list based on position.
parsedObjectXMLList = []
parsedOrientationXMLList = []
# Retrieves all the files in a directory and checks if they are xml
fileList = os.listdir(annotationsPath)
annotationsFullPath = os.path.abspath(annotationsPath)
for file in fileList:
fileTypeMatch = re.search('.xml',file)
if fileTypeMatch:
print "Processing file: " + file
try:
filePath = os.path.join(annotationsFullPath, file)
f = open(filePath)
soup = bsoup(f)
f.close()
# Finds the object of all xml files and places the objects into a list
# and returns it.
parsedObjectXML = (soup.findAll('name'))
parsedOrientationXML = soup.pose.string
for object in parsedObjectXML:
match = re.search('(<name>)(\w+)(</name>)', str(object))
object = match.group(2)
if objectType == 'all':
parsedObjectXMLList += object,
parsedOrientationXMLList += str(parsedOrientationXML),
elif objectType == 'car' and object == 'car':
parsedObjectXMLList += object,
parsedOrientationXMLList += str(parsedOrientationXML),
elif objectType == 'person' and object == 'person':
parsedObjectXMLList += object,
parsedOrientationXMLList += str(parsedOrientationXML),
elif objectType == 'bicycle' and object == 'bicycle':
parsedObjectXMLList += object,
parsedOrientationXMLList += str(parsedOrientationXML),
except IOError:
sys.stderr.write('There was a problem with file: ' + file + '/n')
for x in range (0,len(parsedObjectXMLList)):
if objectType == 'all':
tempType = objectType
objectType = parsedObjectXMLList[x]
if parsedObjectXMLList[x] == objectType:
if parsedOrientationXMLList[x] == 'Left':
(orientationDict[objectType])[0]+=1
elif parsedOrientationXMLList[x] == 'Right':
(orientationDict[objectType])[1]+=1
elif parsedOrientationXMLList[x] == 'Frontal':
(orientationDict[objectType])[2]+=1
elif parsedOrientationXMLList[x] == 'Rear':
(orientationDict[objectType])[3]+=1
elif parsedOrientationXMLList[x] == 'Unspecified':
(orientationDict[objectType])[4]+=1
if tempType == 'all':
objectType = tempType
return orientationDict
"""
Counts the number of objects and returns a list.
The list is organized as these lines:
[Number of Cars, Number of Persons, Number of Bicycles]
"""
def createAllGraph(objectOrientationDict,outputPath):
N = 5
carOrientation = objectOrientationDict['car']
personOrientation = objectOrientationDict['person']
bicycleOrientation = objectOrientationDict['bicycle']
bicycleBarHeight = [0,0,0,0,0]
orientationNumber = [0,0,0,0,0]
for x in range (0,5):
height = (carOrientation[x] + personOrientation[x])
bicycleBarHeight[x] = height
orientationNumber[x] = (carOrientation[x] + personOrientation[x] + bicycleOrientation[x])
ind = np.arange(N) # the x locations for the groups
width = 0.50 # the width of the bars: can also be len(x) sequence
p1 = plt.bar(ind, carOrientation, width, color='r')
p2 = plt.bar(ind, personOrientation, width, color='#FFD700',bottom=carOrientation)
p3 = plt.bar(ind, bicycleOrientation, width, color='g',bottom=bicycleBarHeight)
plt.ylabel('The Number of Objects', fontsize=12, fontweight='bold')
plt.title('The Distribution of ' + str(sum(orientationNumber)) + ' Objects' + '\n' +'in the Database by Orientation.', fontsize=18, fontweight='bold')
plt.xticks(ind+width/2., ('Left - ' + str(orientationNumber[0]), 'Right - ' + str(orientationNumber[1]), 'Frontal - ' + str(orientationNumber[2]),'Rear - ' + str(orientationNumber[3]),'Unspecified - ' + str(orientationNumber[4])),fontsize=12, fontweight='bold' )
plt.legend( (p1[0], p2[0],p3[0]), ('Cars', 'Persons','Bicycles'),fancybox = True,shadow = True,frameon = True)
# Asks the user if he or she wants to save the file
userInput = ''
while userInput is not 'y' or 'n':
userInput = raw_input("Would you like to save the figure? (y/n) ")
userInput = userInput.lower()
if userInput == 'y':
path = os.path.abspath(outputPath)
filename = 'graphObjectOrientation_1.png'
fullpath = os.path.join(path, filename)
plt.savefig(fullpath)
break
elif userInput == 'n':
break
else:
print "Please answer y or n. " '\n'
plt.show()
def createSingleObjectPieChart(objectOrientationDict,outputPath,objectType):
userInput=''
orientationList = objectOrientationDict[objectType]
labels = 'Left - ' + str(orientationList[0]), 'Right - ' + str(orientationList[1]), 'Frontal - ' + str(orientationList[2]), 'Rear - ' + str(orientationList[3]), 'Unspecified - ' + str(orientationList[4])
sizes = objectOrientationDict[objectType]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'orange','red']
plt.subplots_adjust(top=0.85)
plt.title('The Distribution of ' + str(sum(orientationList)) + ' '+ objectType[0].upper() + objectType[1:] + ' Object by Orientation.',fontsize=16, fontweight='bold')
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=45)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
while userInput is not 'y' or 'n':
userInput = raw_input("Would you like to save the figure? (y/n) ")
userInput = userInput.lower()
if userInput == 'y':
path = os.path.abspath(outputPath)
filename = 'graphObjectOrientation_Piechart.png'
fullpath = os.path.join(path, filename)
plt.savefig(fullpath)
break
elif userInput == 'n':
break
else:
print "Please answer y or n. " '\n'
plt.show()
"""
Creates a graph of object classes by reading files from the
directory,annotationsPath and outputs it to outputPath
"""
def graphObjectOrientation(annotationsPath, outputPath,objectType):
objectOrientationDict = parseFiles(annotationsPath,objectType)
if objectType == 'all':
createAllGraph(objectOrientationDict,outputPath)
else:
createSingleObjectPieChart(objectOrientationDict,outputPath,objectType)
"""
Given the path, it opens the all the xml files.
Use matlab to create a graph to a certain locations.
Create your own directory if not present.
Accepting the argument.
"""
def main():
annotationsPath = raw_input("Path to the annotations?: ")
outputPath = raw_input("Output Path?: ")
objectType = raw_input("What type of object would you like? (car/person/bicycle/all): ")
if os.path.exists(annotationsPath) and os.path.exists(outputPath):
graphObjectOrientation(annotationsPath,outputPath,objectType)
if __name__ == '__main__':
main()