-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_eval.py
240 lines (203 loc) · 8.96 KB
/
policy_eval.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from __future__ import print_function
from Data import *
import os,sys
from network_run import *
from controlfunctions import *
import cv2
import pyocr, pyocr.builders
from PIL import Image, ImageFont, ImageDraw, ImageOps, ImageStat
import pygame, pygame.camera
from pygame.locals import *
from Pygame_UI import *
import pyrealsense as pyrs
from skimage import color
from skimage.transform import *
from Arduino import Arduino
import numpy as np
import time, datetime
import logging
import math, random
import numpy as np
import h5py
import tflearn
import matplotlib.pyplot as plt
import scipy.misc
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
import tensorflow as tf
tf.reset_default_graph()
class RoverRun():
def __init__(self, framestack=False, film=False):
self.d = Data()
#self.userInterface = Pygame_UI()
self.clock = pygame.time.Clock()
self.FPS = 30
self.image = None
self.done = False
self.paused = False
self.angle = 0
self.timeStart = time.time()
self.stack = framestack
self.film = film
self.keystates = {'up':False, 'down':False, 'left':False, 'right':False, 'shift':False, 'space':False, 'enter':False}
self.combo_dict = {
0 : {'up':False, 'down':False, 'left':False, 'right':False, 'shift':False, 'space':False, 'enter':False},
1 : {'up':True, 'down':False, 'left':False, 'right':False, 'shift':False, 'space':False, 'enter':False},
2 : {'up':False, 'down':True, 'left':False, 'right':False, 'shift':False, 'space':False, 'enter':False},
3 : {'up':False, 'down':False, 'left':True, 'right':False, 'shift':False, 'space':False, 'enter':False},
4 : {'up':False, 'down':False, 'left':False, 'right':True, 'shift':False, 'space':False, 'enter':False},
5 : {'up':False, 'down':False, 'left':False, 'right':False, 'shift':True, 'space':False, 'enter':False},
6 : {'up':True, 'down':False, 'left':False, 'right':False, 'shift':True, 'space':False, 'enter':False},
7 : {'up':False, 'down':True, 'left':False, 'right':False, 'shift':True, 'space':False, 'enter':False},
8 : {'up':False, 'down':False, 'left':True, 'right':False, 'shift':True, 'space':False, 'enter':False},
9 : {'up':False, 'down':False, 'left':False, 'right':True, 'shift':True, 'space':False, 'enter':False},
10 : {'up':True, 'down':False, 'left':True, 'right':False, 'shift':False, 'space':False, 'enter':False},
11 : {'up':True, 'down':False, 'left':False, 'right':True, 'shift':False, 'space':False, 'enter':False},
12 : {'up':False, 'down':True, 'left':True, 'right':False, 'shift':False, 'space':False, 'enter':False},
13 : {'up':False, 'down':True, 'left':False, 'right':True, 'shift':False, 'space':False, 'enter':False},
14 : {'up':True, 'down':False, 'left':True, 'right':False, 'shift':True, 'space':False, 'enter':False},
15 : {'up':False, 'down':True, 'left':True, 'right':False, 'shift':True, 'space':False, 'enter':False},
16 : {'up':True, 'down':False, 'left':False, 'right':True, 'shift':True, 'space':False, 'enter':False},
17 : {'up':False, 'down':True, 'left':False, 'right':True, 'shift':True, 'space':False, 'enter':False},
18 : {'up':True, 'down':True, 'left':False, 'right':True, 'shift':True, 'space':False, 'enter':False},
19 : {'up':True, 'down':False, 'left':True, 'right':True, 'shift':True, 'space':False, 'enter':False},
}
print("BBBBBBBBBBBBBBBBBBBBBBBBB")
tools = pyocr.get_available_tools()
if len(tools) == 0:
print("No OCR tool found")
sys.exit(1)
# The tools are returned in the recommended order of usage
self.tool = tools[0]
print("Will use tool '%s'" % (self.tool.get_name()))
# Ex: Will use tool 'libtesseract'
langs = self.tool.get_available_languages()
print("Available languages: %s" % ", ".join(langs))
self.lang_road = langs[5]
print("Will use lang '%s' for road" % (self.lang_road))
# Webcam (above is for Intel Realsense)
cv2.namedWindow("preview")
global vc
vc = cv2.VideoCapture(0)
print("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC")
# Switch to root (sudo)
print("Connecting to board...")
#board = subprocess.check_output(['sudo', 'python', 'board_connect.py'])
global board
board = Arduino('9600')
print("type: ", type(board))
print("Setting pin 13 to output...")
board.pinMode(13, "OUTPUT")
print("Setting pin 11 to output...")
board.pinMode(11, "OUTPUT")
print("Setting pin 9 to output...")
board.pinMode(9, "OUTPUT")
print("Setting pin 7 to output...")
board.pinMode(7, "OUTPUT")
print("Setting pin 5 to output...")
board.pinMode(5, "OUTPUT")
print("Setting pin 3 to output...")
board.pinMode(3, "OUTPUT")
print("Setting pin 2 to output...")
board.pinMode(2, "OUTPUT")
print("Done configuring board")
# Setup network prediction
if framestack is False:
self.network = input_data(shape=[None, 240, 320, 1])
print("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD")
else:
self.network = input_data(shape=[None, 240, 320, len(framestack)+1])
self.framestack = np.zeros([1, 240, 320, self.FPS])
self.stack.append(0)
self.stack.sort()
print("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
self.network = Alex1(self.network)
print("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
self.model = tflearn.DNN(self.network)
print("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG")
self.model.load('/home/mpcr/Desktop/rodrigo/deepcontrol/saved_models/11-17run1Alex1', weights_only=True)#/home/TF_Rover/RoverData/Felix_3frames10-20_FeatureScaling_DNN1',weights_only=True)
print("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH")
self.run()
def process_video_from_rover(self, jpegbytes, timestamp_10msec):
array_of_bytes = np.fromstring(jpegbytes, np.uint8)
self.image = cv2.imdecode(array_of_bytes, flags=3)
k = cv2.waitKey(5) & 0xFF
return self.image
def onehot_to_combo(self, onehot):
return self.combo_dict[onehot]
def run(self):
logging.basicConfig(filename='eval_logs/example.log',level=logging.DEBUG)
hdlr = logging.FileHandler('/home/mpcr/Desktop/rodrigo/deepcontrol/eval_logs/example.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
rval, frame = vc.read()
cv2.imshow("preview", frame)
key = cv2.waitKey(20)
if key == 27: # Exit on ESCAPE
sys.exit()
c = np.mean(frame,2)#self.cam.color, 2)
c = resize(c, (240,320))
c = np.asarray(c)
c = c[None, :, :, None]
self.image = c
while type(self.image) == type(None):
print("self.image type None")
time.sleep(10000000)
while True:
self.done = False
# send "enter" key to arduino and wait for 2 seconds to reset get_name
send_keys(board, {'right': False, 'space': False, 'shift': False, 'up': False, 'down': False, 'left': False, 'enter': True})
send_keys(board, {'right': False, 'space': False, 'shift': False, 'up': False, 'down': False, 'left': False, 'enter': False})
start_time = time.time()
while not self.done:
rval, frame = vc.read()
cv2.imshow("preview", frame)
key = cv2.waitKey(20)
grayscaled_im = Image.fromarray(color.rgb2gray(frame))
txt_road = self.tool.image_to_string(
grayscaled_im,
lang=self.lang_road,
builder=pyocr.builders.TextBuilder())
if is_off_road(txt_road):
self.done = True
if key == 27: # Exit on ESCAPE
sys.exit()
c = np.mean(frame,2)
c = resize(c, (240,320))
c = np.asarray(c)
c = c[None, :, :, None]
self.image = c
current_frame = self.image
# grayscale and crop
# current_frame=np.mean(current_frame[None,110:,:,:], 3, keepdims=True)
# Local Feature Scaling
current_frame = (current_frame-np.mean(current_frame))/(np.std(current_frame)+1e-6)
# Framestack
if self.stack is not False:
current = current_frame
self.framestack = np.concatenate((current, self.framestack[:, :, :, 1:]), 3)
current_frame = self.framestack[:, :, :, self.stack]
# predict the correct steering angle from input
self.angle = self.model.predict(current_frame)
output_predictions = self.angle
self.angle = np.argmax(self.angle)
keystates = self.onehot_to_combo(self.angle)
random_keystates = self.onehot_to_combo(random.randint(0,19))
# print out feedback
os.system('clear')
print("Final prediction: " + str(self.angle), keystates)
print("Predictions: " + str(output_predictions))
print(self.image.shape)
print(self.clock)
# send predicted keystate to the arduino
send_keys(board, keystates)
# send_keys(board, {'right': False, 'space': False, 'shift': False, 'up': False, 'down': False, 'left': False, 'enter': True})
self.clock.tick(self.FPS)
elapsed_time = np.round(time.time() - start_time, 2)
print('This run lasted %.2f seconds'%(elapsed_time))
logging.info('date: {}: episode terminated after {}s'.format(datetime.datetime.now(), time.time()-start_time))
if __name__ == "__main__":
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
rover = RoverRun()