1
1
import inspect
2
2
import os
3
- from PIL import Image ,ImageDraw ,ImageFont
3
+ from PIL import Image , ImageDraw , ImageFont
4
4
import numpy as np
5
5
import time
6
6
import math
7
7
import random
8
8
9
+
9
10
class BBX :
10
11
def __init__ (self ):
11
12
pass
12
- def str2bbx (self ,str ):
13
+
14
+ def str2bbx (self , str ):
13
15
chrs = str .split (' ' )
14
16
15
17
self .name = chrs [0 ]
16
-
18
+
17
19
self .x = int (chrs [1 ])
18
20
self .y = int (chrs [2 ])
19
21
self .w = int (chrs [3 ])
20
22
self .h = int (chrs [4 ])
21
- self .score = float (0 )
23
+ self .score = float (chrs [ 5 ] )
22
24
23
- def resize (self , scale ,x_d ,y_d ):
24
- self .x = int (self .x * scale )+ x_d
25
- self .y = int (self .y * scale )+ y_d
26
- self .w = int (self .w * scale )
27
- self .h = int (self .h * scale )
25
+ def resize (self , scale , x_d , y_d ):
26
+ self .x = int (self .x * scale ) + x_d
27
+ self .y = int (self .y * scale ) + y_d
28
+ self .w = int (self .w * scale )
29
+ self .h = int (self .h * scale )
28
30
29
-
30
-
31
-
32
31
33
32
class IMGLIB :
34
33
def __init__ (self ):
35
34
pass
36
- def setBBXs (self ,bboxs ,name0 ):
35
+
36
+ def setBBXs (self , bboxs , name0 ):
37
37
self .bbxs = []
38
38
for bbox in bboxs :
39
39
bbx = BBX ()
@@ -48,98 +48,113 @@ def setBBXs(self,bboxs,name0):
48
48
def showBBXs (self ):
49
49
self .drawBox ()
50
50
self .img .show ()
51
- def saveBBXs (self ,fileName ):
52
- f = open (fileName ,'w' )
51
+
52
+ def saveBBXs (self , fileName ):
53
+ f = open (fileName , 'w' )
53
54
f .write ('% bbGt version=3\n ' )
54
55
for bbx in self .bbxs :
55
- 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 ))
56
+ 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 ))
56
57
f .close ()
57
58
58
- def drawOneBox (self ,bbx ):
59
- x = bbx .x
60
- y = bbx .y
61
- w = bbx .w
62
- h = bbx .h
63
- line1 = ((x ,y ),(x + w ,y ),(x + w ,y + h ),(x ,y + h ),(x ,y ))
64
-
65
- self .draw .line ( line1 , fill = (255 ,0 ,0 ))
66
-
67
- font = ImageFont .truetype ("OpenSans-Regular.ttf" , 20 )
68
- self .draw .text ((x ,y - 25 ),bbx .name + ' ' + str (bbx .score ), fill = (255 ,0 ,0 ),font = font )
69
-
70
-
71
- def drawBox (self ):
72
- self .draw = ImageDraw .Draw (self .img )
59
+ def drawOneBox (self , bbx , thr = - 1.0 ):
60
+
61
+ if bbx .score >= thr :
62
+ x = bbx .x
63
+ y = bbx .y
64
+ w = bbx .w
65
+ h = bbx .h
66
+ line1 = ((x , y ), (x + w , y ), (x + w , y + h ), (x , y + h ), (x , y ))
67
+
68
+ self .draw .line (line1 , fill = (255 , 0 , 0 ))
69
+
70
+ font = ImageFont .truetype ("OpenSans-Regular.ttf" , 20 )
71
+ self .draw .text ((x , y - 25 ), str (bbx .score ), fill = (255 , 0 , 0 ), font = font )
72
+
73
+ def drawBox (self , thr = - 1.0 ):
74
+ self .draw = ImageDraw .Draw (self .img )
73
75
for bbx in self .bbxs :
74
- self .drawOneBox (bbx )
76
+ self .drawOneBox (bbx , thr )
75
77
76
78
def read_img (self , fileName ):
77
79
self .img = Image .open (fileName )
78
80
79
81
def read_ano (self , fileName ):
80
-
81
- f = open (fileName ,'r' )
82
+
83
+ f = open (fileName , 'r' )
82
84
lines = f .readlines ()
83
85
self .bbxs = []
84
86
for line in lines [1 :]:
85
87
nbbx = BBX ()
86
88
nbbx .str2bbx (line )
87
- self .bbxs .append (nbbx )
88
-
89
- #self.img.show()
89
+ self .bbxs .append (nbbx )
90
90
91
- def resizeBBXs (self ,r ,x_d ,y_d ):
91
+ # self.img.show()
92
+
93
+ def resizeBBXs (self , r , x_d , y_d ):
92
94
for bbx in self .bbxs :
93
- bbx .resize (r ,x_d ,y_d )
95
+ bbx .resize (r , x_d , y_d )
94
96
95
- def resize (self , width , height ,scale = 1.0 ):
97
+ def resize (self , width , height , scale = 1.0 ):
96
98
o_width , o_height = self .img .size
97
99
t_width = int (width * scale )
98
- t_height = int (height * scale )
100
+ t_height = int (height * scale )
101
+
102
+ o_ratio = o_width / float (o_height )
103
+ n_ratio = width / float (height )
99
104
100
- o_ratio = o_width / float (o_height )
101
- n_ratio = width / float (height )
102
-
103
105
if o_ratio > n_ratio :
104
- re_ration = t_width / float (o_width )
105
- a_height = int (re_ration * o_height )
106
+ re_ration = t_width / float (o_width )
107
+ a_height = int (re_ration * o_height )
106
108
a_width = t_width
107
- self .img = self .img .resize ((a_width ,a_height ),Image .ANTIALIAS )
109
+ self .img = self .img .resize ((a_width , a_height ), Image .ANTIALIAS )
108
110
else :
109
- re_ration = t_height / float (o_height )
110
- a_width = int (re_ration * o_width )
111
+ re_ration = t_height / float (o_height )
112
+ a_width = int (re_ration * o_width )
111
113
a_height = t_height
112
- self .img = self .img .resize ( (a_width ,a_height ),Image .ANTIALIAS )
114
+ self .img = self .img .resize ((a_width , a_height ), Image .ANTIALIAS )
113
115
114
- self .x_d = random .randint (0 , abs (a_width - width ) )
115
- self .y_d = random .randint (0 , abs (a_height - height ) )
116
+ self .x_d = random .randint (0 , abs (a_width - width ))
117
+ self .y_d = random .randint (0 , abs (a_height - height ))
116
118
imgNew = Image .new ("RGB" , (width , height ), "black" )
117
119
118
- box = (0 ,0 , a_width ,a_height )
119
- region = self .img .crop (box )
120
-
121
- imgNew .paste (region , (self .x_d ,self .y_d ) )
120
+ box = (0 , 0 , a_width , a_height )
121
+ region = self .img .crop (box )
122
+
123
+ imgNew .paste (region , (self .x_d , self .y_d ))
122
124
self .img = imgNew
123
- self .resizeBBXs (re_ration ,self .x_d ,self .y_d )
125
+ self .resizeBBXs (re_ration , self .x_d , self .y_d )
124
126
# self.drawBox()
125
127
128
+ def cleanAno (self ,w0 , h0 ):
129
+ newBBXS = []
130
+ for bbox in self .bbxs :
131
+ 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 :
132
+ bbx = BBX ()
133
+ bbx .name = bbox .name
134
+ bbx .x = bbox .x
135
+ bbx .y = bbox .y
136
+ bbx .w = bbox .w
137
+ bbx .h = bbox .h
138
+ bbx .score = bbox .score
139
+ newBBXS .append (bbx )
140
+ self .bbxs = newBBXS
141
+
126
142
def save_img (self , imgName ):
127
143
self .img .save (imgName )
128
144
145
+
129
146
if __name__ == '__main__' :
130
-
131
- imageName = '23.jpg'
132
- anoName = '23.txt'
133
- saveImgName = '23_resized.jpg'
134
- saveAnoName = '23_resized.txt'
147
+ imageName = '/Users/shiyuhuang/Downloads/ATOCAR/ATOCAR_UNITY_2/ATOCAR_UNITY/imgSaveDir/11.jpg'
148
+ anoName = '/Users/shiyuhuang/Downloads/ATOCAR/ATOCAR_UNITY_2/ATOCAR_UNITY/anoSaveDir/11.txt'
149
+ saveImgName = '1_resized.jpg'
150
+ saveAnoName = '1_resized.txt'
135
151
imgWidth = 960
136
- imgHeight = 720
152
+ imgHeight = 720
137
153
imglib = IMGLIB ()
138
154
139
155
imglib .read_img (imageName )
140
156
imglib .read_ano (anoName )
141
- imglib .resize (imgWidth ,imgHeight ,0.85 )
157
+ imglib .resize (imgWidth , imgHeight , 0.85 )
142
158
imglib .drawBox ()
143
159
imglib .save_img (saveImgName )
144
- imglib .savaBBXs (saveAnoName )
145
-
160
+ imglib .saveBBXs (saveAnoName )
0 commit comments