zjuJish commited on
Commit
1f25d5b
·
verified ·
1 Parent(s): fbc778e

Upload VITON-HD/extract.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. VITON-HD/extract.py +216 -0
VITON-HD/extract.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # extract the key phrases of garments from the sentences
2
+ # data_path = '/mnt/workspace/workgroup/sihui.jsh/VITON-HD/try'
3
+ # data_path = '/mnt/workspace/workgroup/sihui.jsh/humanparsing'
4
+ data_path = '/mnt/workspace/workgroup/sihui.jsh/SHHQ'
5
+ # data_path = '/mnt/workspace/workgroup/sihui.jsh/dresscode/dresses'
6
+ # data_path = '/mnt/workspace/workgroup/sihui.jsh/deepfashion'
7
+
8
+ # define the key words
9
+ from tqdm import tqdm
10
+ import os
11
+ colors = ['black', 'white', 'grey', 'khaki', 'burgundy']
12
+ colors = ['red', 'blue', 'green', 'yellow', 'orange', 'purple',
13
+ 'pink', 'black', 'white', 'brown', 'gray', 'teal', 'navy', 'maroon',
14
+ 'olive', 'cyan', 'magenta', 'beige', 'turquoise', 'lavender', 'coral', 'peach',
15
+ 'gold', 'silver', 'indigo', 'violet', 'chartreuse', 'khaki', 'salmon', 'auburn',
16
+ 'plum', 'ivory', 'taupe', 'fuchsia', 'cerulean', 'tan', 'russet', 'sapphire',
17
+ 'ruby', 'emerald', 'amber', 'burgundy', 'charcoal', 'taupe', 'mauve', 'periwinkle',
18
+ 'mustard', 'vermillion', 'cobalt', 'crimson', 'lilac', 'jade', 'khaki', 'grey']
19
+
20
+ upper = ['T-shirt', 'T-shirts', 't-shirt', 'shirt', 'tee', 'top', 'jacket', 'sweatshirt', 'coat', 'suit',
21
+ 'blouse', 'hoodie', 'sweater', 'cardigan', 'vest', 'tunic', 'dress', 'outfit', 'bodysuit', 'TEE', 'T恤', 'gown']
22
+
23
+ pants = ['jeans', 'trousers', 'pants', 'shorts', 'skirt',
24
+ 'leggings', 'sweatpants', 'joggers', 'jogger']
25
+
26
+ shoes = ['sneakers', 'boots', 'sandals', 'shoes', 'shoe', 'heels', 'flats', 'loafers', 'oxfords',
27
+ 'moccasins', 'slippers', 'wedges', 'clogs', 'espadrilles',
28
+ 'pumps', 'platforms', 'slides', 'brogues', 'flip-flops', 'booties',
29
+ 'stilettos', 'derbies', 'monks', 'chukkas', 'slingbacks', 'mules', 'maryjanes', 'slingbacks', 'trainers']
30
+ with open(os.path.join(data_path, 'data.txt'), 'r') as file1,open(os.path.join(data_path,'data_chunk.txt'), 'a') as file2:
31
+ texts = file1.readlines()
32
+ pbar = tqdm(enumerate(texts),total=len(texts))
33
+ for cnt, line in pbar:
34
+ # if cnt==10:
35
+ # break
36
+ id, sentence = line.split('\t')
37
+ items = {'upper': '*', 'pants': '*', 'shoes': '*'}
38
+
39
+ # if id != 'MEN_Denim_id_00000080_01_2_side.png':
40
+ # continue
41
+ # extract the upper garments phrases
42
+ for word in upper:
43
+ if word in sentence:
44
+ # get the position of the word
45
+ pos = sentence.find(word)
46
+ # starting from the word, find the first word 'a' or 'an' before it
47
+ start_A = sentence.rfind('A ', 0, pos)
48
+ start_a = sentence.rfind(' a ', 0, pos)
49
+ start_an = sentence.rfind(' an ', 0, pos)
50
+ start = max(start_A, start_a, start_an)
51
+ # collect the phrase
52
+ if start != -1:
53
+ # find if these are any comma in between
54
+ # comma = sentence.find(',', start, pos)
55
+ # if comma != -1:
56
+ # phrase = sentence[comma+1:pos+len(word)]
57
+ # else:
58
+ phrase = sentence[start:pos+len(word)]
59
+ # filter out extra words
60
+ wearing = phrase.find('wearing')
61
+ if wearing != -1:
62
+ phrase = phrase[wearing+8:]
63
+ man = phrase.find('man')
64
+ if man != -1:
65
+ phrase = phrase[man+4:]
66
+ woman = phrase.find('woman')
67
+ if woman != -1:
68
+ phrase = phrase[woman+6:]
69
+ phrase = phrase.strip()
70
+ # if start with 'with' or 'in', remove them
71
+ if phrase.startswith('with'):
72
+ phrase = phrase[4:]
73
+ elif phrase.startswith('in'):
74
+ phrase = phrase[2:]
75
+ phrase = phrase.strip()
76
+ else:
77
+ phrase = word
78
+
79
+ # eliminate the extra spaces
80
+ phrase = phrase.strip()
81
+ items['upper'] = phrase
82
+ break
83
+
84
+ for word in pants:
85
+ if word in sentence:
86
+ pos = sentence.find(word)
87
+ start_A = sentence.rfind('A ', 0, pos)
88
+ start_a = sentence.rfind(' a ', 0, pos)
89
+ start_an = sentence.rfind(' an ', 0, pos)
90
+ start = max(start_A, start_a, start_an)
91
+ if start != -1:
92
+ # filter out the comma
93
+ comma = sentence.rfind(',', start, pos)
94
+ if comma != -1:
95
+ phrase = sentence[comma+1:pos+len(word)]
96
+ else:
97
+ phrase = sentence[start:pos+len(word)]
98
+ # filter out extra words
99
+ wearing = phrase.find('wearing')
100
+ if wearing != -1:
101
+ phrase = phrase[wearing+8:]
102
+ man = phrase.find('man')
103
+ if man != -1:
104
+ phrase = phrase[man+4:]
105
+ woman = phrase.find('woman')
106
+ if woman != -1:
107
+ phrase = phrase[woman+6:]
108
+ phrase = phrase.strip()
109
+ # if start with 'with' or 'in', remove them
110
+ if phrase.startswith('with'):
111
+ phrase = phrase[4:]
112
+ elif phrase.startswith('in'):
113
+ phrase = phrase[2:]
114
+ phrase = phrase.strip()
115
+ # filter out upper garments
116
+ for word in upper:
117
+ upper_pos = phrase.find(word)
118
+ if upper_pos != -1:
119
+ phrase = phrase[upper_pos+len(word):]
120
+ break
121
+ phrase = phrase.strip()
122
+ # if start with 'and', remove it
123
+ if phrase.startswith('and'):
124
+ phrase = phrase[3:]
125
+ phrase = phrase.strip()
126
+ else:
127
+ phrase = word
128
+ # look back to see if there is any abjective
129
+ for color in colors:
130
+ color_pos = sentence.rfind(color, 0, pos)
131
+ if color_pos != -1:
132
+ phrase = sentence[color_pos:pos+len(word)]
133
+ break
134
+ # print(id, phrase)
135
+
136
+ phrase = phrase.strip()
137
+ items['pants'] = phrase
138
+ break
139
+
140
+ # like pants
141
+ for word in shoes:
142
+ if word in sentence:
143
+ pos = sentence.find(word)
144
+ start_A = sentence.rfind('A ', 0, pos)
145
+ start_a = sentence.rfind(' a ', 0, pos)
146
+ start_an = sentence.rfind(' an ', 0, pos)
147
+ start = max(start_A, start_a, start_an)
148
+ if start != -1:
149
+ comma = sentence.rfind(',', start, pos)
150
+ if comma != -1:
151
+ phrase = sentence[comma+1:pos+len(word)]
152
+ else:
153
+ phrase = sentence[start:pos+len(word)]
154
+ phrase = phrase.strip()
155
+ # filter out extra words
156
+ wearing = phrase.find('wearing')
157
+ if wearing != -1:
158
+ phrase = phrase[wearing+8:]
159
+ wears = phrase.find('wears')
160
+ if wears != -1:
161
+ phrase = phrase[wears+6:]
162
+ man = phrase.find('man')
163
+ if man != -1:
164
+ phrase = phrase[man+4:]
165
+ woman = phrase.find('woman')
166
+ if woman != -1:
167
+ phrase = phrase[woman+6:]
168
+ phrase = phrase.strip()
169
+ # if start with 'with' or 'in', remove them
170
+ if phrase.startswith('with'):
171
+ phrase = phrase[4:]
172
+ elif phrase.startswith('in'):
173
+ phrase = phrase[2:]
174
+ phrase = phrase.strip()
175
+ # filter out upper garments
176
+ for word in upper:
177
+ upper_pos = phrase.find(word)
178
+ if upper_pos != -1:
179
+ phrase = phrase[upper_pos+len(word):]
180
+ break
181
+ # filter out pants
182
+ for word in pants:
183
+ pants_pos = phrase.find(word)
184
+ if pants_pos != -1:
185
+ phrase = phrase[pants_pos+len(word):]
186
+ break
187
+ phrase = phrase.strip()
188
+ # if start with 'and', remove it
189
+ if phrase.startswith('and'):
190
+ phrase = phrase[3:]
191
+ phrase = phrase.strip()
192
+ else:
193
+ phrase = word
194
+ # look back to see if there is any abjective
195
+ for color in colors:
196
+ color_pos = sentence.rfind(color, 0, pos)
197
+ if color_pos != -1:
198
+ phrase = sentence[color_pos:pos+len(word)]
199
+ break
200
+ # print(id, phrase)
201
+
202
+ phrase = phrase.strip()
203
+ items['shoes'] = phrase
204
+ # print(phrase)
205
+ break
206
+
207
+ # flag = False
208
+ # for key in items:
209
+ # if items[key] != '':
210
+ # flag = True
211
+ # if not flag:
212
+ # print(id)
213
+ file2.write(id + '\t' + sentence.strip() +'#'+ items['upper']+'#'+ items['pants']+'#'+ items['shoes']+ '\n')
214
+
215
+ # print(id, items)
216
+ # break