forked from 3C-SCSU/Avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI5.py
More file actions
762 lines (642 loc) · 29.4 KB
/
Copy pathGUI5.py
File metadata and controls
762 lines (642 loc) · 29.4 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import sys
import os
import subprocess
from pathlib import Path
from PySide6.QtWidgets import QApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import QObject, Signal, Slot, QProcess, QUrl
from pdf2image import convert_from_path
from djitellopy import Tello
import threading
import random
import re
import pandas as pd
import time
import io
import urllib.parse
import contextlib
from collections import defaultdict
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds
from GUI5_ManualDroneControl.cameraview.camera_controller import CameraController
from NAO6.nao_connection import send_command
from PySide6.QtCore import Property
# from Developers.hofCharts import main as hofCharts, ticketsByDev_text NA
from Developers import devCharts
# Import BCI connection for brainwave prediction
try:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'random-forest-prediction')))
from client.brainflow1 import bciConnection, DataMode
BCI_AVAILABLE = True
except ImportError as e:
print(f"Warning: BCI connection not available: {e}")
BCI_AVAILABLE = False
# Add the parent directory to the Python path for file-shuffler
sys.path.append(str(Path(__file__).resolve().parent / "file-shuffler"))
sys.path.append(str(Path(__file__).resolve().parent / "file-unify-labels"))
sys.path.append(str(Path(__file__).resolve().parent / "file-remove8channel"))
import unifyTXT
import run_file_shuffler
import remove8channel
class TabController(QObject):
def __init__(self):
super().__init__()
self.nao_process = None
class BrainwavesBackend(QObject):
# Define signals to update QML components
flightLogUpdated = Signal(list)
predictionsTableUpdated = Signal(list)
imagesReady = Signal(list)
logMessage = Signal(str)
naoStarted = Signal()
naoEnded = Signal()
isConnectedChanged = Signal()
@Slot()
def startNaoManual(self):
print("Nao6 Manual session started")
self.naoStarted.emit("Nao6 Manual session started")
@Slot()
def stopNaoManual(self):
print("Nao6 Manual session ended")
self.naoEnded.emit("Nao6 Manual session ended")
@Slot(str, str)
def connectNao(self, ip="192.168.23.53", port="9559"):
"""Connect to NAO robot with specified IP and Port"""
try:
# Log the connection attempt
self.flight_log.insert(0, f"Attempting to connect to NAO at {ip}:{port}...")
self.flightLogUpdated.emit(self.flight_log)
# Send connect command
if send_command("connect"):
self.flight_log.insert(0, f"Nao connected successfully at {ip}:{port}.")
else:
self.flight_log.insert(0, f"Nao failed to connect at {ip}:{port}.")
except Exception as e:
self.flight_log.insert(0, f"Error connecting to NAO: {str(e)}")
self.flightLogUpdated.emit(self.flight_log)
@Slot()
def nao_sit_down(self):
if send_command("sit_down"):
self.flight_log.insert(0, "Sitting down.")
else:
self.flight_log.insert(0, "Nao failed to sit.")
self.flightLogUpdated.emit(self.flight_log)
@Slot()
def nao_stand_up(self):
if send_command("stand_up"):
self.flight_log.insert(0, "Standing Up.")
else:
self.fligt_log.insert(0, "Nao failed to stand up.")
self.flightLogUpdated.emit(self.flight_log)
@Slot(result=str)
def getDevList(self):
exclude = {
"3C Cloud Computing Club <114175379+3C-SCSU@users.noreply.github.com>",
}
proc = subprocess.run(
["git", "shortlog", "-sne", "--all"],
capture_output=True, text=True, encoding="utf-8", errors="ignore"
)
if proc.returncode != 0:
return "No developers found."
lines = proc.stdout.strip().splitlines()
filtered_lines = []
for line in lines:
# Match the author portion
match = re.match(r"^\s*\d+\s+(?P<author>.+)$", line)
if match:
author = match.group("author").strip()
if author not in exclude:
filtered_lines.append(line)
return "\n".join(filtered_lines) if filtered_lines else "No developers found."
@Slot(result=str)
def getTicketsByDev(self) -> str:
exclude = {
"3C Cloud Computing Club <114175379+3C-SCSU@users.noreply.github.com>"
}
pretty = "%x1e%an <%ae>%x1f%s%x1f%b"
try:
proc = subprocess.run(
["git", "log", "--all", f"--pretty=format:{pretty}"],
capture_output=True,
text=True,
encoding="utf-8",
errors="ignore",
check=True
)
except subprocess.CalledProcessError:
return "No tickets found."
raw = proc.stdout or ""
commits = raw.split("\x1e")
jira_re = re.compile(r'\b([A-Za-z]{2,}-\d+)\b')
hash_re = re.compile(r'(?<![A-Za-z0-9])#\d+\b')
author_to_ticketset = defaultdict(set)
for entry in commits:
entry = entry.strip()
if not entry:
continue
parts = entry.split("\x1f", 2)
author = parts[0].strip()
subject = parts[1] if len(parts) > 1 else ""
body = parts[2] if len(parts) > 2 else ""
msg = (subject + "\n" + body).strip()
found = set()
for m in jira_re.findall(msg):
found.add(m.upper())
for m in hash_re.findall(msg):
found.add(m)
if found:
author_to_ticketset[author].update(found)
if not author_to_ticketset:
return "No tickets found."
# Sort authors by ticket count descending, then by name
lines = []
for author, tickets in sorted(author_to_ticketset.items(), key=lambda kv: (-len(kv[1]), kv[0].lower())):
if author in exclude:
continue # skip excluded authors entirely
lines.append(f"{author}: {', '.join(sorted(tickets))}")
return "\n".join(lines)
@Slot()
def devChart(self):
print("hofChart() SLOT CALLED")
try:
devCharts.main()
print("hofCharts.main() COMPLETED")
except Exception as e:
print(f"hofCharts.main() ERROR: {e}")
#might be useless because is.connected is defined already in the def__init method
def get_is_connected(self):
return self.isConnectedChanged
def set_is_connected(self, value):
if self.is_connected != value:
self.is_connected = value
self.isConnectedChanged.emit()
is_connected_prop = Property(bool, get_is_connected, set_is_connected, notify=isConnectedChanged)
def __init__(self):
super().__init__()
self.flight_log = [] # List to store flight log entries
self.predictions_log = [] # List to store prediction records
self.current_prediction_label = ""
self.current_model = "Random Forest" # Default model
self.current_framework = "PyTorch" # Default framework
self.image_paths = [] # Store converted image paths
self.plots_dir = os.path.abspath("plotscode/plots") # Base plots directory
self.current_dataset = "refresh" # Default dataset to display
self.is_connected = False
try:
self.tello = Tello()
self.connected = False
except Exception as e:
print(f"Warning: Failed to initialize Tello drone: {e}")
self.logMessage.emit(f"Warning: Failed to initialize Tello drone: {e}")
# Initialize camera controller with tello instance
self.camera_controller = CameraController()
if hasattr(self, 'tello'):
self.camera_controller.set_tello_instance(self.tello)
# Initialize BCI connection for brainwave prediction
if BCI_AVAILABLE:
try:
self.bcicon = bciConnection.get_instance()
print("BCI connection initialized successfully")
except Exception as e:
print(f"Warning: Failed to initialize BCI connection: {e}")
self.bcicon = None
else:
self.bcicon = None
@Slot(str)
def selectModel(self, model_name):
""" Select the machine learning model """
self.logMessage.emit(f"Model selected: {model_name}")
self.current_model = model_name
self.flight_log.insert(0, f"Selected Model: {model_name}")
self.flightLogUpdated.emit(self.flight_log)
@Slot(str)
def selectFramework(self, framework_name):
""" Select the machine learning framework """
self.logMessage.emit(f"Framework selected: {framework_name}")
self.current_framework = framework_name
self.flight_log.insert(0, f"Selected Framework: {framework_name}")
self.flightLogUpdated.emit(self.flight_log)
@Slot()
def readMyMind(self):
""" Runs the selected model and processes the brainwave data. """
if self.current_model == "Random Forest":
if self.current_framework == "PyTorch":
prediction = self.run_random_forest_pytorch()
elif self.current_framework == "TensorFlow":
prediction = self.run_random_forest_tensorflow()
elif self.current_framework == "JAX":
prediction = self.run_random_forest_jax()
else: # Deep Learning
if self.current_framework == "PyTorch":
prediction = self.run_deep_learning_pytorch()
elif self.current_framework == "TensorFlow":
prediction = self.run_deep_learning_tensorflow()
elif self.current_framework == "JAX":
prediction = self.run_deep_learning_jax()
self.logMessage.emit(f"Prediction received: {prediction}")
# Set current prediction
self.current_prediction_label = prediction
# Log the prediction
self.predictions_log.append({
"count": str(len(self.predictions_log) + 1),
"server": "Brainwave AI",
"label": prediction
})
self.predictionsTableUpdated.emit(self.predictions_log)
# Update Flight Log
self.flight_log.insert(0, f"Executed: {prediction} (Model: {self.current_model}, Framework: {self.current_framework})")
self.flightLogUpdated.emit(self.flight_log)
def run_random_forest_pytorch(self):
""" Random Forest model processing with PyTorch backend """
print("Running Random Forest Model with PyTorch...")
try:
# Use the BCI connection to get real brainwave data
if hasattr(self, 'bcicon') and self.bcicon:
prediction_response = self.bcicon.bciConnectionController()
if prediction_response:
return prediction_response.get('prediction_label', 'forward')
except Exception as e:
print(f"Error with PyTorch Random Forest: {e}")
# Fallback to simulation with PyTorch-specific labels
time.sleep(1)
return random.choice(["forward", "backward", "left", "right", "takeoff", "land"])
def run_random_forest_tensorflow(self):
""" Random Forest model processing with TensorFlow backend """
print("Running Random Forest Model with TensorFlow...")
try:
# Use the BCI connection to get real brainwave data
if hasattr(self, 'bcicon') and self.bcicon:
prediction_response = self.bcicon.bciConnectionController()
if prediction_response:
return prediction_response.get('prediction_label', 'forward')
except Exception as e:
print(f"Error with TensorFlow Random Forest: {e}")
# Fallback to simulation with TensorFlow-specific labels
time.sleep(1)
return random.choice(["forward", "backward", "left", "right", "takeoff", "land"])
def run_random_forest_jax(self):
""" Random Forest model processing with JAX backend """
print("Running Random Forest Model with JAX...")
try:
#use the BCI connection to get real brainwave data
if hasattr(self, 'bcicon') and self.bcicon:
prediction_response = self.bcicon.bciConnectionController()
if prediction_response:
return prediction_response.get('prediction_label', 'forward')
except Exception as e:
print(f"Error with Jax Random Forest: {e}")
# Fallback to simulation with Jax-specific labels
time.sleep(1)
return random.choice(["forward", "backward", "left", "right", "takeoff", "land"])
def run_deep_learning_pytorch(self):
""" Deep Learning model processing with PyTorch backend """
print("Running Deep Learning Model with PyTorch...")
try:
# Simulate PyTorch deep learning model processing
# In a real implementation, this would load and run a PyTorch CNN model
time.sleep(2) # Simulate longer processing time for deep learning
return random.choice(["forward", "backward", "left", "right", "takeoff", "land", "up", "down"])
except Exception as e:
print(f"Error with PyTorch Deep Learning: {e}")
return "forward"
def run_deep_learning_tensorflow(self):
""" Deep Learning model processing with TensorFlow backend """
print("Running Deep Learning Model with TensorFlow...")
try:
# Simulate TensorFlow deep learning model processing
# In a real implementation, this would load and run a TensorFlow CNN model
time.sleep(2) # Simulate longer processing time for deep learning
return random.choice(["forward", "backward", "left", "right", "takeoff", "land", "up", "down"])
except Exception as e:
print(f"Error with TensorFlow Deep Learning: {e}")
return "forward"
def run_deep_learning_jax(self):
""" Deep Learning model processing with Jax """
print("Running Deep Learning Model with Jax...")
try:
# Simulate Jax deep learning model processing
# In a real implementation, this would load and run a Jax CNN model
time.sleep(2) # Simulate longer processing time for deep learning
return random.choice(["forward", "backward", "left", "right", "takeoff", "land", "up", "down"])
except Exception as e:
print(f"Error with Jax Deep Learning: {e}")
return "forward"
@Slot(str)
def notWhatIWasThinking(self, manual_action):
# Handle manual action input
self.predictions_log.append({
"count": "manual",
"server": "manual",
"label": manual_action
})
self.predictionsTableUpdated.emit(self.predictions_log)
# Also update flight log
self.flight_log.insert(0, f"Manual Action: {manual_action}")
self.flightLogUpdated.emit(self.flight_log)
self.logMessage.emit(f"Manual input: {manual_action}")
@Slot()
def executeAction(self):
# Execute the current prediction
if self.current_prediction_label:
self.flight_log.insert(0, f"Executed: {self.current_prediction_label}")
self.flightLogUpdated.emit(self.flight_log)
self.logMessage.emit(f"Executed action: {self.current_prediction_label}")
@Slot()
def connectDrone(self):
# Mock function to simulate drone connection
self.flight_log.insert(0, "Drone connected.")
self.flightLogUpdated.emit(self.flight_log)
self.logMessage.emit("Drone connected.")
@Slot()
def keepDroneAlive(self):
# Mock function to simulate sending keep-alive signal
self.flight_log.insert(0, "Keep alive signal sent.")
self.flightLogUpdated.emit(self.flight_log)
@Slot(str)
def getDroneAction(self, action):
threading.Thread(target=self._doAction, args=(action,), daemon=True).start()
def _doAction(self, action):
try:
if action == 'connect':
self.tello.connect()
self.connected = True
self.logMessage.emit("Connected to Tello Drone")
elif self.connected:
self.logMessage.emit("Drone did not connect.")
return
if action == 'up':
self.tello.move_up(30)
self.logMessage.emit("Moving up")
elif action == 'down':
self.tello.move_down(30)
self.logMessage.emit("Moving down")
elif action == 'forward':
self.tello.move_forward(30)
self.logMessage.emit("Moving forward")
elif action == 'backward':
self.tello.move_back(30)
self.logMessage.emit("Moving backward")
elif action == 'left':
self.tello.move_left(30)
self.logMessage.emit("Moving left")
elif action == 'right':
self.tello.move_right(30)
self.logMessage.emit("Moving right")
elif action == 'turn_left':
self.tello.rotate_counter_clockwise(45)
self.logMessage.emit("Rotating left")
elif action == 'turn_right':
self.tello.rotate_clockwise(45)
self.logMessage.emit("Rotating right")
elif action == 'takeoff':
self.tello.takeoff()
self.logMessage.emit("Taking off")
elif action == 'land':
self.tello.land()
self.logMessage.emit("Landing")
elif action == 'go_home':
self.go_home()
elif action == 'stream':
if hasattr(self, 'camera_controller'):
self.camera_controller.start_camera_stream()
self.logMessage.emit("Starting camera stream")
else:
self.logMessage.emit("Camera controller not available")
else:
self.logMessage.emit("Unknown action")
except Exception as e:
self.logMessage.emit(f"Error during {action}: {e}")
# Method for returning to home (an approximation)
def go_home(self):
# Assuming the home action means moving backward and upwards
self.tello.move_back(50) # Move back to home point (adjust distance as needed)
self.tello.move_up(50) # Move up to avoid obstacles
self.logMessage.emit("Returning to home")
@Slot()
def check_plots_exist(self):
"""
Check if all necessary plot PDFs exist in both Rollback and Refresh directories.
If not, run controller.py to generate them.
"""
print("\n=== CHECKING IF PLOTS EXIST ===")
# Create plots base directory if it doesn't exist
plots_base_dir = Path(self.plots_dir)
if not plots_base_dir.exists():
print(f"Creating plots base directory: {plots_base_dir}")
plots_base_dir.mkdir(parents=True, exist_ok=True)
# List of datasets to check
datasets = ["rollback", "refresh"]
# List of PDF files that should exist for each dataset
pdf_files = [
"takeoff_plots.pdf", "forward_plots.pdf", "right_plots.pdf",
"land_plots.pdf", "backward_plots.pdf", "left_plots.pdf"
]
# Check if all directories and PDFs exist
missing_pdfs = False
for dataset in datasets:
dataset_dir = plots_base_dir / dataset
if not dataset_dir.exists():
print(f"Creating dataset directory: {dataset_dir}")
dataset_dir.mkdir(parents=True, exist_ok=True)
missing_pdfs = True
continue
print(f"Checking PDFs in {dataset_dir}...")
for pdf_file in pdf_files:
pdf_path = dataset_dir / pdf_file
if not pdf_path.exists():
print(f"Missing file: {pdf_path}")
missing_pdfs = True
break
# If any PDFs are missing, run the controller.py script
if missing_pdfs:
print("Some plot files are missing. Running controller.py to generate them...")
# Get the path to controller.py in the plotscode directory
controller_path = Path(self.plots_dir).parent / "controller.py" # plotscode/controller.py
print(f"Controller path: {controller_path}")
print(f"Controller exists: {controller_path.exists()}")
if controller_path.exists():
try:
# Change to the plotscode directory before running the script
original_dir = os.getcwd()
os.chdir(controller_path.parent)
# Run the controller.py script to generate plots for both datasets
print(f"Executing: {sys.executable} {controller_path}")
result = subprocess.run(
[sys.executable, str(controller_path)],
check=True,
capture_output=True,
text=True
)
# Go back to the original directory
os.chdir(original_dir)
# Print output for debugging
print(f"Output: {result.stdout}")
if result.stderr:
print(f"Errors: {result.stderr}")
print("Successfully generated plot files.")
return True
except subprocess.CalledProcessError as e:
print(f"Error running controller.py: {e}")
if hasattr(e, 'stderr'):
print(f"Error output: {e.stderr}")
return False
except Exception as e:
print(f"Unexpected error: {str(e)}")
return False
else:
print(f"Controller script not found: {controller_path}")
return False
return True # All files exist
@Slot(str)
def setDataset(self, dataset_name):
"""
Set the current dataset to display (refresh or rollback).
:param dataset_name: Name of the dataset ('refresh' or 'rollback')
"""
if dataset_name.lower() in ["refresh", "rollback"]:
self.current_dataset = dataset_name.lower()
print(f"Switched to {self.current_dataset} dataset")
# Update the displayed images
self.convert_pdfs_to_images()
else:
print(f"Invalid dataset name: {dataset_name}")
@Slot()
def convert_pdfs_to_images(self):
"""
Convert PDF files from the current dataset to images and send to QML.
"""
print(f"\n=== STARTING CONVERT PDFS TO IMAGES FOR {self.current_dataset.upper()} ===")
# First check if all plot PDFs exist, and generate them if needed
success = self.check_plots_exist()
print(f"Result of check_plots_exist: {success}")
# Current dataset directory
dataset_dir = Path(self.plots_dir) / self.current_dataset
# Convert PDF files to images and send image paths + graph names to QML.
self.image_paths = []
graph_titles = ["Takeoff", "Forward", "Right",
"Landing", "Backward", "Left"]
# Load files in the correct order
pdf_files = [
"takeoff_plots.pdf", "forward_plots.pdf", "right_plots.pdf",
"land_plots.pdf", "backward_plots.pdf", "left_plots.pdf"
]
for i, pdf_file in enumerate(pdf_files):
pdf_path = dataset_dir / pdf_file
if not pdf_path.exists():
print(f"Missing file: {pdf_path}") # Debugging: Check missing PDFs
continue # Skip if file does not exist
images = convert_from_path(str(pdf_path), dpi=150) # Convert PDF to image
image_path = dataset_dir / f"{pdf_file.replace('.pdf', '.png')}"
images[0].save(str(image_path), "PNG") # Save first page as an image
# Debugging: Print the generated image path
print(f"Generated image: {image_path}")
self.image_paths.append({
"graphTitle": graph_titles[i],
"imagePath": QUrl.fromLocalFile(str(image_path)).toString()
})
# Debugging: Print final list of image paths
print("Final Image Paths Sent to QML:", self.image_paths)
self.imagesReady.emit(self.image_paths) # Send data to QML
@Slot()
def launch_file_shuffler_gui(self):
# Launch the file shuffler GUI program
file_shuffler_path = Path(__file__).resolve().parent / "file-shuffler/file-shuffler-gui.py"
subprocess.Popen(["python", str(file_shuffler_path)])
@Slot(str, result=str)
def run_file_shuffler_program(self, path):
# Need to parse the path as the FolderDialog appends file:// in front of the selection
path = path.replace("file://", "")
if path.startswith("/C:"):
path = 'C' + path[2:]
response = run_file_shuffler.main(path)
return response
# Adding Synthetic Data and Live Data Logic (Row 327 to 355) as part of Ticket 186
@Slot(str, result=str)
def unify_thoughts(self, base_dir):
"""
Called from QML when the user picks a directory.
"""
# strip file:/// if necessary
path = base_dir.replace("file://", "")
if base_dir.startswith("file:///"):
base_dir = urllib.parse.unquote(base_dir.replace("file://", ""))
if os.name == 'nt' and base_dir.startswith("/"):
base_dir = base_dir[1:]
print("Unify Thoughts on directory:", base_dir)
output = io.StringIO()
try:
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output):
unifyTXT.move_any_txt_files(base_dir)
print("Unify complete.")
except Exception as e:
print("Error during unify:", e)
return output.getvalue()
@Slot(str, result=str)
def remove_8_channel(self, base_dir):
"""
Called from QML when the user picks a directory to remove 8 channel data.
"""
# Decode URL path
if base_dir.startswith("file:///"):
base_dir = urllib.parse.unquote(base_dir.replace("file://", ""))
if os.name == 'nt' and base_dir.startswith("/"):
base_dir = base_dir[1:]
print("Removing 8 Channel data form:", base_dir)
output = io.StringIO()
try:
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output):
remove8channel.file_remover(base_dir)
print("8 Channel Data Removal complete.")
except Exception as e:
print("Error during cleanup: ", e)
@Slot(str)
def setDataMode(self, mode):
"""
Set data mode to either synthetic or live based on radio button selection.
"""
if mode == "synthetic":
self.init_synthetic_board()
self.logMessage.emit("Switched to Synthetic Data Mode")
elif mode == "live":
self.init_live_board()
self.logMessage.emit("Switched to Live Data Mode")
else:
print(f"Unknown data mode: {mode}")
def init_synthetic_board(self):
""" Initialize BrainFlow with synthetic board for testing """
params = BrainFlowInputParams()
self.board = BoardShim(BoardIds.SYNTHETIC_BOARD.value, params)
print("\nSynthetic board initialized.")
def init_live_board(self):
""" Initialize BrainFlow with a real headset """
params = BrainFlowInputParams()
params.serial_port = "/dev/cu.usbserial-D200PMA1" # Update if different on your system
self.board = BoardShim(BoardIds.CYTON_DAISY_BOARD.value, params)
print("\nLive headset board initialized.")
if __name__ == "__main__":
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Fusion"
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
# Create our controllers
tab_controller = TabController()
print("TabController created")
# Initialize backend before loading QML
backend = BrainwavesBackend()
engine.rootContext().setContextProperty("tabController", tab_controller)
engine.rootContext().setContextProperty("backend", backend)
engine.rootContext().setContextProperty("imageModel", []) # Initialize empty model
engine.rootContext().setContextProperty("fileShufflerGui", backend) # For file shuffler
engine.rootContext().setContextProperty("cameraController", backend.camera_controller)
print("Controllers exposed to QML")
engine.rootContext().setContextProperty("fileShufflerGui", backend) # For file shuffler
# Load QML
qml_file = Path(__file__).resolve().parent / "main.qml"
engine.load(str(qml_file))
# Convert PDFs after engine load
try:
backend.convert_pdfs_to_images()
except Exception as e:
print(f"Error converting PDFs: {str(e)}")
# Ensure image model updates correctly
backend.imagesReady.connect(lambda images: engine.rootContext().setContextProperty("imageModel", images))
sys.exit(app.exec())