diff --git a/BabbleApp/camera.py b/BabbleApp/camera.py index 9f75f2b..b312817 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 @@ -13,6 +14,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) @@ -25,9 +27,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: @@ -304,6 +306,19 @@ class Camera: ) 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. @@ -312,5 +327,5 @@ class Camera: print( f'{Fore.YELLOW}[{lang._instance.get_string("log.warn")}] {lang._instance.get_string("warn.backpressure1")} {qsize}. {lang._instance.get_string("warn.backpressure2")}{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 b1326e1..d96bf08 100644 --- a/BabbleApp/camera_widget.py +++ b/BabbleApp/camera_widget.py @@ -4,7 +4,7 @@ from threading import Event, Thread 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 ( @@ -101,9 +101,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, @@ -424,11 +424,11 @@ class CameraWidget: if event == self.gui_autoroi: print(lang._instance.get_string("info.setROI")) - 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: 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")}.' - ) diff --git a/BabbleApp/utils/misc_utils.py b/BabbleApp/utils/misc_utils.py index 5690d50..fa8fc03 100644 --- a/BabbleApp/utils/misc_utils.py +++ b/BabbleApp/utils/misc_utils.py @@ -167,10 +167,6 @@ def get_camera_index_by_name(name): return None -# def get_serial_port(name): -# for i, device in enumerate(cam_list): - - # Placeholder for sound functions on Windows def PlaySound(*args, **kwargs): pass