Skip to content

Commit edf582e

Browse files
authored
Merge pull request #50 from RamesTheGeneric/feat-50
Add clamp max input resolution
2 parents 6bc0ea3 + 62635ea commit edf582e

File tree

4 files changed

+26
-30
lines changed

4 files changed

+26
-30
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
@@ -13,6 +14,7 @@
1314
from enum import Enum
1415

1516
WAIT_TIME = 0.1
17+
MAX_RESOLUTION: int = 600
1618

1719
# Serial communication protocol:
1820
# header-begin (2 bytes)
@@ -25,9 +27,9 @@
2527

2628

2729
class CameraState(Enum):
28-
CONNECTING = 0
29-
CONNECTED = 1
30-
DISCONNECTED = 2
30+
CONNECTING: int = 0
31+
CONNECTED: int = 1
32+
DISCONNECTED: int = 2
3133

3234

3335
class Camera:
@@ -304,6 +306,19 @@ def start_serial_connection(self, port):
304306
)
305307
self.camera_status = CameraState.DISCONNECTED
306308

309+
def clamp_max_res(self, image: MatLike) -> MatLike:
310+
shape = image.shape
311+
max_value = np.max(shape)
312+
if max_value > MAX_RESOLUTION:
313+
scale: float = MAX_RESOLUTION/max_value
314+
width: int = int(shape[1] * scale)
315+
height: int = int(shape[0] * scale)
316+
image = cv2.resize(image, (width, height))
317+
318+
return image
319+
else: return image
320+
321+
307322
def push_image_to_queue(self, image, frame_number, fps):
308323
# If there's backpressure, just yell. We really shouldn't have this unless we start getting
309324
# some sort of capture event conflict though.
@@ -312,5 +327,5 @@ def push_image_to_queue(self, image, frame_number, fps):
312327
print(
313328
f'{Fore.YELLOW}[{lang._instance.get_string("log.warn")}] {lang._instance.get_string("warn.backpressure1")} {qsize}. {lang._instance.get_string("warn.backpressure2")}{Fore.RESET}'
314329
)
315-
self.camera_output_outgoing.put((image, frame_number, fps))
330+
self.camera_output_outgoing.put((self.clamp_max_res(image), frame_number, fps))
316331
self.capture_event.clear()

BabbleApp/camera_widget.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import PySimpleGUI as sg
55
import cv2
66
from babble_processor import BabbleProcessor, CamInfoOrigin
7-
from camera import Camera, CameraState
7+
from camera import Camera, CameraState, MAX_RESOLUTION
88
from config import BabbleConfig
99
from osc import Tab
1010
from utils.misc_utils import (
@@ -101,9 +101,9 @@ def __init__(self, widget_id: Tab, main_config: BabbleConfig, osc_queue: Queue):
101101
],
102102
[
103103
sg.Graph(
104-
(640, 480),
105-
(0, 480),
106-
(640, 0),
104+
(MAX_RESOLUTION, MAX_RESOLUTION),
105+
(0, MAX_RESOLUTION),
106+
(MAX_RESOLUTION, 0),
107107
key=self.gui_roi_selection,
108108
drag_submits=True,
109109
enable_events=True,
@@ -424,11 +424,11 @@ def render(self, window, event, values):
424424

425425
if event == self.gui_autoroi:
426426
print(lang._instance.get_string("info.setROI"))
427-
output = self.babble_cnn.get_framesize()
427+
output = self.maybe_image[0].shape
428428
self.config.roi_window_x = 0
429429
self.config.roi_window_y = 0
430-
self.config.roi_window_w = output[0]
431-
self.config.roi_window_h = output[1]
430+
self.config.roi_window_w = output[1]
431+
self.config.roi_window_h = output[0]
432432
self.main_config.save()
433433

434434
if event == self.gui_refresh_button:

BabbleApp/config.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,19 @@ def load():
6464
EnsurePath()
6565

6666
if not os.path.exists(CONFIG_FILE_NAME):
67-
print(
68-
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.noSettingsFile")}'
69-
)
7067
return BabbleConfig()
7168
try:
7269
with open(CONFIG_FILE_NAME, "r") as settings_file:
7370
return BabbleConfig(**json.load(settings_file))
7471
except json.JSONDecodeError:
75-
print(
76-
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.failedToLoadSettings")}'
77-
)
7872
load_config = None
7973
if os.path.exists(BACKUP_CONFIG_FILE_NAME):
8074
try:
8175
with open(BACKUP_CONFIG_FILE_NAME, "r") as settings_file:
8276
load_config = BabbleConfig(**json.load(settings_file))
83-
print(
84-
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.usingBackupSettings")}'
85-
)
8677
except json.JSONDecodeError:
8778
pass
8879
if load_config is None:
89-
print(
90-
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.usingBaseSettings")}'
91-
)
9280
load_config = BabbleConfig()
9381
return load_config
9482

@@ -108,6 +96,3 @@ def save(self):
10896
pass
10997
with open(CONFIG_FILE_NAME, "w") as settings_file:
11098
json.dump(obj=self.dict(), fp=settings_file, indent=2)
111-
print(
112-
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("config.saved")}.'
113-
)

BabbleApp/utils/misc_utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ def get_camera_index_by_name(name):
167167
return None
168168

169169

170-
# def get_serial_port(name):
171-
# for i, device in enumerate(cam_list):
172-
173-
174170
# Placeholder for sound functions on Windows
175171
def PlaySound(*args, **kwargs):
176172
pass

0 commit comments

Comments
 (0)