-
Notifications
You must be signed in to change notification settings - Fork 1
/
readlabel.py
53 lines (49 loc) · 1.57 KB
/
readlabel.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
import numpy as np
import glob as gb
from PIL import Image
def read_image(path1 = './test_images/', path2 = './test_annotation.annotation', data_size = 1050):
data = list()
with open(path2, 'r') as fileReader:
# read all the data
lines = fileReader.readlines()
for line in lines:
line = line.strip()
# split the index and label
line = line.split("\t")
data.append(line[:])
data = np.array(data)
data = data.astype(int)
index = data[:, 0]
label = data[:, 1:]
label = label.reshape(-1)
print("data loaded!")
path3 = '.JPEG'
num = 0
row_data = []
for ii in range(1, data_size + 1):
num_str = str(ii)
path = path1+num_str+path3
imm = Image.open(path)
l1 = []
for n in range(64):
l2 = []
for m in range(64):
l3 = imm.getpixel((m, n))
if type(l3) is int:
l2.append(list([l3, l3, l3]))
else:
l2.append(list(l3))
l1.append(l2)
one_image = [np.array(l1), label[num]]
row_data.append(one_image)
num = num + 1
whole_x = np.zeros((data_size, 64, 64, 3))
whole_y = np.zeros((data_size, 20), dtype=int)
for i in range(data_size):
whole_x[i] = row_data[i][0]
label = row_data[i][1]
if label!=21:
whole_y[i][label - 1] = 1
whole_data = [whole_x, whole_y]
print("image loaded!")
return whole_data