-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
197 lines (182 loc) · 9.11 KB
/
pipeline.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
import cv2
import numpy as np
from rq import push_connection
import tldextract
from . import constants
from . import whitelist
db = constants.db
TTL = constants.general_ttl
q1 = constants.q1
nn_categories = [item for sector in constants.nn_categories.values() for item in sector]
pd_categories = [item for sector in constants.paperdoll_categories.values() for item in sector]
push_connection(constants.redis_conn)
# -----------------------------------------------CO-FUNCTIONS-----------------------------------------------------------
def is_in_whitelist(page_url):
page_domain = tldextract.extract(page_url).registered_domain
if page_domain not in whitelist.all_white_lists:
return False
else:
return True
def after_pd_conclusions(mask, labels, face=None):
"""
1. if there's a full-body clothing:
1.1 add to its' mask - all the rest lower body items' masks.
1.2 add other upper cover items if they pass the pixel-amount condition/
2. else -
2.1 lower-body: decide whether it's a pants, jeans.. or a skirt, and share masks
2.2 upper-body: decide whether it's a one-part or under & cover
3. return new mask - using same category values as incoming mask
"""
if face:
ref_area = face[2] * face[3]
y_split = face[1] + 3 * face[3]
else:
ref_area = (np.mean((mask.shape[0], mask.shape[1])) / 10) ** 2
y_split = np.round(0.4 * mask.shape[0])
final_mask = mask[:, :]
mask_sizes = {"upper_cover": [], "upper_under": [], "lower_cover": [], "lower_under": [], "whole_body": []}
for num in np.unique(mask):
item_mask = 255 * np.array(mask == num, dtype=np.uint8)
category = list(labels.keys())[list(labels.values()).index(num)]
# print "checking {0}".format(category)
for key, item in constants.paperdoll_categories.iteritems():
if category in item:
mask_sizes[key].append({num: cv2.countNonZero(item_mask)})
# 1
whole_sum = np.sum([item.values()[0] for item in mask_sizes['whole_body']])
partly_sum = np.sum([item.values()[0] for item in mask_sizes['upper_under']]) +\
np.sum([item.values()[0] for item in mask_sizes['lower_cover']])
if whole_sum > partly_sum:
max_amount = np.max([item.values()[0] for item in mask_sizes['whole_body']])
max_item_num = [item.keys()[0] for item in mask_sizes['whole_body'] if item.values()[0] == max_amount][0]
max_item_cat = list(labels.keys())[list(labels.values()).index(max_item_num)]
# print "W2P: That's a {0}".format(max_item_cat)
for num in np.unique(mask):
cat = list(labels.keys())[list(labels.values()).index(num)]
# 1.1, 1.2
if cat in constants.paperdoll_categories["lower_cover"] or \
cat in constants.paperdoll_categories["lower_under"] or \
cat in constants.paperdoll_categories["upper_under"]:
final_mask = np.where(mask == num, max_item_num, final_mask)
return final_mask
# 2, 2.1
sections = {"upper_cover": 0, "upper_under": 0, "lower_cover": 0, "lower_under": 0}
max_item_count = 0
max_cat = 9 #wtf is 9
# print "W2P: That's a 2-part clothing item!"
for section in sections.keys():
for item in mask_sizes[section]:
if item.values()[0] > max_item_count:
max_item_count = item.values()[0]
max_cat = item.keys()[0]
sections[section] = max_cat
# share masks
if max_item_count > 0:
for item in mask_sizes[section]:
cat = list(labels.keys())[list(labels.values()).index(item.keys()[0])]
# 2.1, 2.2
if cat in constants.paperdoll_categories[section]:
final_mask = np.where(mask == item.keys()[0], max_cat, final_mask)
max_item_count = 0
for item in mask_sizes['whole_body']:
for i in range(0, mask.shape[0]):
if i <= y_split:
for j in range(0, mask.shape[1]):
if mask[i][j] == item.keys()[0]:
final_mask[i][j] = sections["upper_under"] or sections["upper_cover"] or 0
else:
for j in range(0, mask.shape[1]):
if mask[i][j] == item.keys()[0]:
final_mask[i][j] = sections["lower_cover"] or sections["lower_under"] or 0
return final_mask
def after_nn_conclusions(mask, labels, face=None):
# Possible to add - remove coat from bottom of body
# remove holes in items
"""
0. threshold out small items
1. if there's a full-body clothing:
1.1 add to its' mask - all the rest lower body items' masks.
1.2 add other upper cover items if they pass the pixel-amount condition/
2. else -
2.1 lower-body: decide whether it's a pants, jeans.. or a skirt, and share masks
2.2 upper-body: decide whether it's a one-part or under & cover
3. return new mask
"""
if face:
y_split = face[1] + 3 * face[3]
else:
# BETTER TO SEND A FACE
y_split = np.round(0.4 * mask.shape[0])
final_mask = np.zeros(mask.shape, dtype=np.uint8)
mask_sizes = {"upper_cover": [], "upper_under": [], "lower_cover": [], "lower_under": [], "whole_body": []}
for num in np.unique(mask):
item_mask = 255 * np.array(mask == num, dtype=np.uint8)
category = list(labels.keys())[list(labels.values()).index(num)]
# print "W2P: checking {0}".format(category)
if category in nn_categories:
amount_of_category_pixels = cv2.countNonZero(item_mask)
sector = [key for key, value in constants.nn_categories.iteritems() if category in value][0]
mask_sizes[sector].append({num: amount_of_category_pixels})
# 1
whole_sum = np.sum([item.values()[0] for item in mask_sizes['whole_body']])
partly_sum = np.sum([item.values()[0] for item in mask_sizes['upper_under']]) +\
np.sum([item.values()[0] for item in mask_sizes['lower_cover']])
if whole_sum > partly_sum:
max_amount_whole = np.max([item.values()[0] for item in mask_sizes['whole_body']])
max_item_num_whole = [item.keys()[0] for item in mask_sizes['whole_body'] if item.values()[0] == max_amount_whole][0]
max_item_cat_whole = list(labels.keys())[list(labels.values()).index(max_item_num_whole)]
# print "W2P: That's a {0}".format(max_item_cat_whole)
for num in np.unique(mask):
cat = list(labels.keys())[list(labels.values()).index(num)]
# 1.1, 1.2
if cat in constants.nn_categories["lower_cover"] or \
cat in constants.nn_categories["lower_under"] or \
cat in constants.nn_categories["upper_under"] or \
cat in constants.nn_categories["whole_body"]:
final_mask = np.where(mask == num, max_item_num_whole, final_mask)
elif cat in constants.nn_categories["upper_cover"]:
max_amount_cover = np.max([item.values()[0] for item in mask_sizes['upper_cover']])
max_item_num_cover = [item.keys()[0] for item in mask_sizes['upper_cover'] if item.values()[0] == max_amount_cover][0]
item_mask = 255 * np.array(mask == num, dtype=np.uint8)
if float(cv2.countNonZero(item_mask))/mask.size > 0.01:
final_mask = np.where(mask == num, max_item_num_cover, final_mask)
else:
final_mask = np.where(mask == num, max_item_num_whole, final_mask)
return final_mask
# 2, 2.1
sections = {"upper_cover": 0, "upper_under": 0, "lower_cover": 0, "lower_under": 0}
max_item_count = 0
max_cat = 9
# print "W2P: That's a 2-part clothing item!"
for section in sections.keys():
for item in mask_sizes[section]:
if item.values()[0] > max_item_count:
max_item_count = item.values()[0]
max_cat = item.keys()[0]
sections[section] = max_cat
# share masks
if max_item_count > 0:
for item in mask_sizes[section]:
cat = list(labels.keys())[list(labels.values()).index(item.keys()[0])]
# 2.1, 2.2
if cat in constants.nn_categories[section]:
final_mask = np.where(mask == item.keys()[0], max_cat, final_mask)
max_item_count = 0
for item in mask_sizes['whole_body']:
for i in range(0, mask.shape[0]):
if i <= y_split:
for j in range(0, mask.shape[1]):
if mask[i][j] == item.keys()[0]:
final_mask[i][j] = sections["upper_under"] or sections["upper_cover"] or 0
else:
for j in range(0, mask.shape[1]):
if mask[i][j] == item.keys()[0]:
final_mask[i][j] = sections["lower_cover"] or sections["lower_under"] or 0
return final_mask
def set_collections(lang):
if not lang:
return 'images', 'products'
else:
products_collection = 'products_' + lang
images_collection = 'images_' + lang
return images_collection, products_collection