Skip to content

Commit 971f711

Browse files
Clamp Max Input Resolution
Prevents high resolution camera sources from displaying too large to fit in the app.
1 parent ddff2e5 commit 971f711

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

BabbleApp/camera.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cv2
2+
from cv2.typing import *
23
import numpy as np
34
import queue
45
import serial
@@ -12,6 +13,7 @@
1213
from enum import Enum
1314

1415
WAIT_TIME = 0.1
16+
MAX_RESOLUTION: int = 600
1517

1618
# Serial communication protocol:
1719
# header-begin (2 bytes)
@@ -24,9 +26,9 @@
2426

2527

2628
class CameraState(Enum):
27-
CONNECTING = 0
28-
CONNECTED = 1
29-
DISCONNECTED = 2
29+
CONNECTING: int = 0
30+
CONNECTED: int = 1
31+
DISCONNECTED: int = 2
3032

3133

3234
class Camera:
@@ -277,12 +279,25 @@ def start_serial_connection(self, port):
277279
print(f"{Fore.CYAN}[INFO] Failed to connect on {port}{Fore.RESET}")
278280
self.camera_status = CameraState.DISCONNECTED
279281

282+
def clamp_max_res(self, image: MatLike) -> MatLike:
283+
shape = image.shape
284+
max_value = np.max(shape)
285+
if max_value > MAX_RESOLUTION:
286+
scale: float = MAX_RESOLUTION/max_value
287+
width: int = int(shape[1] * scale)
288+
height: int = int(shape[0] * scale)
289+
image = cv2.resize(image, (width, height))
290+
291+
return image
292+
else: return image
293+
294+
280295
def push_image_to_queue(self, image, frame_number, fps):
281296
# If there's backpressure, just yell. We really shouldn't have this unless we start getting
282297
# some sort of capture event conflict though.
283298
qsize = self.camera_output_outgoing.qsize()
284299
if qsize > 1:
285300
print(
286301
f"{Fore.YELLOW}[WARN] CAPTURE QUEUE BACKPRESSURE OF {qsize}. CHECK FOR CRASH OR TIMING ISSUES IN ALGORITHM.{Fore.RESET}")
287-
self.camera_output_outgoing.put((image, frame_number, fps))
302+
self.camera_output_outgoing.put((self.clamp_max_res(image), frame_number, fps))
288303
self.capture_event.clear()

BabbleApp/camera_widget.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import cv2
88
from babble_processor import BabbleProcessor, CamInfoOrigin
9-
from camera import Camera, CameraState
9+
from camera import Camera, CameraState, MAX_RESOLUTION
1010
from config import BabbleConfig
1111
from osc import Tab
1212
from utils.misc_utils import PlaySound, SND_FILENAME, SND_ASYNC, list_camera_names, get_camera_index_by_name
@@ -85,9 +85,9 @@ def __init__(self, widget_id: Tab, main_config: BabbleConfig, osc_queue: Queue):
8585
],
8686
[
8787
sg.Graph(
88-
(640, 480),
89-
(0, 480),
90-
(640, 0),
88+
(MAX_RESOLUTION, MAX_RESOLUTION),
89+
(0, MAX_RESOLUTION),
90+
(MAX_RESOLUTION, 0),
9191
key=self.gui_roi_selection,
9292
drag_submits=True,
9393
enable_events=True,
@@ -337,11 +337,11 @@ def render(self, window, event, values):
337337

338338
if event == self.gui_autoroi:
339339
print("Set ROI")
340-
output = self.babble_cnn.get_framesize()
340+
output = self.maybe_image[0].shape
341341
self.config.roi_window_x = 0
342342
self.config.roi_window_y = 0
343-
self.config.roi_window_w = output[0]
344-
self.config.roi_window_h = output[1]
343+
self.config.roi_window_w = output[1]
344+
self.config.roi_window_h = output[0]
345345
self.main_config.save()
346346

347347
if (event == self.gui_refresh_button):

0 commit comments

Comments
 (0)