From 726209eaeabdcf4969f4d917e3361c56bbc69eef Mon Sep 17 00:00:00 2001 From: Rames The Generic Date: Fri, 11 Oct 2024 22:03:29 -0400 Subject: [PATCH 1/5] Add Input type blocking to Calibration Tab --- BabbleApp/algo_settings_widget.py | 22 +++++++--------------- BabbleApp/calib_settings_widget.py | 15 ++++++++++----- BabbleApp/utils/misc_utils.py | 10 ++++++++-- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/BabbleApp/algo_settings_widget.py b/BabbleApp/algo_settings_widget.py index abd75c5..c1209e6 100644 --- a/BabbleApp/algo_settings_widget.py +++ b/BabbleApp/algo_settings_widget.py @@ -4,7 +4,7 @@ from config import BabbleSettingsConfig from osc import Tab from queue import Queue from threading import Event -import re +from utils import misc_utils class AlgoSettingsWidget: @@ -135,19 +135,11 @@ class AlgoSettingsWidget: return self.cancellation_event.set() - def is_valid_float_input(self, value): - # Allow empty string, negative sign, or a float number - return bool(re.match(r'^-?\d*\.?\d*$', value)) - - def is_valid_int_input(self, value): - # Allow empty string, negative sign, or an integer number - return bool(re.match(r'^-?\d*$', value)) - def render(self, window, event, values): # Input validation for the fields if event == self.gui_multiply: value = values[self.gui_multiply] - if not self.is_valid_float_input(value): + if not misc_utils.is_valid_float_input(value): # Invalid input, remove last character value = value[:-1] window[self.gui_multiply].update(value) @@ -155,35 +147,35 @@ class AlgoSettingsWidget: elif event == self.calib_deadzone: value = values[self.calib_deadzone] - if not self.is_valid_float_input(value): + if not misc_utils.is_valid_float_input(value): value = value[:-1] window[self.calib_deadzone].update(value) values[self.calib_deadzone] = value elif event == self.gui_inference_threads: value = values[self.gui_inference_threads] - if not self.is_valid_int_input(value): + if not misc_utils.is_valid_float_input(value): value = value[:-1] window[self.gui_inference_threads].update(value) values[self.gui_inference_threads] = value elif event == self.gui_gpu_index: value = values[self.gui_gpu_index] - if not self.is_valid_int_input(value): + if not misc_utils.is_valid_float_input(value): value = value[:-1] window[self.gui_gpu_index].update(value) values[self.gui_gpu_index] = value elif event == self.gui_min_cutoff: value = values[self.gui_min_cutoff] - if not self.is_valid_float_input(value): + if not misc_utils.is_valid_float_input(value): value = value[:-1] window[self.gui_min_cutoff].update(value) values[self.gui_min_cutoff] = value elif event == self.gui_speed_coefficient: value = values[self.gui_speed_coefficient] - if not self.is_valid_float_input(value): + if not misc_utils.is_valid_float_input(value): value = value[:-1] window[self.gui_speed_coefficient].update(value) values[self.gui_speed_coefficient] = value diff --git a/BabbleApp/calib_settings_widget.py b/BabbleApp/calib_settings_widget.py index 3c2df43..303a5da 100644 --- a/BabbleApp/calib_settings_widget.py +++ b/BabbleApp/calib_settings_widget.py @@ -6,6 +6,7 @@ from queue import Queue from threading import Event import numpy as np from calib_settings_values import set_shapes +from utils import misc_utils class CalibSettingsWidget: @@ -194,12 +195,16 @@ class CalibSettingsWidget: for count1, element1 in enumerate(self.shape): for count2, element2 in enumerate(element1): + value = values[element2] + if not misc_utils.is_valid_float_input(value): + value = value[:-1] + window[element2].update(value) + values[element2] = value + if values[element2] != '': - try: - if float(self.array[count1][count2]) != float(values[element2]): - self.array[count1][count2] = float(values[element2]) - changed = True - except: print("Not a float") + if float(self.array[count1][count2]) != float(value): + self.array[count1][count2] = float(value) + changed = True if event == self.gui_reset_min: for count1, element1 in enumerate(self.shape): diff --git a/BabbleApp/utils/misc_utils.py b/BabbleApp/utils/misc_utils.py index a69823b..79cdcff 100644 --- a/BabbleApp/utils/misc_utils.py +++ b/BabbleApp/utils/misc_utils.py @@ -6,6 +6,7 @@ import os import platform import cv2 import subprocess +import re from pygrabber.dshow_graph import FilterGraph is_nt = True if sys.platform.startswith('win') else False @@ -157,8 +158,13 @@ def get_camera_index_by_name(name): return None -#def get_serial_port(name): -# for i, device in enumerate(cam_list): +def is_valid_float_input(value): + # Allow empty string, negative sign, or a float number + return bool(re.match(r'^-?\d*\.?\d*$', value)) + +def is_valid_int_input(value): + # Allow empty string, negative sign, or an integer number + return bool(re.match(r'^-?\d*$', value)) From ddff2e57c09a1d1e3afacaaa05c51adc2280ca86 Mon Sep 17 00:00:00 2001 From: Rames The Generic Date: Sat, 12 Oct 2024 14:22:32 -0400 Subject: [PATCH 2/5] Correct accidently changed type checks. --- BabbleApp/algo_settings_widget.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BabbleApp/algo_settings_widget.py b/BabbleApp/algo_settings_widget.py index c1209e6..65e55ba 100644 --- a/BabbleApp/algo_settings_widget.py +++ b/BabbleApp/algo_settings_widget.py @@ -154,14 +154,14 @@ class AlgoSettingsWidget: elif event == self.gui_inference_threads: value = values[self.gui_inference_threads] - if not misc_utils.is_valid_float_input(value): + if not misc_utils.is_valid_int_input(value): value = value[:-1] window[self.gui_inference_threads].update(value) values[self.gui_inference_threads] = value elif event == self.gui_gpu_index: value = values[self.gui_gpu_index] - if not misc_utils.is_valid_float_input(value): + if not misc_utils.is_valid_int_input(value): value = value[:-1] window[self.gui_gpu_index].update(value) values[self.gui_gpu_index] = value From 971f711dcd53f5efa12863290eefcaa756ceabe7 Mon Sep 17 00:00:00 2001 From: Rames The Generic Date: Thu, 17 Oct 2024 21:00:04 -0400 Subject: [PATCH 3/5] Clamp Max Input Resolution Prevents high resolution camera sources from displaying too large to fit in the app. --- BabbleApp/camera.py | 23 +++++++++++++++++++---- BabbleApp/camera_widget.py | 14 +++++++------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/BabbleApp/camera.py b/BabbleApp/camera.py index 213a26c..0918b84 100644 --- a/BabbleApp/camera.py +++ b/BabbleApp/camera.py @@ -1,4 +1,5 @@ import cv2 +from cv2.typing import * import numpy as np import queue import serial @@ -12,6 +13,7 @@ from utils.misc_utils import get_camera_index_by_name, list_camera_names from enum import Enum WAIT_TIME = 0.1 +MAX_RESOLUTION: int = 600 # Serial communication protocol: # header-begin (2 bytes) @@ -24,9 +26,9 @@ ETVR_HEADER_LEN = 6 class CameraState(Enum): - CONNECTING = 0 - CONNECTED = 1 - DISCONNECTED = 2 + CONNECTING: int = 0 + CONNECTED: int = 1 + DISCONNECTED: int = 2 class Camera: @@ -277,6 +279,19 @@ class Camera: print(f"{Fore.CYAN}[INFO] Failed to connect on {port}{Fore.RESET}") self.camera_status = CameraState.DISCONNECTED + def clamp_max_res(self, image: MatLike) -> MatLike: + shape = image.shape + max_value = np.max(shape) + if max_value > MAX_RESOLUTION: + scale: float = MAX_RESOLUTION/max_value + width: int = int(shape[1] * scale) + height: int = int(shape[0] * scale) + image = cv2.resize(image, (width, height)) + + return image + else: return image + + def push_image_to_queue(self, image, frame_number, fps): # If there's backpressure, just yell. We really shouldn't have this unless we start getting # some sort of capture event conflict though. @@ -284,5 +299,5 @@ class Camera: if qsize > 1: print( f"{Fore.YELLOW}[WARN] CAPTURE QUEUE BACKPRESSURE OF {qsize}. CHECK FOR CRASH OR TIMING ISSUES IN ALGORITHM.{Fore.RESET}") - self.camera_output_outgoing.put((image, frame_number, fps)) + self.camera_output_outgoing.put((self.clamp_max_res(image), frame_number, fps)) self.capture_event.clear() diff --git a/BabbleApp/camera_widget.py b/BabbleApp/camera_widget.py index 116f88c..a49e125 100644 --- a/BabbleApp/camera_widget.py +++ b/BabbleApp/camera_widget.py @@ -6,7 +6,7 @@ import PySimpleGUI as sg import cv2 from babble_processor import BabbleProcessor, CamInfoOrigin -from camera import Camera, CameraState +from camera import Camera, CameraState, MAX_RESOLUTION from config import BabbleConfig from osc import Tab from utils.misc_utils import PlaySound, SND_FILENAME, SND_ASYNC, list_camera_names, get_camera_index_by_name @@ -85,9 +85,9 @@ class CameraWidget: ], [ sg.Graph( - (640, 480), - (0, 480), - (640, 0), + (MAX_RESOLUTION, MAX_RESOLUTION), + (0, MAX_RESOLUTION), + (MAX_RESOLUTION, 0), key=self.gui_roi_selection, drag_submits=True, enable_events=True, @@ -337,11 +337,11 @@ class CameraWidget: if event == self.gui_autoroi: print("Set ROI") - output = self.babble_cnn.get_framesize() + output = self.maybe_image[0].shape self.config.roi_window_x = 0 self.config.roi_window_y = 0 - self.config.roi_window_w = output[0] - self.config.roi_window_h = output[1] + self.config.roi_window_w = output[1] + self.config.roi_window_h = output[0] self.main_config.save() if (event == self.gui_refresh_button): From f2ffeeb41ee483108976f3e7681a846c68c99d72 Mon Sep 17 00:00:00 2001 From: Rames The Generic Date: Sat, 19 Oct 2024 21:12:33 -0400 Subject: [PATCH 4/5] Fix recursive requirements --- BabbleApp/config.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/BabbleApp/config.py b/BabbleApp/config.py index 05274ee..1275c39 100644 --- a/BabbleApp/config.py +++ b/BabbleApp/config.py @@ -64,31 +64,19 @@ class BabbleConfig(BaseModel): EnsurePath() if not os.path.exists(CONFIG_FILE_NAME): - print( - f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.noSettingsFile")}' - ) return BabbleConfig() try: with open(CONFIG_FILE_NAME, "r") as settings_file: return BabbleConfig(**json.load(settings_file)) except json.JSONDecodeError: - print( - f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.failedToLoadSettings")}' - ) load_config = None if os.path.exists(BACKUP_CONFIG_FILE_NAME): try: with open(BACKUP_CONFIG_FILE_NAME, "r") as settings_file: load_config = BabbleConfig(**json.load(settings_file)) - print( - f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.usingBackupSettings")}' - ) except json.JSONDecodeError: pass if load_config is None: - print( - f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.usingBaseSettings")}' - ) load_config = BabbleConfig() return load_config @@ -108,6 +96,3 @@ class BabbleConfig(BaseModel): pass with open(CONFIG_FILE_NAME, "w") as settings_file: json.dump(obj=self.dict(), fp=settings_file, indent=2) - print( - f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.saved")}.' - ) From 62635ea5abcc1af43eecd49cf139dd476847d0fd Mon Sep 17 00:00:00 2001 From: Rames The Generic Date: Sat, 19 Oct 2024 21:16:47 -0400 Subject: [PATCH 5/5] remove duplicate function --- BabbleApp/algo_settings_widget.py | 1 - BabbleApp/utils/misc_utils.py | 11 ----------- 2 files changed, 12 deletions(-) diff --git a/BabbleApp/algo_settings_widget.py b/BabbleApp/algo_settings_widget.py index 9a143ed..79bb344 100644 --- a/BabbleApp/algo_settings_widget.py +++ b/BabbleApp/algo_settings_widget.py @@ -8,7 +8,6 @@ from utils.misc_utils import bg_color_highlight, bg_color_clear, is_valid_float_ from lang_manager import LocaleStringManager as lang - class AlgoSettingsWidget: def __init__( self, widget_id: Tab, main_config: BabbleSettingsConfig, osc_queue: Queue diff --git a/BabbleApp/utils/misc_utils.py b/BabbleApp/utils/misc_utils.py index ecb0188..fa8fc03 100644 --- a/BabbleApp/utils/misc_utils.py +++ b/BabbleApp/utils/misc_utils.py @@ -166,17 +166,6 @@ def get_camera_index_by_name(name): return None -def is_valid_float_input(value): - # Allow empty string, negative sign, or a float number - return bool(re.match(r'^-?\d*\.?\d*$', value)) - -def is_valid_int_input(value): - # Allow empty string, negative sign, or an integer number - return bool(re.match(r'^-?\d*$', value)) - -# def get_serial_port(name): -# for i, device in enumerate(cam_list): - # Placeholder for sound functions on Windows def PlaySound(*args, **kwargs):