-
Notifications
You must be signed in to change notification settings - Fork 5
/
label_images1.py
78 lines (72 loc) · 2.6 KB
/
label_images1.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
import cv2
import numpy as np
import glob
import time
from sklearn.utils import shuffle
def label():
k = np.zeros((4,4), 'float')
for i in range(4):
k[i,i] = 1
temp_label = np.zeros((1,4),'float')
image_array1 = np.zeros((1,115200),'float')
label_array1 = np.zeros((1,4),'float')
count = 0
for img in glob.glob("left/*.png"):
image = cv2.imread(img,cv2.IMREAD_COLOR)
#print(image)
roi = image[120:240, :, :]
#img1 = Image.fromstring(img,'RGB')
#plt.imshow(image,interpolation = 'nearest')
#plt.show()
#img1.show()
temp_array1 = roi.reshape(1,115200).astype(np.float32)
#plt.imshow(temp_array1,interpolation = 'nearest')
#plt.show()
image_array1 = np.vstack((image_array1, temp_array1))
label_array1 = np.vstack((label_array1, k[0]))
count+=1
print('left done')
print(count)
for img in glob.glob("right/*.png"):
image = cv2.imread(img,cv2.IMREAD_COLOR)
#cv2.imshow('img',image)
roi = image[120:240, :, :]
temp_array1 = roi.reshape(1,115200).astype(np.float32)
image_array1 = np.vstack((image_array1, temp_array1))
label_array1 = np.vstack((label_array1, k[2]))
count+=1
print('right done')
print(count)
for img in glob.glob("forward/*.png"):
image = cv2.imread(img,cv2.IMREAD_COLOR)
#cv2.imshow('img',image)
roi = image[120:240, :, :]
temp_array1 = roi.reshape(1,115200).astype(np.float32)
image_array1 = np.vstack((image_array1, temp_array1))
label_array1 = np.vstack((label_array1, k[1]))
count+=1
print('forward done')
print(count)
for img in glob.glob("reverse/*.png"):
if not img:
break
else:
image = cv2.imread(img,cv2.IMREAD_COLOR)
#cv2.imshow('img',image)
roi = image[120:240, :, :]
temp_array1 = roi.reshape(1,115200).astype(np.float32)
image_array1 = np.vstack((image_array1, temp_array1))
label_array1 = np.vstack((label_array1, k[3]))
count+=1
print('reverse done')
print(count)
image_array2 = image_array1[1:,:]
label_array2 = label_array1[1:,:]
image_array2, label_array2 = shuffle(image_array1, label_array1)
train = image_array2[1:, :]
train_labels = label_array2[1:, :]
# save training data as a numpy file
npzNames = 'training_data_temp/test'+time.strftime("%Y%m%d-%H%M%S")+'.npz'
np.savez(npzNames,train=train, train_labels=train_labels)
print('npz file saved!')
return npzNames, count