Skip to content

Commit 5dd3283

Browse files
author
Shiyu Huang
committed
auto-push
1 parent e0cc9e3 commit 5dd3283

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

image_pylib.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def __init__(self,color_conf = None):
7474
else:
7575
self.color_conf = color_conf
7676

77+
FontData = os.path.join(os.path.dirname(os.path.realpath(__file__)), "OpenSans-Regular.ttf")
78+
self.font = ImageFont.truetype(FontData, self.color_conf.default_font_size)
79+
7780
def setBBXs(self, bboxs=None, names=None):
7881
self.bbxs = []
7982
for i, bbox in enumerate(bboxs):
@@ -117,18 +120,32 @@ def drawOneBox(self, bbx, thr=-1.0, showName=False):
117120
self.draw.line(line1, fill=fill_color,width=self.color_conf.line_width)
118121

119122
if bbx.name == None or showName == False:
120-
font = ImageFont.truetype("OpenSans-Regular.ttf", self.color_conf.default_font_size)
121-
122-
self.draw.text((x+self.color_conf.line_width, y), str(bbx.score), fill=fill_color, font=font)
123+
self.draw.text((x+self.color_conf.line_width, y), str(bbx.score), fill=fill_color, font=self.font)
123124
else:
124-
font = ImageFont.truetype("OpenSans-Regular.ttf", self.color_conf.default_font_size)
125-
self.draw.text((x+self.color_conf.line_width, y), bbx.name + ' ' + str(bbx.score), fill=fill_color, font=font)
125+
self.draw.text((x+self.color_conf.line_width, y), bbx.name + ' ' + str(bbx.score), fill=fill_color, font=self.font)
126+
127+
def drawOneBoxTrue(self, bbx, showName=False):
128+
x = bbx.x
129+
y = bbx.y
130+
w = bbx.w
131+
h = bbx.h
132+
line1 = ((x, y), (x + w, y), (x + w, y + h), (x, y + h), (x, y))
133+
fill_color = self.color_conf.get_color(bbx.name)
134+
self.draw.line(line1, fill=fill_color,width=self.color_conf.line_width)
135+
if bbx.name == None or showName == False:
136+
self.draw.text((x+self.color_conf.line_width, y), 'True', fill=fill_color, font=self.font)
137+
else:
138+
self.draw.text((x+self.color_conf.line_width, y), bbx.name + '_True', fill=fill_color, font=self.font)
126139

127-
def drawBox(self, thr=-1.0, showName=False):
140+
def drawBox(self, thr=-1.0, showName=True, show_true = True):
128141
self.draw = ImageDraw.Draw(self.img)
129142
for bbx in self.bbxs:
130143
self.drawOneBox(bbx, thr, showName)
131144

145+
if show_true and hasattr(self,'bbxs_true'):
146+
for bbx in self.bbxs_true:
147+
self.drawOneBoxTrue(bbx, showName)
148+
132149
def read_img(self, fileName):
133150
self.img = Image.open(fileName).convert('RGB')
134151

@@ -149,11 +166,11 @@ def read_ano_true(self, fileName):
149166

150167
f = open(fileName, 'r')
151168
lines = f.readlines()
152-
self.bbxs = []
169+
self.bbxs_true = []
153170
for line in lines[:]:
154171
nbbx = BBX()
155172
nbbx.str2bbx_true(line)
156-
self.bbxs.append(nbbx)
173+
self.bbxs_true.append(nbbx)
157174

158175
def resizeBBXs(self, r, x_d, y_d):
159176
for bbx in self.bbxs:
@@ -187,7 +204,8 @@ def resize(self, width, height, scale=1.0):
187204

188205
imgNew.paste(region, (self.x_d, self.y_d))
189206
self.img = imgNew
190-
self.resizeBBXs(re_ration, self.x_d, self.y_d)
207+
if hasattr(self,'bbxs'):
208+
self.resizeBBXs(re_ration, self.x_d, self.y_d)
191209
# self.drawBox()
192210

193211
def cleanAno(self, w0, h0):
@@ -210,7 +228,8 @@ def save_img(self, imgName):
210228
def pureResize(self, width, height):
211229
re_ration = float(width)/self.img.size[0]
212230
self.img = self.img.resize((width, height), Image.ANTIALIAS)
213-
self.resizeBBXs(re_ration, 0, 0)
231+
if hasattr(self,'bbxs'):
232+
self.resizeBBXs(re_ration, 0, 0)
214233

215234
def flip(self, width):
216235
self.img = self.img.transpose(Image.FLIP_LEFT_RIGHT)

0 commit comments

Comments
 (0)