-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdata_viewer.py
160 lines (132 loc) · 5.82 KB
/
data_viewer.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
import argparse
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import os, sys
import pandas as pd
import json
import time, datetime
from logic import SignalPainter
import cv2
from clients import QTSteeringClient
class MainApp(QWidget):
def __init__(self, data_path, steering_file):
QWidget.__init__(self)
self.data_path = data_path
self.dirlist = os.listdir(self.data_path)
self.video_size = QSize(320, 240)
self.setup_ui()
self.painter = QPainter()
self._current_dir = os.path.join(self.data_path, '')
self.steering_hist = ''
self.pic_list = []
self.signal_list=[]
self.signal = {}
self.draw_border = 5
self.steering_file = steering_file
self.signal_painter = SignalPainter(
video_size= (320, 240),
draw_border=self.draw_border,
letters='wsad',
)
@property
def current_dir(self):
return self._current_dir
@current_dir.setter
def current_dir(self, newdir):
self._current_dir = os.path.join(self.data_path, newdir.text())
def draw_keys(self, *args, **kwargs):
img, kwrgs = self.signal_painter.action(*args, **kwargs)
return img
def load_signal(self, signal_number):
signal = self.signal[signal_number]
picpath = os.path.join(self.current_dir, signal['filenames'])
frame = cv2.imread(picpath)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = self.draw_keys(frame, keys=signal['keys'])
image = QImage(frame, frame.shape[1], frame.shape[0],
frame.strides[0], QImage.Format_RGB888)
self.image_label.setPixmap(QPixmap.fromImage(image))
@staticmethod
def to_common_datatype(df):
signal = {}
for i, (index , data) in enumerate(df.to_dict('index').items()):
keys = {k:data[k] for k in 'wsad'}
signal[i] = dict(date=datetime.datetime.fromtimestamp(index), keys=keys, **data)
return signal
def create_signal(self):
self.signal = self.to_common_datatype(self.data)
def load_new_dir(self):
dir_steering_path = os.path.join(self.current_dir,self.steering_file)
self.data = pd.read_csv(dir_steering_path)
self.create_signal()
self.slider.setMaximum(max(list(self.signal.keys())))
self.load_signal(0)
def on_dir_change(self, curr, prev):
self.current_dir = curr
self.load_new_dir()
def change_signal(self, signal_number):
self.load_signal(signal_number)
signal = self.signal[signal_number]
t = signal['date']
tim = t.strftime("%H:%M:%S.%f")
signal = "".join([k if signal[k]==True else '' for k in "wsad"])
self.image_data.setText("Number:{}/{} Time:{} \n Signal:{}".format(signal_number, max(list(self.signal.keys())), tim, signal))
def delete_frames(self, min_id, max_id=0):
if max_id == 0:
max_id = min_id
frames_to_remove = [self.signal[x] for x in [min_id, max_id]]
for frame_to_remove in frames_to_remove:
filename = os.path.join(self._current_dir, frame_to_remove['filenames'])
if os.path.isfile(filename):
os.remove(filename)
self.data = self.data.drop(self.data.index[[min_id, max_id]])
self.create_signal()
self.slider.setMaximum(max(list(self.signal.keys())))
dir_steering_path = os.path.join(self.current_dir,self.steering_file)
self.data.to_csv(dir_steering_path)
self.change_signal(self.slider.value())
def delete_frame(self):
self.delete_frames(self.slider.value())
def setup_ui(self):
self.image_label = QLabel()
self.image_label.setFixedSize(self.video_size)
self.quit_button = QPushButton("Quit")
self.quit_button.clicked.connect(self.close)
self.delete_button = QPushButton("Delete frame")
self.delete_button.clicked.connect(self.delete_frame)
self.main_layout = QHBoxLayout()
self.image_layout = QVBoxLayout()
self.image_layout.addWidget(self.image_label)
self.image_data = QLabel()
self.image_layout.addWidget(self.image_data)
self.slider = QSlider(Qt.Horizontal)
self.slider.setMinimum(0)
self.slider.setTickInterval(1)
self.slider.setMaximum(1)
self.slider.valueChanged.connect(self.change_signal)
self.image_layout.addWidget(self.slider)
self.main_layout.addLayout(self.image_layout)
self.file_list = QListWidget()
self.file_list.addItems(self.dirlist)
self.file_list.currentItemChanged.connect(self.on_dir_change)
self.right_panel = QVBoxLayout()
self.right_panel.addWidget(self.quit_button)
self.right_panel.addWidget(self.delete_button)
self.right_panel.addWidget(self.file_list)
self.main_layout.addLayout(self.right_panel)
self.setLayout(self.main_layout)
def display_video_stream(self):
signal = self.signal
image = QImage(signal, signal.shape[1], signal.shape[0], signal.strides[0], QImage.Format_RGB888)
self.image_label.setPixmap(QPixmap.fromImage(image))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='View the data')
parser.add_argument('folder', type=str)
parser.add_argument('--steering_file', metavar='steering_file', type=str, default='steering_v1.csv',
help='name of the file to use to reconstruct the steering signal')
args = parser.parse_args()
app = QApplication(sys.argv)
win = MainApp(args.folder, args.steering_file)
win.show()
sys.exit(app.exec_())