6
6
import math
7
7
import random
8
8
9
+
10
+
9
11
def safeInt (ss ):
10
12
return int (float (ss ))
11
13
@@ -24,22 +26,64 @@ def str2bbx(self, str):
24
26
self .h = safeInt (chrs [4 ])
25
27
self .score = float (chrs [5 ])
26
28
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
+
27
40
def resize (self , scale , x_d , y_d ):
28
41
self .x = safeInt (self .x * scale ) + x_d
29
42
self .y = safeInt (self .y * scale ) + y_d
30
43
self .w = safeInt (self .w * scale )
31
44
self .h = safeInt (self .h * scale )
32
45
33
46
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
+
34
69
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
37
76
38
- def setBBXs (self , bboxs , name0 ):
77
+ def setBBXs (self , bboxs = None , names = None ):
39
78
self .bbxs = []
40
- for bbox in bboxs :
79
+ for i , bbox in enumerate (bboxs ):
80
+
41
81
bbx = BBX ()
42
- bbx .name = name0
82
+
83
+ if names == None :
84
+ bbx .name = None
85
+ else :
86
+ bbx .name = names [i ]
43
87
bbx .x = safeInt (bbox [0 ])
44
88
bbx .y = safeInt (bbox [1 ])
45
89
bbx .w = safeInt (bbox [2 ])
@@ -53,44 +97,63 @@ def showBBXs(self):
53
97
54
98
def saveBBXs (self , fileName ):
55
99
f = open (fileName , 'w' )
56
- f .write ('% bbGt version=3\n ' )
57
100
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 ))
59
102
f .close ()
60
103
61
- def drawOneBox (self , bbx , thr = - 1.0 ,showName = False ):
62
-
104
+ def drawOneBox (self , bbx , thr = - 1.0 , showName = False ):
63
105
if bbx .score >= thr :
64
106
x = bbx .x
65
107
y = bbx .y
66
108
w = bbx .w
67
109
h = bbx .h
110
+ # print x,y,w,h
68
111
line1 = ((x , y ), (x + w , y ), (x + w , y + h ), (x , y + h ), (x , y ))
69
112
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 )
74
121
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 ):
76
128
self .draw = ImageDraw .Draw (self .img )
77
129
for bbx in self .bbxs :
78
- self .drawOneBox (bbx , thr ,showName )
130
+ self .drawOneBox (bbx , thr , showName )
79
131
80
132
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' )
82
137
83
138
def read_ano (self , fileName ):
84
139
85
140
f = open (fileName , 'r' )
86
141
lines = f .readlines ()
87
142
self .bbxs = []
88
- for line in lines [1 :]:
143
+ for line in lines [:]:
89
144
nbbx = BBX ()
90
145
nbbx .str2bbx (line )
91
146
self .bbxs .append (nbbx )
92
147
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 )
94
157
95
158
def resizeBBXs (self , r , x_d , y_d ):
96
159
for bbx in self .bbxs :
@@ -127,7 +190,7 @@ def resize(self, width, height, scale=1.0):
127
190
self .resizeBBXs (re_ration , self .x_d , self .y_d )
128
191
# self.drawBox()
129
192
130
- def cleanAno (self ,w0 , h0 ):
193
+ def cleanAno (self , w0 , h0 ):
131
194
newBBXS = []
132
195
for bbox in self .bbxs :
133
196
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):
144
207
def save_img (self , imgName ):
145
208
self .img .save (imgName )
146
209
147
- def pureResize (self ,width , height ):
148
-
210
+ def pureResize (self , width , height ):
211
+ re_ration = float ( width ) / self . img . size [ 0 ]
149
212
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
+
150
223
151
224
152
225
if __name__ == '__main__' :
0 commit comments