-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataprep.py
191 lines (161 loc) · 5.35 KB
/
dataprep.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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 12 18:13:05 2023
@author: Mateo-drr
"""
import pandas as pd
import os
import shutil
from PIL import Image
import pickle
import json
ds = 'D:/MachineLearning/datasets/refcocog/refcocog/'
out = ds+'idk/'#'E:/Users/Mateo/Documents/idk/'
obj = pd.read_pickle(r'D:/MachineLearning/datasets/refcocog/refcocog/annotations/refs(umd).p')
with open(ds+'annotations/instances.json', 'r') as file:
jdat = json.load(file)
idk = obj
found = []
saved = []
failed = []
#print(obj[0])
#/filesPath =[]
i=0
files = os.listdir(ds+'images/') #all files have train label
'''
for ref in obj:
for file in files:
if file[:-4] == ref['file_name'][:27]:
file_path = os.path.join(ds+'images/', file)
name = 'COCO_{}2014_{}.jpg'.format(ref['split'],file[-16:-4])
shutil.copy2(file_path, out+name)
files = os.listdir(ds+'idk/') #all files have train label
for name in files:
file_path = os.path.join(ds+'idk/', name)
if name.find('train') != -1:
shutil.copy2(file_path, ds+'train/'+name)
elif name.find('val') != -1:
shutil.copy2(file_path, ds+'val/'+name)
elif name.find('test') != -1:
shutil.copy2(file_path, ds+'test/'+name)
'''
labels = []
dataset = []
split = 'val/'
files = os.listdir(ds+split) #all files have train label
for file in files:
for ref in obj:
refname = 'COCO_{}2014_{:012d}.jpg'.format(ref['split'],ref['image_id'])
if file == refname:
labels.append(ref)
anns = []
for ann in jdat['annotations']:
for ref2 in labels:
if ann['image_id'] == ref2['image_id']:
anns.append(ann)
labann = []
for label in labels:
for ann in anns:
if ann['id'] == label['ann_id']:
labann.append({'label':label, 'annotation':ann})
lb = []
seen = set()
for item in labann:
item_json = json.dumps(item, sort_keys=True)
if item_json not in seen:
lb.append(item)
seen.add(item_json)
if len(lb) == 0:
print('ERROR')
break
file_path = os.path.join(ds+split, file)
im = Image.open(file_path)
image = {
'pixels': im.tobytes(),
'size': im.size,
'mode': im.mode,
}
im=0
#f = {'img':image, 'metadata':labann}
labels =[]
for i in range(len(lb)):
f = {'img': image, 'label': lb[i]['label'], 'annotation':lb[i]['annotation']}
with open(ds +'final/'+split+ file[:-4] +'_lbl'+str(i)+ '.p', 'wb') as pkl:
pickle.dump(f, pkl)
image=0
'''
for a in jdat['annotations']:
if a['image_id'] == 131074:
print(a)
'''
'''
maxx = [0,0] #x,y
for f in train_ds:
if f['img']['size'][0] > maxx[0]:
maxx[0] = f['img']['size'][0]
if f['img']['size'][1] > maxx[1]:
maxx[1] = f['img']['size'][1]
'''
'''
with open('image.pkl', 'rb') as file:
image = pickle.load(file)
im = Image.frombytes(image['mode'], image['size'], image['pixels'])
'''
'''
filt = [[],[],[]] #train,val,test = [],[],[]
for x in obj:
if x['split'] == 'train':
filt[0].append(x)
elif x['split'] == 'val':
filt[1].append(x)
else:
filt[2].append(x)
for i in range(0,3): #sort train first
print(i)
for ref in filt[i]:
name = ref['image_id']#'COCO_{}2014_{:012d}.jpg'.format(ref['split'],ref['image_id'])
for file_name in files:
fname = int(file_name[-16:-4].lstrip('0'))
if name == fname:
name = 'COCO_{}2014_{:012d}.jpg'.format(ref['split'],ref['image_id'])
file_path = os.path.join(ds+'images/', file_name)
shutil.copy2(file_path, out+name)
files.remove(file_name)
'''
'''
for ref in obj:
name = ref['image_id']#'COCO_{}2014_{:012d}.jpg'.format(ref['split'],ref['image_id'])
print('searching', name)
for file_name in files:
#print(file_name[-16:-4])
fname = int(file_name[-16:-4].lstrip('0'))
if name == fname:
print('found file', file_name)
name = 'COCO_{}2014_{:012d}.jpg'.format(ref['split'],ref['image_id'])
file_path = os.path.join(ds+'images/', file_name)
if name.find('train') != -1:
#idk[i]['split'] = 'train'
saved.append(idk[i])
shutil.copy2(file_path, out+name)
elif name.find('val') != -1:
shutil.copy2(file_path, out+name)
#idk[i]['split'] = 'val'
saved.append(idk[i])
elif name.find('test') != -1:
shutil.copy2(file_path, out+name)
#idk[i]['split'] = 'test'
saved.append(idk[i])
else:
print('FAIL')
failed.append(idk[i])
found.append(idk[i])
break
#print('checking name', file_path)
i+=1
'''
'''
if name == file_name:
newf = ds+'{}'.format(ref['split'])
print(file_path, newf, i)#shutil.copy2(name, )
i+=1
'''