-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotGridAndBound.py
85 lines (56 loc) · 2.25 KB
/
plotGridAndBound.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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 9 20:37:12 2019
@author: nithish k
"""
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
from XMLParser import parseXMLtoDict
def addBoundingBox(plt,objectList,dispClassLabel =True):
"""
args: plt object , objectList
return: adds bounding boxes
"""
ax = plt.gca()
for eachObj in objectList:
xLeft,yLeft = eachObj['xmin'],eachObj['ymin']
objWidth = eachObj['xmax'] - eachObj['xmin']
objHeight = eachObj['ymax'] - eachObj['ymin']
centerX = int((eachObj['xmax'] + eachObj['xmin'])/2)
centerY = int((eachObj['ymax'] + eachObj['ymin'])/2)
rect = patches.Rectangle((xLeft,yLeft),objWidth,objHeight,
linewidth=2,edgecolor='r',facecolor='none')
plt.scatter(x=[centerX], y=[centerY], c='r', s=10)
if dispClassLabel == True:
plt.text(centerX, centerY, eachObj['name'], fontsize = 15 ,color = 'red')
ax.add_patch(rect)
return plt
def plotGridOnImg(filepath,numXGrids,numYGrids,objectList,dispClassLabel= True,grid = True):
"""
"""
plt.clf()
img = plt.imread(filepath)
# img = np.array(img)
yPixels,xPixels,channels = img.shape
xSteps,ySteps = xPixels//numXGrids , yPixels//numYGrids
# print(xSteps,ySteps)
if grid == True:
## horizontal lines
for xCordLine in range(0,xPixels,xSteps):
plt.axvline(x=xCordLine,linewidth = 2)
### vertical lines
for yCordLine in range(0,yPixels,ySteps):
plt.axhline(y=yCordLine,linewidth = 2)
addBoundingBox(plt,objectList,dispClassLabel)
#
ax = plt.gca()
# ax.figure.figimage(img,0,0)
# ax.add_image(img)
plt.imshow(img)
return plt
if __name__ == '__main__':
filepath = "C:/Users/ntihish/Pictures/lions-cubs-kenya_53922_990x742.jpg"
imageDict, objectList = parseXMLtoDict("C:/Users/ntihish/Documents/IUB/Deep Learning/Project/Train images/annotations/xmls/Arla-Ecological-Medium-Fat-Milk_001.xml")
gridImg = plotGridOnImg(filepath,6,6,objectList)
gridImg.savefig("griddedImage")