Skip to content

Commit 1f49aa2

Browse files
author
Shiyu Huang
committed
update
1 parent fc21aa9 commit 1f49aa2

File tree

6 files changed

+94
-66
lines changed

6 files changed

+94
-66
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
.idea/

.idea/dictionaries/shiyuhuang.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

1_resized.jpg

41.2 KB
Loading

1_resized.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
% bbGt version=3
2+
person 244 146 120 375 0.000000 0 0 0 0 0 0

image_pylib.py

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import inspect
22
import os
3-
from PIL import Image,ImageDraw,ImageFont
3+
from PIL import Image, ImageDraw, ImageFont
44
import numpy as np
55
import time
66
import math
77
import random
88

9+
910
class BBX:
1011
def __init__(self):
1112
pass
12-
def str2bbx(self,str):
13+
14+
def str2bbx(self, str):
1315
chrs = str.split(' ')
1416

1517
self.name = chrs[0]
16-
18+
1719
self.x = int(chrs[1])
1820
self.y = int(chrs[2])
1921
self.w = int(chrs[3])
2022
self.h = int(chrs[4])
21-
self.score=float(0)
23+
self.score = float(chrs[5])
2224

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)
2830

29-
30-
31-
3231

3332
class IMGLIB:
3433
def __init__(self):
3534
pass
36-
def setBBXs(self,bboxs,name0):
35+
36+
def setBBXs(self, bboxs, name0):
3737
self.bbxs = []
3838
for bbox in bboxs:
3939
bbx = BBX()
@@ -48,98 +48,113 @@ def setBBXs(self,bboxs,name0):
4848
def showBBXs(self):
4949
self.drawBox()
5050
self.img.show()
51-
def saveBBXs(self,fileName):
52-
f = open(fileName,'w')
51+
52+
def saveBBXs(self, fileName):
53+
f = open(fileName, 'w')
5354
f.write('% bbGt version=3\n')
5455
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))
5657
f.close()
5758

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)
7375
for bbx in self.bbxs:
74-
self.drawOneBox(bbx)
76+
self.drawOneBox(bbx, thr)
7577

7678
def read_img(self, fileName):
7779
self.img = Image.open(fileName)
7880

7981
def read_ano(self, fileName):
80-
81-
f = open(fileName,'r')
82+
83+
f = open(fileName, 'r')
8284
lines = f.readlines()
8385
self.bbxs = []
8486
for line in lines[1:]:
8587
nbbx = BBX()
8688
nbbx.str2bbx(line)
87-
self.bbxs.append(nbbx)
88-
89-
#self.img.show()
89+
self.bbxs.append(nbbx)
9090

91-
def resizeBBXs(self,r,x_d,y_d):
91+
# self.img.show()
92+
93+
def resizeBBXs(self, r, x_d, y_d):
9294
for bbx in self.bbxs:
93-
bbx.resize(r,x_d,y_d)
95+
bbx.resize(r, x_d, y_d)
9496

95-
def resize(self, width, height,scale = 1.0):
97+
def resize(self, width, height, scale=1.0):
9698
o_width, o_height = self.img.size
9799
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)
99104

100-
o_ratio = o_width/float(o_height)
101-
n_ratio = width/float(height)
102-
103105
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)
106108
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)
108110
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)
111113
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)
113115

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))
116118
imgNew = Image.new("RGB", (width, height), "black")
117119

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))
122124
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)
124126
# self.drawBox()
125127

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+
126142
def save_img(self, imgName):
127143
self.img.save(imgName)
128144

145+
129146
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'
135151
imgWidth = 960
136-
imgHeight= 720
152+
imgHeight = 720
137153
imglib = IMGLIB()
138154

139155
imglib.read_img(imageName)
140156
imglib.read_ano(anoName)
141-
imglib.resize(imgWidth,imgHeight,0.85)
157+
imglib.resize(imgWidth, imgHeight, 0.85)
142158
imglib.drawBox()
143159
imglib.save_img(saveImgName)
144-
imglib.savaBBXs(saveAnoName)
145-
160+
imglib.saveBBXs(saveAnoName)

0 commit comments

Comments
 (0)