Skip to content

Commit c7a932c

Browse files
author
Shiyu Huang
committed
auto-push
1 parent 91242f5 commit c7a932c

File tree

1 file changed

+94
-21
lines changed

1 file changed

+94
-21
lines changed

image_pylib.py

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import math
77
import random
88

9+
10+
911
def safeInt(ss):
1012
return int(float(ss))
1113

@@ -24,22 +26,64 @@ def str2bbx(self, str):
2426
self.h = safeInt(chrs[4])
2527
self.score = float(chrs[5])
2628

29+
def str2bbx_true(self, str):
30+
chrs = str.split(' ')
31+
32+
self.name = chrs[0]
33+
34+
self.x = safeInt(chrs[1])
35+
self.y = safeInt(chrs[2])
36+
self.w = safeInt(chrs[3])
37+
self.h = safeInt(chrs[4])
38+
self.score = 1
39+
2740
def resize(self, scale, x_d, y_d):
2841
self.x = safeInt(self.x * scale) + x_d
2942
self.y = safeInt(self.y * scale) + y_d
3043
self.w = safeInt(self.w * scale)
3144
self.h = safeInt(self.h * scale)
3245

3346

47+
48+
class COLOR_CONF:
49+
def __init__(self,names = None,default_color = (255,0,0), default_font_size = 12, line_width = 1):
50+
self.colors = {}
51+
if names is not None:
52+
self.generate_colors(names)
53+
self.default_color = default_color
54+
self.default_font_size = default_font_size
55+
self.line_width = line_width
56+
def set_color(self,name,color):
57+
self.colors[name] = color
58+
59+
def generate_colors(self,names):
60+
for i in range(len(names)):
61+
self.colors[names[i]] = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
62+
63+
def get_color(self,name):
64+
if name in self.colors:
65+
return self.colors[name]
66+
else:
67+
return self.default_color
68+
3469
class IMGLIB:
35-
def __init__(self):
36-
pass
70+
def __init__(self,color_conf = None):
71+
if color_conf is None:
72+
default_color = (255,0,0)
73+
self.color_conf = COLOR_CONF(default_color=default_color)
74+
else:
75+
self.color_conf = color_conf
3776

38-
def setBBXs(self, bboxs, name0):
77+
def setBBXs(self, bboxs=None, names=None):
3978
self.bbxs = []
40-
for bbox in bboxs:
79+
for i, bbox in enumerate(bboxs):
80+
4181
bbx = BBX()
42-
bbx.name = name0
82+
83+
if names == None:
84+
bbx.name = None
85+
else:
86+
bbx.name = names[i]
4387
bbx.x = safeInt(bbox[0])
4488
bbx.y = safeInt(bbox[1])
4589
bbx.w = safeInt(bbox[2])
@@ -53,44 +97,63 @@ def showBBXs(self):
5397

5498
def saveBBXs(self, fileName):
5599
f = open(fileName, 'w')
56-
f.write('% bbGt version=3\n')
57100
for bbx in self.bbxs:
58-
f.write('%s %d %d %d %d %f 0 0 0 0 0 0\n' % (bbx.name, bbx.x, bbx.y, bbx.w, bbx.h, bbx.score))
101+
f.write('%s %d %d %d %d %f\n' % (bbx.name, bbx.x, bbx.y, bbx.w, bbx.h, bbx.score))
59102
f.close()
60103

61-
def drawOneBox(self, bbx, thr=-1.0,showName = False):
62-
104+
def drawOneBox(self, bbx, thr=-1.0, showName=False):
63105
if bbx.score >= thr:
64106
x = bbx.x
65107
y = bbx.y
66108
w = bbx.w
67109
h = bbx.h
110+
# print x,y,w,h
68111
line1 = ((x, y), (x + w, y), (x + w, y + h), (x, y + h), (x, y))
69112

70-
self.draw.line(line1, fill=(255, 0, 0))
71-
if showName:
72-
font = ImageFont.truetype("OpenSans-Regular.ttf", 20)
73-
self.draw.text((x, y - 25), str(bbx.score), fill=(255, 0, 0), font=font)
113+
fill_color = self.color_conf.get_color(bbx.name)
114+
# print line1
115+
# print fill_color
116+
# print self.color_conf.line_width
117+
self.draw.line(line1, fill=fill_color,width=self.color_conf.line_width)
118+
119+
if bbx.name == None or showName == False:
120+
font = ImageFont.truetype("OpenSans-Regular.ttf", self.color_conf.default_font_size)
74121

75-
def drawBox(self, thr=-1.0, showName = False):
122+
self.draw.text((x+self.color_conf.line_width, y), str(bbx.score), fill=fill_color, font=font)
123+
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)
126+
127+
def drawBox(self, thr=-1.0, showName=False):
76128
self.draw = ImageDraw.Draw(self.img)
77129
for bbx in self.bbxs:
78-
self.drawOneBox(bbx, thr,showName)
130+
self.drawOneBox(bbx, thr, showName)
79131

80132
def read_img(self, fileName):
81-
self.img = Image.open(fileName)
133+
self.img = Image.open(fileName).convert('RGB')
134+
135+
def read_gray_img(self, fileName):
136+
self.img = Image.open(fileName).convert('L')
82137

83138
def read_ano(self, fileName):
84139

85140
f = open(fileName, 'r')
86141
lines = f.readlines()
87142
self.bbxs = []
88-
for line in lines[1:]:
143+
for line in lines[:]:
89144
nbbx = BBX()
90145
nbbx.str2bbx(line)
91146
self.bbxs.append(nbbx)
92147

93-
# self.img.show()
148+
def read_ano_true(self, fileName):
149+
150+
f = open(fileName, 'r')
151+
lines = f.readlines()
152+
self.bbxs = []
153+
for line in lines[:]:
154+
nbbx = BBX()
155+
nbbx.str2bbx_true(line)
156+
self.bbxs.append(nbbx)
94157

95158
def resizeBBXs(self, r, x_d, y_d):
96159
for bbx in self.bbxs:
@@ -127,7 +190,7 @@ def resize(self, width, height, scale=1.0):
127190
self.resizeBBXs(re_ration, self.x_d, self.y_d)
128191
# self.drawBox()
129192

130-
def cleanAno(self,w0, h0):
193+
def cleanAno(self, w0, h0):
131194
newBBXS = []
132195
for bbox in self.bbxs:
133196
if bbox.x >= 0 and bbox.x <= w0 and bbox.y >= 0 and bbox.y <= h0 and bbox.w >= 20 and bbox.w <= w0 and bbox.h >= 30 and bbox.h <= h0:
@@ -144,9 +207,19 @@ def cleanAno(self,w0, h0):
144207
def save_img(self, imgName):
145208
self.img.save(imgName)
146209

147-
def pureResize(self,width, height):
148-
210+
def pureResize(self, width, height):
211+
re_ration = float(width)/self.img.size[0]
149212
self.img = self.img.resize((width, height), Image.ANTIALIAS)
213+
self.resizeBBXs(re_ration, 0, 0)
214+
215+
def flip(self, width):
216+
self.img = self.img.transpose(Image.FLIP_LEFT_RIGHT)
217+
newBBXS = []
218+
for bbox in self.bbxs:
219+
bbox.x = width - bbox.x - bbox.w
220+
newBBXS.append(bbox)
221+
self.bbxs = newBBXS
222+
150223

151224

152225
if __name__ == '__main__':

0 commit comments

Comments
 (0)