Skip to content

Commit

Permalink
Merge pull request #69 from alex5105/screen_resolution
Browse files Browse the repository at this point in the history
Fix screen resoultion error and change face recognition interval to 50 ms
  • Loading branch information
willwade authored Oct 4, 2024
2 parents 6605768 + 68d89c3 commit 3e07110
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
4 changes: 2 additions & 2 deletions FaceCommander.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, tk_root):
self.is_active = True

# Enter loop
self.tk_root.after(1, self.anim_loop)
self.tk_root.after(50, self.anim_loop)

def anim_loop(self):
try:
Expand All @@ -60,7 +60,7 @@ def anim_loop(self):

# Run detectors and controllers.
self.pipeline_tick()
self.tk_root.after(1, self.anim_loop)
self.tk_root.after(50, self.anim_loop)
except Exception as e:
logging.critical(e, exc_info=e)

Expand Down
46 changes: 30 additions & 16 deletions src/gui/frames/frame_cam_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from PIL import Image, ImageDraw
import threading
import platform
import ctypes

CANVAS_WIDTH = 320
CANVAS_HEIGHT = 240
Expand Down Expand Up @@ -46,7 +47,7 @@ def __init__(self, master, master_callback: callable, **kwargs):
bd=0,
relief='ridge',
highlightthickness=0)
self.canvas.grid(row=0, column=0, padx=10, pady=10, sticky="sw")
self.canvas.grid(row=0, column=0, padx=10, pady=0, sticky="sw")

# Toggle label
self.toggle_label = customtkinter.CTkLabel(master=self,
Expand Down Expand Up @@ -96,7 +97,7 @@ def __init__(self, master, master_callback: callable, **kwargs):
self.minimize_label.grid(row=1,
column=0,
padx=(10, 0),
pady=30,
pady=(30, 0),
sticky="nw")

# Toggle switch
Expand All @@ -119,22 +120,35 @@ def __init__(self, master, master_callback: callable, **kwargs):
self.minimize_switch.grid(row=1,
column=0,
padx=(100, 0),
pady=30,
pady=(30, 0),
sticky="nw")

# Toggle description label
self.toggle_label = customtkinter.CTkLabel(
master=self,
compound='right',
text="Allow facial gestures to control\nyour actions. ",
text_color="#444746",
justify=tkinter.LEFT)
self.toggle_label.cget("font").configure(size=12)
self.toggle_label.grid(row=2,
column=0,
padx=(10, 0),
pady=5,
sticky="nw")

hdc = ctypes.windll.user32.GetDC(0)

# Get the screen DPI
dpi_x = ctypes.windll.gdi32.GetDeviceCaps(hdc, 88) # 88 is the LOGPIXELSX value for horizontal DPI

# Release the device context (DC)
ctypes.windll.user32.ReleaseDC(0, hdc)

# 96 DPI is the default for 100% scaling, so scaling is DPI / 96
scaling_percentage = dpi_x / 96 * 100

if int(self.tk_root.winfo_screenwidth() * 100 / scaling_percentage) > 1280:
# Toggle description label
self.toggle_label = customtkinter.CTkLabel(
master=self,
compound='right',
text="Allow facial gestures to control\nyour actions. ",
text_color="#444746",
justify=tkinter.LEFT)
self.toggle_label.cget("font").configure(size=12)
self.toggle_label.grid(row=2,
column=0,
padx=(10, 0),
pady=5,
sticky="nw")

# Set first image.
self.canvas_image = self.canvas.create_image(0,
Expand Down
19 changes: 15 additions & 4 deletions src/gui/main_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tkinter import IntVar, StringVar

import customtkinter

import ctypes
from src.app import App
from src.update_manager import UpdateManager
from src.gui import frames
Expand All @@ -28,16 +28,27 @@ def __init__(self, tk_root):
self.tk_root = tk_root

self.tk_root.bind('<Control-x>', self.switch_cursor)

hdc = ctypes.windll.user32.GetDC(0)

# Get the screen DPI
dpi_x = ctypes.windll.gdi32.GetDeviceCaps(hdc, 88) # 88 is the LOGPIXELSX value for horizontal DPI

# Release the device context (DC)
ctypes.windll.user32.ReleaseDC(0, hdc)

# 96 DPI is the default for 100% scaling, so scaling is DPI / 96
scaling_percentage = dpi_x / 96 * 100
# Get screen width and height for dynamic scaling
screen_width = self.tk_root.winfo_screenwidth()
screen_height = self.tk_root.winfo_screenheight()
screen_width = int(self.tk_root.winfo_screenwidth() * 100 / scaling_percentage)
screen_height = int(self.tk_root.winfo_screenheight() * 100 / scaling_percentage)
self.bounday_width = 800
self.sidebar_big_size = 260
self.sidebar_small_size = 60

# Set window size based on screen dimensions for tablets
if screen_width <= 1280:
self.tk_root.geometry(f"{int(screen_width * 0.9)}x{int(screen_height * 0.9)}")
self.tk_root.geometry(f"{int(screen_width * 0.9)}x{int(screen_height * 0.9)}+0+0")
else:
self.tk_root.geometry("1024x800")

Expand Down
2 changes: 1 addition & 1 deletion src/gui/pages/page_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(
self.shared_info_balloon = Balloon(
self, image_path="assets/images/balloon.png")

self.shared_dialog = Select_Facial_Gesture(self, shape_list.available_gestures, width=750, callback=self.dialog_callback)
self.shared_dialog = Select_Facial_Gesture(self, shape_list.available_gestures, width=650, callback=self.dialog_callback)

self.help_icon = customtkinter.CTkImage(
Image.open("assets/images/help.png").resize(HELP_ICON_SIZE),
Expand Down
2 changes: 1 addition & 1 deletion src/gui/pages/page_select_gestures.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
Image.open("assets/images/help.png").resize(HELP_ICON_SIZE),
size=HELP_ICON_SIZE)

self.shared_dialog = Select_Facial_Gesture(self, shape_list.available_gestures, width=750, callback=self.dialog_callback)
self.shared_dialog = Select_Facial_Gesture(self, shape_list.available_gestures, width=650, callback=self.dialog_callback)
# Divs
self.divs = self.create_divs(shape_list.available_actions_keys,
shape_list.available_gestures_keys)
Expand Down
23 changes: 18 additions & 5 deletions src/gui/select_facial_gesture.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import customtkinter
from PIL import Image
from src.config_manager import ConfigManager
import ctypes

ICON_SIZE = (40, 40) # Adjust icon size as needed
IMAGE_SIZE = (100, 70)
Expand Down Expand Up @@ -36,7 +37,7 @@ def open(self, div_name, used_gestures):
self.dialog_window.resizable(True, True)

# Center the dialog on the parent window
self.center_window(self.dialog_window, self.width, 550)
self.center_window(self.dialog_window, self.width, 500)

# Set dialog as modal
self.dialog_window.grab_set() # Block interaction with other windows
Expand Down Expand Up @@ -106,10 +107,22 @@ def open(self, div_name, used_gestures):

def center_window(self, window, width, height):
"""Center the window on the parent window."""
parent_x = self.master.winfo_rootx()
parent_y = self.master.winfo_rooty()
parent_width = self.master.winfo_width()
parent_height = self.master.winfo_height()

hdc = ctypes.windll.user32.GetDC(0)

# Get the screen DPI
dpi_x = ctypes.windll.gdi32.GetDeviceCaps(hdc, 88) # 88 is the LOGPIXELSX value for horizontal DPI

# Release the device context (DC)
ctypes.windll.user32.ReleaseDC(0, hdc)

# 96 DPI is the default for 100% scaling, so scaling is DPI / 96
scaling_percentage = dpi_x / 96 * 100

parent_x = int(self.master.winfo_rootx() * 100 / scaling_percentage)
parent_y = int(self.master.winfo_rooty() * 100 / scaling_percentage)
parent_width = int(self.master.winfo_width() * 100 / scaling_percentage)
parent_height = int(self.master.winfo_height() * 100 / scaling_percentage)
x = parent_x + (parent_width // 4) - (width // 2)
y = parent_y + (parent_height // 4) - (height // 2)
window.geometry(f"{width}x{height}+{x}+{y}")
Expand Down

0 comments on commit 3e07110

Please sign in to comment.