Skip to content

Commit

Permalink
added utils file and moved main.py out of App folder and into top folder
Browse files Browse the repository at this point in the history
  • Loading branch information
SkaisteMotiejunaite committed Jan 9, 2025
1 parent 10cf2c1 commit dd49e42
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 61 deletions.
17 changes: 4 additions & 13 deletions App/pages/facial_expression_page.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Facial expression page opened from home_Page.ui"""
import sys
import os
import cv2
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import QTimer, Qt

sys.path.append(os.path.abspath("C:\\Users\\skais\\ThesisProject\\DevCode"))
from Algorithms.Body.emotion_recognition import EmotionRecognizer
from utils import load_stylesheet, close_event

class FacialExpressionRecognitionPage(QWidget):
"""Emotion Recognition"""
Expand All @@ -30,7 +28,7 @@ def __init__(self):
self.timer.timeout.connect(self.update_frame)
self.timer.start(30)

self.load_stylesheet()
load_stylesheet(self,'App/styles/facial_expression.qss')

def setup_ui(self):
"""setup face expression page ui"""
Expand Down Expand Up @@ -63,12 +61,6 @@ def setup_ui(self):
main_layout.addLayout(right_layout)
self.setLayout(main_layout)

def load_stylesheet(self):
"""Load the stylesheet for the page."""
with open("App/styles/facial_expression.qss", "r") as file:
stylesheet = file.read()
self.setStyleSheet(stylesheet)

def update_frame(self):
"""update frames from video stream and detected emotion"""
result = self.expression_recognizer.process_frame()
Expand All @@ -89,7 +81,6 @@ def _convert_cv_to_qt(self, cv_img):
qt_image = QImage(rgb_image.data, w, h, ch * w, QImage.Format_RGB888)
return QPixmap.fromImage(qt_image)

def close_event(self, event):
def call_close_event(self, event):
"""handle close event to release resources"""
self.expression_recognizer.release()
event.accept()
close_event(event,self)
28 changes: 5 additions & 23 deletions App/pages/general_page.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"""Page that is used when only a stream is outputted, object detection, colour detection etc"""
# Add the DevCode directory to the Python path
from os import close
import sys
import os
import cv2
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage

sys.path.append(os.path.abspath("C:\\Users\\skais\\ThesisProject\\DevCode"))
from Algorithms.Objects.colour_detection import ColourRecognizer
from Algorithms.Objects.object_detection import ObjectRecognizer
from utils import load_stylesheet,close_event

class GeneralDemoPage(QWidget):
"""page used for general display of streams"""
Expand Down Expand Up @@ -51,7 +49,7 @@ def __init__(self, title: str, description: str, algorithm: str):
# Set the main layout
self.setLayout(main_layout)

self.load_stylesheet()
load_stylesheet(self,'App\styles\general.qss')

# Start video capture
self.cap = cv2.VideoCapture(0)
Expand All @@ -62,12 +60,6 @@ def __init__(self, title: str, description: str, algorithm: str):
self.failed_frames = 0 # Counter for consecutive failed frames
self.timer = self.startTimer(20)

def load_stylesheet(self):
"""load stylesheet"""
with open("App\styles\general.qss","r") as file:
stylesheet=file.read()
self.setStyleSheet(stylesheet)

def timer_event(self):
"""handle timer event for capturing and processing frames"""
ret, frame = self.cap.read()
Expand Down Expand Up @@ -98,16 +90,6 @@ def timer_event(self):
pixmap = QPixmap.fromImage(qimg)
self.video_label.setPixmap(pixmap)

def close_event(self, event):
def call_close_event(self, event):
"""handle close event to release resources"""
# Release the video capture and stop the algorithm
if hasattr(self, 'cap') and self.cap.isOpened():
self.cap.release()

print("Video stream and resources have been released.")

# Stop the timer
self.killTimer(self.timer)

# Accept the close event
event.accept()
close_event(event,self)
17 changes: 4 additions & 13 deletions App/pages/hand_gesture_page.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""page for hand gesture detection, opened from home_page.py button"""
import sys
import os
import cv2
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import QTimer, Qt

sys.path.append(os.path.abspath("C:\\Users\\skais\\ThesisProject\\DevCode"))
from Algorithms.Body.hand_gesture_test import GestureRecognizer
from utils import load_stylesheet,close_event

class HandGestureRecognitionPage(QWidget):
"""Hand gesture recognizer"""
Expand All @@ -29,7 +27,7 @@ def __init__(self):
self.timer.timeout.connect(self.update_frame)
self.timer.start(30)

self.load_stylesheet()
load_stylesheet(self,'App/styles/hand_gesture.qss')

def setup_ui(self):
"""UI for hand page setup"""
Expand Down Expand Up @@ -72,12 +70,6 @@ def setup_ui(self):
main_layout.addLayout(right_layout)
self.setLayout(main_layout)

def load_stylesheet(self):
"""Load the stylesheet for the page."""
with open("App/styles/hand_gesture.qss", "r") as file:
stylesheet = file.read()
self.setStyleSheet(stylesheet)

def update_frame(self):
"""update the video feed and hand gesture icons"""
result = self.gesture_recognizer.process_frame()
Expand All @@ -100,7 +92,6 @@ def _convert_cv_to_qt(self, cv_img):
return QPixmap.fromImage(qt_image)


def close_event(self, event):
def call_close_event(self, event):
"""handle close event to release resources"""
self.gesture_recognizer.release()
event.accept()
close_event(event,self)
16 changes: 5 additions & 11 deletions App/pages/home_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from PyQt5.QtGui import QPixmap, QPainter, QColor
from PyQt5 import uic

from .hand_gesture_page import HandGestureRecognitionPage
from .facial_expression_page import FacialExpressionRecognitionPage
from .general_page import GeneralDemoPage

from App.pages.hand_gesture_page import HandGestureRecognitionPage
from App.pages.facial_expression_page import FacialExpressionRecognitionPage
from App.pages.general_page import GeneralDemoPage
from utils import load_stylesheet
class HomePage(QMainWindow):
"""Home page called from main.py"""
def __init__(self):
Expand Down Expand Up @@ -41,13 +41,7 @@ def __init__(self):
self.connection_timer.timeout.connect(self.update_connection_status)
self.connection_timer.start(2000) # Check every 2 seconds

self.load_stylesheet()

def load_stylesheet(self):
"""Load the stylesheet for the page."""
with open("App/styles/base.qss", "r") as file:
stylesheet = file.read()
self.setStyleSheet(stylesheet)
load_stylesheet(self,'App/styles/base.qss')

def draw_circle(self, color, size=20):
"""Draw a circle with the specified color and size."""
Expand Down
2 changes: 1 addition & 1 deletion App/main.py → main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""main file to start desktop application"""
import sys
from PyQt5.QtWidgets import QApplication
from pages.home_page import HomePage
from App.pages.home_page import HomePage

if __name__ == "__main__":
app = QApplication(sys.argv)
Expand Down
9 changes: 9 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def load_stylesheet(window,styleSheetPath):
"""Load the stylesheet for the page."""
with open(styleSheetPath, "r") as file:
window.setStyleSheet(file.read())

def close_event(event,recogniser):
"""handle close event to release resources"""
recogniser.release()
event.accept()

0 comments on commit dd49e42

Please sign in to comment.