-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImageServer.py
252 lines (188 loc) · 9.25 KB
/
ImageServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from scipy import ndimage
import numpy as np
import utils
import cPickle as pickle
import glob
from os import path
class ImageServer(object):
def __init__(self, imgSize=[112, 112], frameFraction=0.25, initialization='box', color=False):
self.origLandmarks = []
self.filenames = []
self.mirrors = []
self.meanShape = np.array([])
self.meanImg = np.array([])
self.stdDevImg = np.array([])
self.perturbations = []
self.imgSize = imgSize
self.frameFraction = frameFraction
self.initialization = initialization
self.color = color;
self.boundingBoxes = []
@staticmethod
def Load(filename):
imageServer = ImageServer()
arrays = np.load(filename)
imageServer.__dict__.update(arrays)
if(len(imageServer.imgs.shape)==3):
imageServer.imgs = imageServer.imgs[:, np.newaxis]
return imageServer
def Save(self, datasetDir, filename=None):
if filename is None:
filename = "dataset_nimgs={0}_perturbations={1}_size={2}".format(len(self.imgs), list(self.perturbations), self.imgSize)
if self.color:
filename += "_color={0}".format(self.color)
filename += ".npz"
arrays = {key:value for key, value in self.__dict__.items() if not key.startswith('__') and not callable(key)}
np.savez(datasetDir + filename, **arrays)
def PrepareData(self, imageDirs, boundingBoxFiles, meanShape, startIdx, nImgs, mirrorFlag):
filenames = []
landmarks = []
boundingBoxes = []
for i in range(len(imageDirs)):
filenamesInDir = glob.glob(imageDirs[i] + "*.jpg")
filenamesInDir += glob.glob(imageDirs[i] + "*.png")
if boundingBoxFiles is not None:
boundingBoxDict = pickle.load(open(boundingBoxFiles[i], 'rb'))
for j in range(len(filenamesInDir)):
filenames.append(filenamesInDir[j])
ptsFilename = filenamesInDir[j][:-3] + "pts"
landmarks.append(utils.loadFromPts(ptsFilename))
if boundingBoxFiles is not None:
basename = path.basename(filenamesInDir[j])
boundingBoxes.append(boundingBoxDict[basename])
filenames = filenames[startIdx : startIdx + nImgs]
landmarks = landmarks[startIdx : startIdx + nImgs]
boundingBoxes = boundingBoxes[startIdx : startIdx + nImgs]
mirrorList = [False for i in range(nImgs)]
if mirrorFlag:
mirrorList = mirrorList + [True for i in range(nImgs)]
filenames = np.concatenate((filenames, filenames))
landmarks = np.vstack((landmarks, landmarks))
boundingBoxes = np.vstack((boundingBoxes, boundingBoxes))
self.origLandmarks = landmarks
self.filenames = filenames
self.mirrors = mirrorList
self.meanShape = meanShape
self.boundingBoxes = boundingBoxes
def LoadImages(self):
self.imgs = []
self.initLandmarks = []
self.gtLandmarks = []
for i in range(len(self.filenames)):
img = ndimage.imread(self.filenames[i])
if self.color:
if len(img.shape) == 2:
img = np.dstack((img, img, img))
else:
if len(img.shape) > 2:
img = np.mean(img, axis=2)
img = img.astype(np.uint8)
if self.mirrors[i]:
self.origLandmarks[i] = utils.mirrorShape(self.origLandmarks[i], img.shape)
img = np.fliplr(img)
if self.color:
img = np.transpose(img, (2, 0, 1))
else:
img = img[np.newaxis]
groundTruth = self.origLandmarks[i]
if self.initialization == 'rect':
bestFit = utils.bestFitRect(groundTruth, self.meanShape)
elif self.initialization == 'similarity':
bestFit = utils.bestFit(groundTruth, self.meanShape)
elif self.initialization == 'box':
bestFit = utils.bestFitRect(groundTruth, self.meanShape, box=self.boundingBoxes[i])
self.imgs.append(img)
self.initLandmarks.append(bestFit)
self.gtLandmarks.append(groundTruth)
self.initLandmarks = np.array(self.initLandmarks)
self.gtLandmarks = np.array(self.gtLandmarks)
def GeneratePerturbations(self, nPerturbations, perturbations):
self.perturbations = perturbations
meanShapeSize = max(self.meanShape.max(axis=0) - self.meanShape.min(axis=0))
destShapeSize = min(self.imgSize) * (1 - 2 * self.frameFraction)
scaledMeanShape = self.meanShape * destShapeSize / meanShapeSize
newImgs = []
newGtLandmarks = []
newInitLandmarks = []
translationMultX, translationMultY, rotationStdDev, scaleStdDev = perturbations
rotationStdDevRad = rotationStdDev * np.pi / 180
translationStdDevX = translationMultX * (scaledMeanShape[:, 0].max() - scaledMeanShape[:, 0].min())
translationStdDevY = translationMultY * (scaledMeanShape[:, 1].max() - scaledMeanShape[:, 1].min())
print "Creating perturbations of " + str(self.gtLandmarks.shape[0]) + " shapes"
for i in range(self.initLandmarks.shape[0]):
print(i)
for j in range(nPerturbations):
tempInit = self.initLandmarks[i].copy()
angle = np.random.normal(0, rotationStdDevRad)
offset = [np.random.normal(0, translationStdDevX), np.random.normal(0, translationStdDevY)]
scaling = np.random.normal(1, scaleStdDev)
R = np.array([[np.cos(angle), -np.sin(angle)],[np.sin(angle), np.cos(angle)]])
tempInit = tempInit + offset
tempInit = (tempInit - tempInit.mean(axis=0)) * scaling + tempInit.mean(axis=0)
tempInit = np.dot(R, (tempInit - tempInit.mean(axis=0)).T).T + tempInit.mean(axis=0)
tempImg, tempInit, tempGroundTruth = self.CropResizeRotate(self.imgs[i], tempInit, self.gtLandmarks[i])
newImgs.append(tempImg)
newInitLandmarks.append(tempInit)
newGtLandmarks.append(tempGroundTruth)
self.imgs = np.array(newImgs)
self.initLandmarks = np.array(newInitLandmarks)
self.gtLandmarks = np.array(newGtLandmarks)
def CropResizeRotateAll(self):
newImgs = []
newGtLandmarks = []
newInitLandmarks = []
for i in range(self.initLandmarks.shape[0]):
tempImg, tempInit, tempGroundTruth = self.CropResizeRotate(self.imgs[i], self.initLandmarks[i], self.gtLandmarks[i])
newImgs.append(tempImg)
newInitLandmarks.append(tempInit)
newGtLandmarks.append(tempGroundTruth)
self.imgs = np.array(newImgs)
self.initLandmarks = np.array(newInitLandmarks)
self.gtLandmarks = np.array(newGtLandmarks)
def NormalizeImages(self, imageServer=None):
self.imgs = self.imgs.astype(np.float32)
if imageServer is None:
self.meanImg = np.mean(self.imgs, axis=0)
else:
self.meanImg = imageServer.meanImg
self.imgs = self.imgs - self.meanImg
if imageServer is None:
self.stdDevImg = np.std(self.imgs, axis=0)
else:
self.stdDevImg = imageServer.stdDevImg
self.imgs = self.imgs / self.stdDevImg
from matplotlib import pyplot as plt
meanImg = self.meanImg - self.meanImg.min()
meanImg = 255 * meanImg / meanImg.max()
meanImg = meanImg.astype(np.uint8)
if self.color:
plt.imshow(np.transpose(meanImg, (1, 2, 0)))
else:
plt.imshow(meanImg[0], cmap=plt.cm.gray)
plt.savefig("../meanImg.jpg")
plt.clf()
stdDevImg = self.stdDevImg - self.stdDevImg.min()
stdDevImg = 255 * stdDevImg / stdDevImg.max()
stdDevImg = stdDevImg.astype(np.uint8)
if self.color:
plt.imshow(np.transpose(stdDevImg, (1, 2, 0)))
else:
plt.imshow(stdDevImg[0], cmap=plt.cm.gray)
plt.savefig("../stdDevImg.jpg")
plt.clf()
def CropResizeRotate(self, img, initShape, groundTruth):
meanShapeSize = max(self.meanShape.max(axis=0) - self.meanShape.min(axis=0))
destShapeSize = min(self.imgSize) * (1 - 2 * self.frameFraction)
scaledMeanShape = self.meanShape * destShapeSize / meanShapeSize
destShape = scaledMeanShape.copy() - scaledMeanShape.mean(axis=0)
offset = np.array(self.imgSize[::-1]) / 2
destShape += offset
A, t = utils.bestFit(destShape, initShape, True)
A2 = np.linalg.inv(A)
t2 = np.dot(-t, A2)
outImg = np.zeros((img.shape[0], self.imgSize[0], self.imgSize[1]), dtype=img.dtype)
for i in range(img.shape[0]):
outImg[i] = ndimage.interpolation.affine_transform(img[i], A2, t2[[1, 0]], output_shape=self.imgSize)
initShape = np.dot(initShape, A) + t
groundTruth = np.dot(groundTruth, A) + t
return outImg, initShape, groundTruth