-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_images.py
192 lines (179 loc) · 6.57 KB
/
view_images.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
192
from __future__ import print_function
import pygame
import os, sys, time, shutil
import csv
import argparse
import logging
import numpy as np
from train4 import process_image, model
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(message)s')
NUM_CLASSES = 4
oshapeX = 640
oshapeY = 480
#Orignal Multi size
moshapeX = 640 * 2
moshapeY = 480
#Scaled Image size
shapeX = 160
shapeY = 120
#Scaled Multi size
mshapeX = 320
mshapeY = 120
conf_level = .3
actions=['STRAIGHT','LEFT','RIGHT','REVERSE']
def build_parser():
parser = argparse.ArgumentParser("Date Set Viewer")
parser.add_argument(
'data_set',
type=str,
help="Name of data set"
)
parser.add_argument(
'model',
type=str,
help="model"
)
parser.add_argument(
'-view',
action='store_true',
default=False,
help="Set view mode on/off. Default: off"
)
return parser
def wait_key(command):
global screen
while True:
for ev in pygame.event.get():
if ev.type == pygame.KEYDOWN:
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE] or keys[pygame.K_q] or \
pygame.event.peek(pygame.QUIT):
logging.info('\rExiting...')
return (-1)
elif keys[pygame.K_UP]:
logging.debug("\rup pressed")
return (0)
elif keys[pygame.K_DOWN]:
logging.debug("\rdown pressed")
return (3)
elif keys[pygame.K_LEFT]:
logging.debug("\rleft pressed")
return (1)
elif keys[pygame.K_RIGHT]:
logging.debug("\rright pressed")
return (2)
elif keys[pygame.K_a]:
logging.info("\rMOVE BACK <-")
return (4)
elif keys[pygame.K_d]:
logging.info("\rMOVE FORWARD ->")
return (5)
elif keys[pygame.K_x]:
logging.info("\rRemoving")
return (6)
elif keys[pygame.K_SPACE]:
return (command)
# else:
# logging.debug("\rIncorrect input")
def display_image(image_path, data_path):
ot = 0
print ("display_image")
left_path = image_path+"/left/"
right_path = image_path+"/right/"
entries = None
attributes = None
with open(data_path, 'r') as csv_file:
reader = csv.reader(csv_file)
attributes = next(reader, None)
entries = list(reader)
i = 0
while i < len(entries):
if entries[i]:
left_img = entries[i][0]
right_img = entries[i][1]
command = int(entries[i][2])
left_name = left_path+left_img
right_name = right_path+right_img
left_img = pygame.image.load(left_path+left_img)
right_img = pygame.image.load(right_path+right_img)
if left_img and right_img:
img_left = pygame.transform.scale(left_img,(oshapeX,oshapeY))
img_right = pygame.transform.scale(right_img,(oshapeX,oshapeY))
screen.blit(img_left,(0,0))
screen.blit(img_right,(oshapeX,0))
print (left_name, right_name)
md_img, _ = process_image([left_name,right_name], None, False, True, shape=(shapeY,shapeX))
pred_act = model.predict(np.array([md_img]))[0]
pred = "Lft: %.2f | Fwd: %.2f | Rht: %.2f | Rev: %.2f" % \
(pred_act[1], pred_act[0], pred_act[2], pred_act[3])
pygame.display.set_caption(actions[command] + " | " + pred)
pygame.display.flip()
# print(entries[i])
act_i = np.argmax(pred_act)
# print (actions[act_i])
# if (pred_act[act_i]<conf_level):
# act_i = 3
# print ("final: ", actions[act_i], actions[command])
if act_i != command or pred_act[act_i]<conf_level or args.view:
press = wait_key(command)
if press < 0:
# exiting and go forward with writing csv
break
elif press > 3:
if press == 4:
if i == 0:
logging.warning("Can't do that, at earliest")
else:
i -= 1
elif press == 5:
if i == len(entries):
logging.warning("Can't do that, at latest")
else:
i += 1
elif press == 6:
os.remove(left_path+entries[i][0])
os.remove(right_path+entries[i][1])
entries[i] = []
i += 1
else:
entries[i][2] = press
i += 1
# print("changed")
# print(entries[i])
ct = time.time()
while (ct - ot) * 1000 < 100:
pygame.event.clear()
pygame.event.pump()
ct = time.time()
ot = ct
else:
i += 1
#write file
# print(entries)
if entries:
with open(data_path, 'w') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(attributes)
for row in entries:
if row:
writer.writerow(row)
else:
logging.info("skipping")
if __name__ == "__main__":
parser = build_parser()
args = parser.parse_args()
img_path = "./data_sets/%s" % args.data_set
if not os.path.exists(img_path):
logging.error("Error: data set doest not exist.")
exit(1)
label_path = "./model_data/%s_log.csv" % args.data_set
if not os.path.exists(label_path):
logging.error("Error: data set does not have labels.")
exit(1)
shape = (mshapeY, mshapeX, 3)
model = model(True, shape, tr_model=args.model)
pygame.init()
size = (moshapeX,oshapeY)
screen = pygame.display.set_mode(size)
display_image(img_path, label_path)
pygame.quit()