-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_data.py
More file actions
163 lines (140 loc) · 3.65 KB
/
create_data.py
File metadata and controls
163 lines (140 loc) · 3.65 KB
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
from pycocotools.coco import COCO
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
from PIL import Image
from threading import Thread
import time
from pathlib import Path
class IlMioThread (Thread):
def __init__(self, lista,d,coco,num):
Thread.__init__(self)
self.lista = lista
self.d = d
self.coco = coco
self.num = num
def run(self):
h = -1
w = -1
k11 = 0
k12 = 0
k21 = 0
k22 = 0
number_list = []
count1 = 0
r_c = 0
for i in self.lista:
number_list = []
t = self.coco.imgs[i]
h, w = t['height'], t['width']
k11 = 0
k12 = 0
k21 = 0
k22 = 0
h = 640-h
w = 640-w
if h > 0 and h%2==0:
k11 = h/2
k12 = h/2
elif h > 0 and h%2!=0:
k11 = ((int)(h/2))
k12 = ((int)(h/2))+1
if w > 0 and w%2==0:
k21 = w/2
k22 = w/2
elif w > 0 and w&2!=0:
k21 = ((int)(w/2))
k22 = ((int)(w/2))+1
for j in range(0,len(d[i])):
if(j%2==0):
self.d[i][j][0] += k21
self.d[i][j][1] += k11
h, w = t['height'], t['width']
imgIds = self.coco.getImgIds(imgIds=[i]);
img = self.coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0]
I = io.imread(img['coco_url'])
io.imsave(str(self.num)+".jpg", I)
im = Image.open(str(self.num)+".jpg", "r")
pix_val = list(im.getdata())
count1 = 0
if(type(pix_val[0]) != int):
#print(i)
my_file = Path("./data/"+str(i)+".txt")
if not my_file.is_file():
f = open("./data/"+str(i)+".txt","w")
r_c += 1
for j in range(0,640):
for k in range(0,640):
if(j < k11 or j >= k11+h or k < k21 or k >= k21+w):
number_list.append(0)
else:
#print(pix_val[count1])
number_list.append(pix_val[count1][0])
count1+=1
count1 = 0
for j in range(0,640):
for k in range(0,640):
if(j < k11 or j >= k11+h or k < k21 or k >= k21+w):
number_list.append(0)
else:
number_list.append(pix_val[count1][1])
count1+=1
count1 = 0
for j in range(0,640):
for k in range(0,640):
if(j < k11 or j >= k11+h or k < k21 or k >= k21+w):
number_list.append(0)
else:
number_list.append(pix_val[count1][2])
count1+=1
for j in range(0,len(number_list)):
f.write((str(number_list[j]))+',')
for j in range(0,len(d[i])):
if(j%2==0):
for k in range(0,len(self.d[i][j])):
f.write(str(self.d[i][j][k])+',')
else:
f.write((str(self.d[i][j])+','))
f.close()
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
dataDir='..'
dataType='train2017'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)
coco=COCO(annFile)
d = {}
for i in coco.anns:
#print(coco.loadAnns([i]))
#exit(0)
if coco.loadAnns([i])[0]['image_id'] not in d:
d[coco.loadAnns([i])[0]['image_id']] = [coco.loadAnns([i])[0]['bbox'],coco.loadAnns([i])[0]['category_id']]
else:
d[coco.loadAnns([i])[0]['image_id']].append(coco.loadAnns([i])[0]['bbox'])
d[coco.loadAnns([i])[0]['image_id']].append(coco.loadAnns([i])[0]['category_id'])
lista = d.keys()
lista.sort()
l1 = []
l2 = []
l3 = []
l4 = []
for i in range(0,len(lista)/4):
l1.append(lista[i])
for i in range(len(lista)/4,len(lista)/2):
l2.append(lista[i])
for i in range(len(lista)/2,len(lista)*3/4):
l3.append(lista[i])
for i in range(len(lista)*3/4,len(lista)):
l4.append(lista[i])
thread1 = IlMioThread(l1,d,coco,1)
thread2 = IlMioThread(l2,d,coco,2)
thread3 = IlMioThread(l3,d,coco,3)
thread4 = IlMioThread(l4,d,coco,4)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
# Join
thread1.join()
thread2.join()
thread3.join()
thread4.join()