Datasets:

Modalities:
Text
Formats:
text
Libraries:
Datasets
License:
File size: 17,929 Bytes
fff4be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from os import listdir, walk
from os.path import isfile, isdir, join, splitext, exists, getmtime
from random import seed, randint, choice
import re
import json
import datetime

import argparse

parser = argparse.ArgumentParser(description='Generate BBBicycles split.')
parser.add_argument('-p', '--path', type=str, required=True,
                    help='directory containing the ID folders')

args = parser.parse_args()
path = args.path 

random_seed = seed(1337)
train_val_bike_type_split = 10

img_regex = re.compile('(^img.\d*[.]png$)')
dir_regex = re.compile('(^\w+_)')

train = open("bike_train.txt", "w")
query_v = open("bike_query_val.txt", "w")
galley_v = open("bike_gallery_val.txt", "w")
query_t = open("bike_query_test.txt", "w")
galley_t = open("bike_gallery_test.txt", "w")

num_ids = 0
num_imgs = 0
num_damaged_imgs = 0
num_broken_imgs = 0
num_bent_imgs = 0
num_missingpart_imgs = 0
nums_missingpart_imgs = [0 for i in range(5)]
models_dist = {}

num_train_ids = 0
num_train_imgs = 0
num_damaged_train_imgs = 0
num_broken_train_imgs = 0
num_bent_train_imgs = 0
num_missingpart_train_imgs = 0
nums_missingpart_train_imgs = [0 for i in range(5)]
models_dist_train = {}

num_val_ids = 0
num_val_imgs = 0
num_damaged_val_imgs = 0
num_broken_val_imgs = 0
num_bent_val_imgs = 0
num_missingpart_val_imgs = 0
nums_missingpart_val_imgs = [0 for i in range(5)]
models_dist_val = {}

num_test_ids = 0
num_test_imgs = 0
num_damaged_test_imgs = 0
num_broken_test_imgs = 0
num_bent_test_imgs = 0
num_missingpart_test_imgs = 0
nums_missingpart_test_imgs = [0 for i in range(5)]
models_dist_test = {}

for id, bike in enumerate(listdir(path)):
    if isdir(join(path, bike)) and dir_regex.match(bike) and exists(join(path, bike, "before")) and len(listdir(join(path, bike, "before"))) != 0 and exists(join(path, bike, "after")) and len(listdir(join(path, bike, "after"))) != 0 and exists(join(path, bike, "fixed_data.json")):
        #aggiungere: prendere json dell'identità per estrarre info 
        json_fixed = open(join(path, bike, 'fixed_data.json'),)  
        data_fixed = json.load(json_fixed)
        
        if str(data_fixed['Bike Type']) not in models_dist:
            models_dist[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1}
        elif str(data_fixed['Model']) not in models_dist[str(data_fixed['Bike Type'])]:
            models_dist[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1
        else:
            models_dist[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 
        c=0
        num_ids = num_ids + 1        
        for type in ["before", "after"]:            
              for file in listdir(join(path, bike, type)):
                if img_regex.match(file):
                    if exists(join(path, bike, type, splitext(file)[0] + '_variable.json')):                    
                        json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),)  
                        data = json.load(json_var)

                        dmgid = 0 if type == "before" else int(data["Damage Type"])
                        missid = "00000" if type == "before" else str(data["Removed Parts"]) 

                        json_var.close()

                        if dmgid != 0:
                            num_damaged_imgs = num_damaged_imgs + 1
                        if dmgid == 2 or dmgid == 3:
                            num_broken_imgs = num_broken_imgs + 1
                        if dmgid == 1 or dmgid == 3:
                            num_bent_imgs = num_bent_imgs + 1 
                        if missid != "00000" :
                            num_missingpart_imgs = num_missingpart_imgs + 1
                            for i in range(5):
                                if missid[i] == "1":
                                    nums_missingpart_imgs[i] = nums_missingpart_imgs[i] + 1  
                        num_imgs = num_imgs + 1
                        c=c+1
                    else:
                        print(bike) 
        if c != 14:
            print(bike) 
        if str(data_fixed['Model']) in ['mfactory ', 'ghost', 'oldbike', 'rondo', 'verdona']:
            #Train
            if str(data_fixed['Bike Type']) not in models_dist_train:
                models_dist_train[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1}
            elif str(data_fixed['Model']) not in models_dist_train[str(data_fixed['Bike Type'])]:
                models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1
            else:
                models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 
        
            num_train_ids = num_train_ids + 1
            for type in ["before", "after"]:
                  for file in listdir(join(path, bike, type)):
                    if img_regex.match(file) and exists(join(path, bike, type, splitext(file)[0] + '_variable.json')):
                        img_path = join(bike, type, file)
                        
                        json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),)  
                        data = json.load(json_var)

                        camid = int(data["Focal Length"]) 
                        viewid = int(data["Viewing Side"])
                        dmgid = 0 if type == "before" else int(data["Damage Type"])
                        missid = "00000" if type == "before" else str(data["Removed Parts"]) 
                        
                        train.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid))
                        json_var.close()
                        
                        if dmgid != 0:
                            num_damaged_train_imgs = num_damaged_train_imgs + 1
                        if dmgid == 2 or dmgid == 3:
                            num_broken_train_imgs = num_broken_train_imgs + 1
                        if dmgid == 1 or dmgid == 3:
                            num_bent_train_imgs = num_bent_train_imgs + 1 
                        if missid != "00000" :
                            num_missingpart_train_imgs = num_missingpart_train_imgs + 1
                            for i in range(5):
                                if missid[i] == "1":
                                    nums_missingpart_train_imgs[i] = nums_missingpart_train_imgs[i] + 1     
                        num_train_imgs = num_train_imgs + 1
        else:
            if str(data_fixed['Model']) not in ['mirage', 'gbike', 'enduro']:                
                if str(data_fixed['Model']) not in ['becane', 'btwin', 'croad'] and randint(0, 100) > train_val_bike_type_split:
                    if str(data_fixed['Bike Type']) not in models_dist_train:
                        models_dist_train[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1}
                    elif str(data_fixed['Model']) not in models_dist_train[str(data_fixed['Bike Type'])]:
                        models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1
                    else:
                        models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 

                    num_train_ids = num_train_ids + 1
                    for type in ["before", "after"]:
                          for file in listdir(join(path, bike, type)):
                            if img_regex.match(file) and exists(join(path, bike, type, splitext(file)[0] + '_variable.json')):
                                img_path = join(bike, type, file)

                                json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),)  
                                data = json.load(json_var)

                                camid = int(data["Focal Length"]) 
                                viewid = int(data["Viewing Side"])
                                dmgid = 0 if type == "before" else int(data["Damage Type"])
                                missid = "00000" if type == "before" else str(data["Removed Parts"]) 

                                train.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid))
                                json_var.close()

                                if dmgid != 0:
                                    num_damaged_train_imgs = num_damaged_train_imgs + 1
                                if dmgid == 2 or dmgid == 3:
                                    num_broken_train_imgs = num_broken_train_imgs + 1
                                if dmgid == 1 or dmgid == 3:
                                    num_bent_train_imgs = num_bent_train_imgs + 1 
                                if missid != "00000" :
                                    num_missingpart_train_imgs = num_missingpart_train_imgs + 1
                                    for i in range(5):
                                        if missid[i] == "1":
                                            nums_missingpart_train_imgs[i] = nums_missingpart_train_imgs[i] + 1     
                                num_train_imgs = num_train_imgs + 1
                else:
                    #Val
                    if str(data_fixed['Bike Type']) not in models_dist_val:
                        models_dist_val[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1}
                    elif str(data_fixed['Model']) not in models_dist_val[str(data_fixed['Bike Type'])]:
                        models_dist_val[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1
                    else:
                        models_dist_val[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_val[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 

                    num_val_ids = num_val_ids + 1
                    for type in ["before", "after"]:
                        files = [f for f in listdir(join(path, bike, type)) if img_regex.match(f) and exists(join(path, bike, type, splitext(f)[0] + '_variable.json'))]
                        file = choice(files)
                        img_path = join(bike, type, file)

                        json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),)  
                        data = json.load(json_var)

                        camid = int(data["Focal Length"]) 
                        viewid = int(data["Viewing Side"])
                        dmgid = 0 if type == "before" else int(data["Damage Type"])
                        missid = "00000" if type == "before" else str(data["Removed Parts"]) 

                        if type == "before" :
                            galley_v.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid))
                        else:
                            query_v.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid))
                        json_var.close()

                        if dmgid != 0:
                            num_damaged_val_imgs = num_damaged_val_imgs + 1
                        if dmgid == 2 or dmgid == 3:
                            num_broken_val_imgs = num_broken_val_imgs + 1
                        if dmgid == 1 or dmgid == 3:
                            num_bent_val_imgs = num_bent_val_imgs + 1 
                        if missid != "00000" :
                            num_missingpart_val_imgs = num_missingpart_val_imgs + 1
                            for i in range(5):
                                if missid[i] == "1":
                                    nums_missingpart_val_imgs[i] = nums_missingpart_val_imgs[i] + 1 
                        num_val_imgs = num_val_imgs + 1
            else:
                #Test
                if str(data_fixed['Bike Type']) not in models_dist_test:
                    models_dist_test[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1}
                elif str(data_fixed['Model']) not in models_dist_test[str(data_fixed['Bike Type'])]:
                    models_dist_test[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1
                else:
                    models_dist_test[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_test[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 
            
                num_test_ids = num_test_ids + 1
                for type in ["before", "after"]:
                    files = [f for f in listdir(join(path, bike, type)) if img_regex.match(f) and exists(join(path, bike, type, splitext(f)[0] + '_variable.json'))]
                    if not files:
                        print(join(path, bike, type))
                    file = choice(files)
                    img_path = join(bike, type, file)
                    
                    json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),)  
                    data = json.load(json_var)

                    camid = int(data["Focal Length"]) 
                    viewid = int(data["Viewing Side"])
                    dmgid = 0 if type == "before" else int(data["Damage Type"])
                    missid = "00000" if type == "before" else str(data["Removed Parts"]) 
                    
                    if type == "before" :
                        galley_t.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid))
                    else:
                        query_t.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid))
                    json_var.close()
                    
                    if dmgid != 0:
                        num_damaged_test_imgs = num_damaged_test_imgs + 1
                    if dmgid == 2 or dmgid == 3:
                        num_broken_test_imgs = num_broken_test_imgs + 1
                    if dmgid == 1 or dmgid == 3:
                        num_bent_test_imgs = num_bent_test_imgs + 1 
                    if missid != "00000" :
                        num_missingpart_test_imgs = num_missingpart_test_imgs + 1
                        for i in range(5):
                            if missid[i] == "1":
                                nums_missingpart_test_imgs[i] = nums_missingpart_test_imgs[i] + 1 
                    num_test_imgs = num_test_imgs + 1
        json_fixed.close()
    else:
        print(bike)
train.close()
query_v.close()
galley_v.close()
query_t.close()
galley_t.close()

data = {}
data["General"] = []
data["General"].append({
   'Num IDs': num_ids,
   'Num Bike types': len(models_dist.keys()), 
   'Num Models': sum(len(models_dist[k].keys()) for k in models_dist.keys()), 
   'Num images': num_imgs,
   'Num bent images': num_bent_imgs,   
   'Num broken images': num_broken_imgs,
   'Num damaged images': num_damaged_imgs,
   'Num images with missing parts': num_missingpart_imgs,
   'Num images with missing Front Wheel': nums_missingpart_imgs[0],
   'Num images with missing Rear Wheel': nums_missingpart_imgs[1],
   'Num images with missing Seat': nums_missingpart_imgs[2],
   'Num images with missing Handlebar': nums_missingpart_imgs[3],
   'Num images with missing Pedals': nums_missingpart_imgs[4]
})
data["Train"] = []
data["Train"].append({
   'Num IDs': num_train_ids,
   'Num Bike types': len(models_dist_train.keys()), 
   'Num Models': sum(len(models_dist_train[k].keys()) for k in models_dist_train.keys()),
   'Num images': num_train_imgs,
   'Num bent images': num_bent_train_imgs,   
   'Num broken images': num_broken_train_imgs,
   'Num damaged images': num_damaged_train_imgs,
   'Num images with missing parts': num_missingpart_train_imgs,
   'Num images with missing Front Wheel': nums_missingpart_train_imgs[0],
   'Num images with missing Rear Wheel': nums_missingpart_train_imgs[1],
   'Num images with missing Seat': nums_missingpart_train_imgs[2],
   'Num images with missing Handlebar': nums_missingpart_train_imgs[3],
   'Num images with missing Pedals': nums_missingpart_train_imgs[4]   
})
data["Validation"] = []
data["Validation"].append({
   'Num IDs': num_val_ids,
   'Num Bike types': len(models_dist_val.keys()), 
   'Num Models': sum(len(models_dist_val[k].keys()) for k in models_dist_val.keys()),
   'Num images': num_val_imgs,
   'Num bent images': num_bent_val_imgs,   
   'Num broken images': num_broken_val_imgs,
   'Num damaged images': num_damaged_val_imgs,
   'Num images with missing parts': num_missingpart_val_imgs,
   'Num images with missing Front Wheel': nums_missingpart_val_imgs[0],
   'Num images with missing Rear Wheel': nums_missingpart_val_imgs[1],
   'Num images with missing Seat': nums_missingpart_val_imgs[2],
   'Num images with missing Handlebar': nums_missingpart_val_imgs[3],
   'Num images with missing Pedals': nums_missingpart_val_imgs[4] 
})
data["Test"] = []
data["Test"].append({
   'Num IDs': num_test_ids,
   'Num Bike types': len(models_dist_test.keys()), 
   'Num Models': sum(len(models_dist_test[k].keys()) for k in models_dist_test.keys()),
   'Num images': num_test_imgs,
   'Num bent images': num_bent_test_imgs,   
   'Num broken images': num_broken_test_imgs,
   'Num damaged images': num_damaged_test_imgs,
   'Num images with missing parts': num_missingpart_test_imgs,
   'Num images with missing Front Wheel': nums_missingpart_test_imgs[0],
   'Num images with missing Rear Wheel': nums_missingpart_test_imgs[1],
   'Num images with missing Seat': nums_missingpart_test_imgs[2],
   'Num images with missing Handlebar': nums_missingpart_test_imgs[3],
   'Num images with missing Pedals': nums_missingpart_test_imgs[4] 
})

with open('bike_current_split_stats.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)