-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.py
353 lines (275 loc) · 10.7 KB
/
utils.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from helper import *
import numpy as np
import cv2
from tqdm import tqdm
import glob
import os.path as osp
import random
from PIL import Image
from scipy import ndimage
import tensorflow as tf
from spatial_transformer import transformer
import numpy as np
from tf_utils import weight_variable, bias_variable, dense_to_one_hot
import matplotlib.pyplot as plt
def RandomCenterCrop(path, min_size, max_size):
'''
simulate dataset step 1: Crop Randomly
'''
size = np.random.randint(min_size, max_size)
img = cv2.imread(path)
h, w, _ = img.shape
top = np.random.randint(0, h - size)
left = np.random.randint(0, w - size)
return img[top:size+top, left:size+left, :]
def get_patch(path, min_patch_size, max_patch_size):
'''
get patch from clothes
'''
patch_size = np.random.randint(min_patch_size, max_patch_size)
img = cv2.imread(path)
h, w, _ = img.shape
center_h = h/2
center_w = w/2
patch = img[int(center_h - patch_size/2):int(center_h + patch_size/2), int(center_w - patch_size/2):int(center_w + patch_size/2), :]
return patch
def edge_detecton(path):
'''
get sketch
'''
from_mat = cv2.imread(path)
width = float(from_mat.shape[1])
height = float(from_mat.shape[0])
new_width = 0
new_height = 0
if (width > height):
from_mat = cv2.resize(from_mat, (512, int(512 / width * height)), interpolation=cv2.INTER_AREA)
new_width = 512
new_height = int(512 / width * height)
else:
from_mat = cv2.resize(from_mat, (int(512 / height * width), 512), interpolation=cv2.INTER_AREA)
new_width = int(512 / height * width)
new_height = 512
from_mat = from_mat.transpose((2, 0, 1))
light_map = np.zeros(from_mat.shape, dtype=np.float)
for channel in range(3):
light_map[channel] = get_light_map_single(from_mat[channel])
light_map = normalize_pic(light_map)
light_map = resize_img_512_3d(light_map)
line_mat = mod.predict(light_map, batch_size=1)
line_mat = line_mat.transpose((3, 1, 2, 0))[0]
line_mat = line_mat[0:int(new_height), 0:int(new_width), :]
#sketchKeras_colored = show_active_img_and_save('sketchKeras_colored', line_mat, 'sketchKeras_colored.jpg')
line_mat = np.amax(line_mat, 2)
#sketchKeras_enhanced = show_active_img_and_save_denoise_filter2('sketchKeras_enhanced', line_mat, 'sketchKeras_enhanced.jpg')
#sketchKeras_pured = show_active_img_and_save_denoise_filter('sketchKeras_pured', line_mat, 'sketchKeras_pured.jpg')
sketchKeras = show_active_img_and_save_denoise('sketchKeras', line_mat, 'sketchKeras.jpg')
cv2.waitKey(0)
return sketchKeras
def get_mask(path):
'''
提取衣服的mask
返回numpy数组
'''
from linefiller.trappedball_fill import trapped_ball_fill_multi, flood_fill_multi, mark_fill, build_fill_map, merge_fill, \
show_fill_map
from linefiller.thinning import thinning
im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
ret, binary = cv2.threshold(im, 220, 255, cv2.THRESH_BINARY)
fills = []
result = binary
fill = trapped_ball_fill_multi(result, 3, method='max')
fills += fill
result = mark_fill(result, fill)
fill = trapped_ball_fill_multi(result, 2, method=None)
fills += fill
result = mark_fill(result, fill)
fill = trapped_ball_fill_multi(result, 1, method=None)
fills += fill
result = mark_fill(result, fill)
fill = flood_fill_multi(result)
fills += fill
fillmap = build_fill_map(result, fills)
fillmap = merge_fill(fillmap)
for i in range(len(fillmap[:,0])):
for j in range(len(fillmap[0,:])):
if fillmap[i,j] == 1:
fillmap[i,j] = 0
else:
fillmap[i,j] = 1
return fillmap
from linefiller.trappedball_fill import trapped_ball_fill_multi, flood_fill_multi, mark_fill, build_fill_map, merge_fill, \
show_fill_map
from linefiller.thinning import thinning
def get_region_picture(path):
'''
获取不规则形状的图片,背景是黑色0,方便rotate
'''
im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
ret, binary = cv2.threshold(im, 220, 255, cv2.THRESH_BINARY)
fills = []
result = binary
fill = trapped_ball_fill_multi(result, 3, method='max')
fills += fill
result = mark_fill(result, fill)
fill = trapped_ball_fill_multi(result, 2, method=None)
fills += fill
result = mark_fill(result, fill)
fill = trapped_ball_fill_multi(result, 1, method=None)
fills += fill
result = mark_fill(result, fill)
fill = flood_fill_multi(result)
fills += fill
fillmap = build_fill_map(result, fills)
fillmap = merge_fill(fillmap)
fillmap = thinning(fillmap)
#获得region mask
for i in range(len(fillmap[:,0])):
for j in range(len(fillmap[0,:])):
if fillmap[i,j] == 0:
fillmap[i,j] = 1
else:
fillmap[i,j] = 0
#获得region picture
im = cv2.imread(path)
# plt.imshow(im)
rgb_fillmap = np.zeros(im.shape)
rgb_fillmap[:,:,0] = fillmap
rgb_fillmap[:,:,1] = fillmap
rgb_fillmap[:,:,2] = fillmap
im = im * rgb_fillmap
return im.astype('uint8')
def Random_paste_patch_img(ori_img, patch_img):
paste_x = np.random.randint(0, ori_img.size[0] - patch_img.size[0])
paste_y = np.random.randint(0, ori_img.size[1] - patch_img.size[1])
rotate_angle = np.random.randint(1, 359)
resize_x = np.random.randint(64, 384)
resize_y = np.random.randint(64, 384)
patch_img = patch_img.resize((resize_x,resize_y))
tem = ori_img.copy()
tem.paste(patch_img.rotate(rotate_angle),(paste_x,paste_y))
tem = np.array(tem)
ori_img = np.array(ori_img)
# for i in range(ori_img.shape[0]):
# for j in range(ori_img.shape[1]):
# if (tem[i,j,:] == np.array([0,0,0])).all():
# tem[i,j,:] = ori_img[i,j,:]
coordinate = np.where(tem == np.array([0,0,0]))
for i in range(len(coordinate[0])):
tem[coordinate[0][i],coordinate[1][i],:] = ori_img[coordinate[0][i],coordinate[1][i],:]
ori_img = np.array(tem)
ori_img = Image.fromarray(ori_img)
# plt.imshow(ori_img)
return ori_img
def Random_paste_region_img(ori_img, region_img):
paste_x = np.random.randint(0, ori_img.size[0])
paste_y = np.random.randint(0, ori_img.size[1])
rotate_angle = np.random.randint(1, 359)
resize_x = np.random.randint(64, 384)
resize_y = np.random.randint(64, 384)
region_img = region_img.resize((resize_x,resize_y))
tem = ori_img.copy()
tem.paste(region_img.rotate(rotate_angle),(paste_x,paste_y))
tem = np.array(tem)
ori_img = np.array(ori_img)
# for i in range(ori_img.shape[0]):
# for j in range(ori_img.shape[1]):
# if (tem[i,j,:] == np.array([0,0,0])).all():
# tem[i,j,:] = ori_img[i,j,:]
coordinate = np.where(tem == np.array([0,0,0]))
for i in range(len(coordinate[0])):
tem[coordinate[0][i],coordinate[1][i],:] = ori_img[coordinate[0][i],coordinate[1][i],:]
ori_img = np.array(tem)
ori_img = Image.fromarray(ori_img)
# plt.imshow(ori_img)
return ori_img
def get_STL(path, num_batch):
h = 1000
w = 700
im = cv2.imread(path[0])
im = im / 255.
# h = im.shape[0]
# w = im.shape[1]
im = cv2.resize(im, (w, h), interpolation=cv2.INTER_CUBIC)
im = im.reshape(1, h, w, 3)
im = im.astype('float32')
batch = np.append(im, im, axis=0)
for p in path:
im = cv2.imread(p)
im = im / 255.
# h = im.shape[0]
# w = im.shape[1]
im = cv2.resize(im, (w, h), interpolation=cv2.INTER_CUBIC)
im = im.reshape(1, h, w, 3)
im = im.astype('float32')
batch = np.append(batch, im, axis=0)
# print(batch.shape)
batch = batch[2:,:,:,:]
# print(batch.shape)
out_size = (h, w)
# %% Simulate batch
# batch = np.append(im, im, axis=0)
# batch.append(im)
# batch = np.append(batch, im, axis=0)
# num_batch = 1
x = tf.placeholder(tf.float32, [None, h, w, 3])
x = tf.cast(batch, 'float32')
# %% Create localisation network and convolutional layer
with tf.variable_scope('spatial_transformer_0'):
# %% Create a fully-connected layer with 6 output nodes
n_fc = 6
W_fc1 = tf.Variable(tf.zeros([h * w * 3, n_fc]), name='W_fc1')
# %% Zoom into the image
a = np.random.randint(5, 10)/10
b = np.random.randint(0, 3)/10
c = np.random.randint(0, 3)/10
d = np.random.randint(5, 10)/10
# initial = np.array([[s, 0, tx], [0, s,ty]])
initial = np.array([[a, b, 0], [b, d, 0]])
initial = initial.astype('float32')
initial = initial.flatten()
b_fc1 = tf.Variable(initial_value=initial, name='b_fc1')
h_fc1 = tf.matmul(tf.zeros([num_batch, h * w * 3]), W_fc1) + b_fc1
h_trans = transformer(x, h_fc1, out_size)
# %% Run session
sess = tf.Session()
sess.run(tf.initialize_all_variables())
y = sess.run(h_trans, feed_dict={x: batch})
# y = batch
return y
#提取图片主要色
import colorsys
def get_dominant_color(image):
#颜色模式转换,以便输出rgb颜色值
image = image.convert('RGBA')
#生成缩略图,减少计算量,减小cpu压力
image.thumbnail((200, 200))
max_score = 0#原来的代码此处为None
dominant_color = 0#原来的代码此处为None,但运行出错,改为0以后 运行成功,原因在于在下面的 score > max_score的比较中,max_score的初始格式不定
for count, (r, g, b, a) in image.getcolors(image.size[0] * image.size[1]):
# 跳过纯黑色
if a == 0:
continue
saturation = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)[1]
y = min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235)
y = (y - 16.0) / (235 - 16)
# 忽略高亮色
if y > 0.9:
continue
# 忽略白背景
if ((r>230)&(g>230)&(b>230)):
continue
# Calculate the score, preferring highly saturated colors.
# Add 0.1 to the saturation so we don't completely ignore grayscale
# colors by multiplying the count by zero, but still give them a low
# weight.
score = (saturation + 0.1) * count
if score > max_score:
max_score = score
dominant_color = (r, g, b)
return dominant_color
def min_dis(point, point_list):
dis = []
for p in point_list:
dis.append(np.sqrt(np.sum(np.square(np.array(point)-np.array(p)))))
return min(dis)