forked from developer-student-club-thapar/Autonomous-Driving
-
Notifications
You must be signed in to change notification settings - Fork 1
/
autolabeler.py
213 lines (167 loc) · 7.27 KB
/
autolabeler.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
import tkinter
import cv2
import PIL.Image, PIL.ImageTk
import time
import glob
import os
import pandas as pd
from time import time
import sys, getopt
import joblib
import numpy as np
import json
from keras.models import load_model
scaler_filename = './assets/scaler_standard.pkl'
model_name = r'save_model/Comma-saved-model-final-1-08-0.0890.hdf5'
scaler = joblib.load(scaler_filename)
model = load_model(model_name, compile=False)
def predict(test_img):
global model
test_img = cv2.resize(test_img, (320, 160))
steering_angle = model.predict([[test_img]], batch_size=1)
degrees = scaler.inverse_transform([[steering_angle]])
#degrees = np.rad2deg(steering_angle)
return float(degrees)
argv = sys.argv[1:]
WIDTH = 640
HEIGHT = 420
datafile = ''
basedir = './'
savefile = './output.csv'
labels = ['disable', 'undefined', 'stay_in_lane', 'change_lane_left',
'change_lane_right', 'turn_left',
'turn_right']
try:
opts, args = getopt.getopt(argv,"b:d:h:w:s:",["basedir=","datafile=", "height=", "width=", "savefile="])
except getopt.GetoptError:
print('following arguements are supported:\n -b <basedir> -d <datafile> -h <height> -w <width> -s <savefile>')
sys.exit(2)
for opt, arg in opts:
if opt in ('-b', '--basedir'):
basedir = arg
if opt in ("-d", "--datafile"):
datafile = arg
if opt in ("-h", "--height"):
HEIGHT = int(arg)
if opt in ("-w", "--width"):
WIDTH = int(arg)
if opt in ("-s", "--savefile"):
savefile = arg
print(opts)
if not datafile:
print('No datafile provided.. Exiting..')
sys.exit(2)
data = pd.read_csv(datafile)
if 'filename' in data.columns:
filenames = data['filename']
else:
print("No 'filename' column found in datafile.. exiting")
sys.exit(2)
if 'action' in data.columns:
actions = data['action']
else:
print("WARNING: 'action' column not found in datafile. initializing to undefined actions..")
actions = pd.Series(['undefined']*len(filenames))
class App:
def __init__(self, window, window_title, video_source=[], labels=[], actions=[], basedir='./', columns=20, savefile='./output.csv'):
self.window = window
self.window.resizable(0, 0)
self.window.title(window_title)
self.video_source = video_source
self.basedir = basedir
self.labels = labels
self.actions = actions
self.savefile = savefile
self.current_label = tkinter.StringVar(self.window)
self.current_label.set(self.labels[0])
self.playing = False
self.vid = VideoCapture(self.video_source)
self.degrees = 0
self.steering_wheel = cv2.imread('assets/steering_wheel_image.jpg',0)
# Create a canvas that can fit the above video source size
self.canvas = tkinter.Canvas(window, width=WIDTH, height=HEIGHT)
self.canvas.grid(row=0, columnspan=columns)
self.slider = tkinter.Scale(window, from_=0, to=len(video_source),
length=WIDTH, sliderrelief='flat',
orient="horizontal", highlightthickness=0, background='#454545',
fg='grey', troughcolor='#1a1a1a', activebackground='#1065BF', command=self.seek)
self.slider.grid(row=1, columnspan=columns)
self.btn_play=tkinter.Button(window, text=u"\u25B6", command=self.play, activebackground='#1a1a1a')
self.btn_play.grid(row=3, column=columns//2 - 2, sticky='NEWS')
self.btn_pause=tkinter.Button(window, text=u"| |", command=self.pause, activebackground='#1a1a1a')
self.btn_pause.grid(row=3, column=columns//2-1, sticky='NEWS')
self.drop_down = tkinter.OptionMenu(window, self.current_label, *self.labels)
self.drop_down.grid(row=3, column=columns-1, sticky='NEWS')
self.display_label = tkinter.Label(self.window)
self.display_label.grid(row=3, column=0, sticky='NEWS')
self.btn_faster=tkinter.Button(window, text=">>", command=self.faster, activebackground='#1a1a1a')
self.btn_faster.grid(row=3, column=columns//2 , sticky='NEWS')
self.btn_slower=tkinter.Button(window, text="<<", command=self.slower, activebackground='#1a1a1a')
self.btn_slower.grid(row=3, column=columns//2-3 , sticky='NEWS')
# After it is called once, the update method will be automatically called every delay milliseconds
self.delay = 64
self.index = 0
self.update()
self.window.configure(bg='#e0e0e0')
self.window.mainloop()
def pause(self):
self.playing = False
def play(self):
self.playing = True
def faster(self):
self.delay -= 10
def slower(self):
self.delay += 10
def speed_buttons_controller(self):
if self.delay <= 104:
self.btn_slower["state"] = "normal"
else:
self.btn_slower["state"] = "disabled"
if self.delay >= 24:
self.btn_faster["state"] = "normal"
else:
self.btn_faster["state"] = "disabled"
def update(self):
if self.index >= len(self.video_source) - 1:
self.index = 0
self.pause()
# Get a frame from the video source
ret, frame = self.vid.get_frame(self.basedir, self.index)
if ret:
self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
self.canvas.create_image(0, 0, image = self.photo, anchor = tkinter.NW)
self.window.after(self.delay, self.update)
self.display_label.configure(text='Current Action: ' + self.actions[self.index])
if self.playing:
degrees = predict(frame) ## GET THIS READING FROM MODEL
self.degrees += 0.2 * pow(abs((degrees - self.degrees)), 2.0 / 3.0) * (degrees - self.degrees) / abs(degrees - self.degrees)
M = cv2.getRotationMatrix2D((240/2,240/2),-self.degrees,1)
dst = cv2.warpAffine(self.steering_wheel,M,(240,240))
cv2.imshow("steering wheel", dst)
if self.current_label.get() != 'disable':
self.actions[self.index] = self.current_label.get()
self.index += 1
self.slider.set(self.index)
self.speed_buttons_controller()
def seek(self, index):
self.index = int(index)
self.slider.set(self.index)
def __del__(self):
df = pd.DataFrame({'filename':self.video_source, 'action':self.actions})
df.to_csv(self.savefile, index=False)
class VideoCapture:
def __init__(self, video_source=[]):
# Open the video source
self.frames = video_source
self.width = WIDTH
self.height = HEIGHT
def get_frame(self, basedir, index):
try:
frame = cv2.imread(os.path.join(basedir, self.frames[index]))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
return (True, cv2.resize(frame, (WIDTH, HEIGHT)))
except Exception as e:
print(e)
return (False, None)
App(tkinter.Tk(), 'AutoLabeler', video_source=filenames, labels=labels, actions=actions, savefile=savefile, basedir=basedir)
#python autolabeler.py -b "C:\Users\asus\Desktop\Self Driving\utils\sampled" -d "C:\Users\asus\Desktop\Self Driving\utils\datafile.csv"