-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
362 lines (259 loc) · 10.6 KB
/
ui.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python
import os
import threading
import socket
import tkinter as tk
from tkinter import ttk
import csv
import time
import pathlib
from PIL import ImageTk, Image
HOST = "127.0.0.1"
PORT = 8888
class Vizualizer(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent.tabs)
self.shouldexit = False
self.path = parent.path
self.runvideo = False
flist = [ f.path for f in os.scandir(self.path) if f.is_dir() ]
self.options = {}
for folder in flist:
name = pathlib.PurePath(folder).name
self.options[name] = folder
self.create_widgets()
self.frame = 0
self.csv = None
self.file = None
self.thread = threading.Thread(target=self.video, args=())
def create_widgets(self):
self.pack(fill=tk.BOTH, expand=True)
self.left_frame = ttk.Frame(self)
self.left_frame.pack(side=tk.LEFT, padx=10, pady=10)
self.playpause_btn = ttk.Button(self.left_frame, text="refresh", command=self.refresh)
self.playpause_btn.pack(pady=10)
self.selected = tk.StringVar()
self.selected.trace('w', self.on_folder_change)
self.opts_menu = ttk.Combobox(self.left_frame, state="readonly", values= list(self.options.keys()), textvariable=self.selected )
self.opts_menu.pack(pady=10)
self.playpause_btn = ttk.Button(self.left_frame, text="play", command=self.playpause)
self.playpause_btn.pack(pady=10)
self.playpause_btn["state"] = "disabled"
self.reset_btn = ttk.Button(self.left_frame, text="reset", command=self.reset)
self.reset_btn.pack(pady=10)
self.reset_btn["state"] = "disabled"
self.right_frame = ttk.Frame(self)
self.right_frame.pack(side=tk.RIGHT, padx=10, pady=10, fill=tk.BOTH, expand=True)
self.img = ttk.Label(self.right_frame)
self.img.pack(fill=tk.BOTH, expand=True)
def on_folder_change(self, *args):
folder = self.selected.get()
self.runvideo = False
self.playpause_btn.config(text = "play")
self.img.config(image='')
if folder == "":
self.playpause_btn["state"] = "disabled"
self.reset_btn["state"] = "disabled"
return
else:
self.playpause_btn["state"] = "normal"
self.reset_btn["state"] = "normal"
self.frame = 0
def refresh(self):
flist = [ f.path for f in os.scandir(self.path) if f.is_dir() ]
self.options = {}
for folder in flist:
name = pathlib.PurePath(folder).name
self.options[name] = folder
self.opts_menu["values"] = list(self.options.keys())
def video(self):
while True:
if self.shouldexit:
break
# only run if unpaused
if self.runvideo and self.csv is not None:
row = self.csv[ self.frame ]
# ignore header row
while row[0] == "timestamp":
self.frame += 1
row = self.csv[ self.frame ]
# open image using collumn 0 from csv
frame_path = self.selected_path + "/pics/" + self.csv[self.frame][0] + ".png"
frame_img = Image.open(frame_path)
frame_img = frame_img.crop((30, 30, 195, 108))
frame_img = frame_img.resize(( frame_img.size[0]*2 , frame_img.size[1]*2), Image.LANCZOS)
frame_widget = ImageTk.PhotoImage(frame_img)
# add image and center it
self.img.config(image=frame_widget)
self.img.place(relx=0.5, rely=0.5, anchor= tk.CENTER)
# go to next frame
self.frame += 1
# loop when data ends
try:
self.csv[ self.frame ]
except IndexError:
self.frame = 0
time.sleep(0.1)
def reset(self):
self.frame = 0
def playpause(self):
self.selected_path = self.path + "/" + self.selected.get()
if self.runvideo:
self.playpause_btn.config(text = "play")
else:
self.file = open(self.selected_path + "/info.csv", 'r', newline='')
self.csv = list(csv.reader(self.file))
self.playpause_btn.config(text = "pause")
self.runvideo = not self.runvideo
class GBARecorder:
def __init__(self, root):
self.root = root
self.root.title("GBA Recorder")
self.shouldexit = False
self.startstop = False
self.changing = False
self.thread = threading.Thread(target=self.recv, args=())
self.path = os.getcwd() + "/data"
self.csv_filename= "info.csv"
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((HOST, PORT))
self.sock.sendall( ("cmd set root '" + self.path + "'").encode("ascii") )
except:
self.sock = None
self.create_widgets()
def pausetoggle(self):
if self.startstop:
self.changing = True
self.startstopbtn.config(text = "start")
self.currtxt["state"] = "normal"
self.file.close()
if isinstance(self.sock, socket.socket):
self.sock.sendall( bytes("cmd stop", 'ascii') )
else:
self.startstopbtn.config(text = "stop")
self.currtxt["state"] = "disabled"
fpath = self.path + '/' + self.currtxt_val.get()
fppath = self.path + '/' + self.currtxt_val.get() + "/pics"
if not os.path.exists(fpath):
os.makedirs(fpath)
os.makedirs(fppath)
if not os.path.exists(fppath):
os.makedirs(fppath)
self.complete_csv_filename = self.path + '/' + self.currtxt_val.get() + '/' + self.csv_filename
exists = os.path.isfile(self.complete_csv_filename)
if exists:
self.file = open(self.complete_csv_filename, 'a', newline='')
else:
self.file = open(self.complete_csv_filename, 'w', newline='')
self.csv = csv.writer(self.file)
if not exists:
self.csv.writerow(["timestamp", "a", "b", "right", "left", "down", "rb"])
self.changing = False
if isinstance(self.sock, socket.socket):
self.sock.sendall( bytes("cmd set curr '" + self.currtxt_val.get() + "'", 'ascii') )
self.sock.sendall( bytes("cmd start", 'ascii') )
self.startstop = not self.startstop
def reconnect(self):
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((HOST, PORT))
self.connbtn["state"] = "disabled"
self.sock.sendall( ("cmd set root '" + self.path + "'").encode("ascii") )
except:
self.connbtn["state"] = "normal"
self.sock = None
def create_widgets(self):
self.tabs = ttk.Notebook(self.root)
self.currtxt_val = tk.StringVar()
self.currtxt_val.trace("w", self.changecurr)
# recording frame
self.rec_frame = ttk.Frame(self.tabs)
self.rec_frame.pack(fill=tk.BOTH, expand=True)
self.left_frame = ttk.Frame(self.rec_frame)
self.left_frame.pack(side=tk.LEFT, padx=10, pady=10)
self.currtxt = tk.Entry(self.left_frame, textvariable=self.currtxt_val)
self.currtxt.pack(pady=10)
self.startstopbtn = ttk.Button(self.left_frame, text="start", command=self.pausetoggle)
self.startstopbtn.pack(pady=10)
self.startstopbtn["state"] = "disabled"
self.connbtn = ttk.Button(self.left_frame, text="connect", command=self.reconnect)
self.connbtn.pack(pady=10)
if self.sock == None:
self.connbtn["state"] = "normal"
else:
self.connbtn["state"] = "disabled"
self.right_frame = ttk.Frame(self.rec_frame)
self.right_frame.pack(side=tk.RIGHT, padx=10, pady=10, fill=tk.BOTH, expand=True)
self.log_text = tk.Text(self.right_frame, wrap=tk.WORD)
self.log_text.pack(fill=tk.BOTH, expand=True)
# visualizing frame
self.viz_frame = Vizualizer(self)
self.tabs.add(self.rec_frame, text='record')
self.tabs.add(self.viz_frame, text='visualize')
self.tabs.pack(expand = 1, fill = "both")
def changecurr(self, *args):
if self.currtxt_val.get() != "":
self.startstopbtn["state"] = "normal"
else:
self.startstopbtn["state"] = "disabled"
def recv(self):
while True:
if self.shouldexit:
break
if self.sock == None or self.changing:
continue
try:
data = self.sock.recv(1024)
except:
self.connbtn["state"] = "normal"
self.sock = None
continue
data = data.decode('UTF-8')
data = data.split("<->")
controller = data[0]
try:
controller = int(controller)
except:
self.connbtn["state"] = "normal"
self.sock = None
continue
BUTTON_A = 1 << 0
BUTTON_B = 1 << 1
BUTTON_RIGHT = 1 << 4
BUTTON_LEFT = 1 << 5
BUTTON_DOWN = 1 << 7
BUTTON_RB = 1 << 8
buttons = [BUTTON_A, BUTTON_B, BUTTON_RIGHT, BUTTON_LEFT, BUTTON_DOWN, BUTTON_RB]
timestamp = data[1]
try:
self.sock.sendall("alive?".encode())
except:
self.connbtn["state"] = "normal"
self.sock = None
continue
row = [timestamp, 0, 0, 0, 0, 0, 0]
if data != "":
self.log_text.insert(tk.END, timestamp)
for idx, btn in enumerate(buttons):
if controller & btn:
row[idx + 1] = 1
if self.sock == None or self.changing:
continue
self.csv.writerow(row)
self.log_text.insert(tk.END, "\n")
self.log_text.see(tk.END)
def exit(self):
self.shouldexit = True
self.viz_frame.shouldexit = True
def run(self):
self.thread.start()
self.viz_frame.thread.start()
self.root.mainloop()
self.exit()
def main():
root = tk.Tk()
app = GBARecorder(root)
app.run()
if __name__ == "__main__":
main()