-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphObjectTags.py
126 lines (116 loc) · 4.75 KB
/
graphObjectTags.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
#!/usr/bin/python
import os
import sys
import re
from BeautifulSoup import BeautifulSoup as bsoup
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def countList(tagList):
countedList = [0,0,0,0]
for tag in tagList:
if tag == 'none':
countedList[0] += 1
if tag == 'truncated':
countedList[1] += 1
if tag == 'truncated and occluded':
countedList[2] += 1
if tag == 'occluded':
countedList[3] += 1
return countedList
def addToTagList(soup):
truncation = int(soup.truncated.string)
occlusion = int(soup.occluded.string)
if truncation and occlusion:
return('truncated and occluded')
elif occlusion:
return('occluded')
elif truncation:
return('truncated')
else:
return('none')
def parseFiles(annotationsPath,objectType):
tagList = []
# Retrieves all the files in a directory and checks if they are xml
annotationsFullPath = os.path.abspath(annotationsPath)
fileList = os.listdir(annotationsFullPath)
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.
parsedXML = (soup.findAll('name'))
if objectType == 'all':
for object in parsedXML:
tagList.append(addToTagList(soup))
elif objectType in ('car','person','bicycle'):
for object in parsedXML:
match = re.search('(<name>)(\w+)(</name>)', str(object))
if match.group(2) == objectType:
tagList.append(addToTagList(soup))
except IOError:
sys.stderr.write('There was a problem with file: ' + file + '\n')
return tagList
def calculatePercentage(countedList):
percentageList = []
sum = 0.0
for category in countedList:
sum += category
for objectTag in countedList:
quotient = (objectTag/sum)*100
percentageList.append(round(quotient,4))
return percentageList
def createPieChart(countedList,objectType,percentageList,outputPath):
# The slices will be ordered and plotted counter-clockwise.
labels = 'None - ' + str(countedList[0]), 'Truncated - ' + str(countedList[1]), 'Truncated and Occluded - ' + str(countedList[2]), 'Occluded - ' + str(countedList[3])
sizes = percentageList
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
plt.title('The Distribution of ' + str(sum(countedList)) + ' ' + objectType[0].upper() + str(objectType[1:]) + ' Object by Tag',fontsize=18, fontweight='bold')
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=0)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
#
# # 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 = 'objectTagsFigure_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 graphObjectTags(annotationsPath,outputPath,objectType='all'):
tagList = parseFiles(annotationsPath,objectType)
countedList = countList(tagList)
percentageList = calculatePercentage(countedList)
createPieChart(countedList,objectType,percentageList,outputPath)
"""
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("Which object tags would you like? (all/car/person/bicycle): ")
if objectType in ('car','person','bicycle'):
graphObjectTags(annotationsPath,outputPath,objectType=objectType)
elif objectType == 'all':
graphObjectTags(annotationsPath,outputPath)
if __name__ == '__main__':
main()