Skip to content

Commit 6af87a7

Browse files
committed
remove my dumbness and rushing for window nightmares, vastly simplified
1 parent 87d8239 commit 6af87a7

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

docs/backends/PixelBlaze.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ marimapper_upload_mapping_to_pixelblaze --csv_file led_map_3d.csv
4040
Or with results CLI, plus `pb` CLI:
4141
```bash
4242
( echo '[' ; marimapper_results \
43-
| tail -n+2 | cut -d, -f 5-7 \
43+
| tail -n+2 | cut -d, -f 2-4 \
4444
| awk '{print (NR==1 ? "[" : "],[" ) $0 }' ; \
4545
echo ']]' ) | pb map
4646
```

marimapper/detector.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
import cv2
44
import time
55
from typing import Optional
6+
from dataclasses import dataclass
67
import numpy as np
78
from multiprocessing import get_logger
89

910
from marimapper.camera import Camera
1011
from marimapper.timeout_controller import TimeoutController
1112
from marimapper.led import Point2D, LED2D
12-
from marimapper.utils import position_window
13+
from marimapper.utils import window_config
1314

1415

1516
logger = get_logger()
1617

1718

19+
@dataclass
20+
class _Window:
21+
name: str = "MariMapper - Detector"
22+
camera_native_aspect_ratio: float = -1
23+
initial_height: int = -1
24+
25+
_win = _Window()
26+
27+
1828
def contour_brightness(image: np.ndarray, contour: np.ndarray) -> int:
1929
"""Calculate the sum of all pixels within a contour."""
2030
mask = np.zeros(image.shape, dtype=np.uint8)
@@ -89,25 +99,34 @@ def draw_led_detections(image: cv2.Mat, led_detection: Optional[Point2D]) -> np.
8999
return render_image
90100

91101

92-
def show_image(image: np.ndarray) -> None:
93-
window_name = "MariMapper - Detector"
102+
# If this is our first render and cv2.imshow(), make sure the window is created
103+
def _init_win_if_needed(image: np.ndarray) -> None:
104+
if _win.initial_height <= 0:
105+
cam_h, cam_w = image.shape[:2]
106+
cam_aspect_ratio = cam_w / cam_h
94107

95-
x, y, _, target_height = position_window(window_name, 320, 0, 960, 540)
108+
x, y, _, target_win_height = window_config(_win.name, 320, 0, 960, 540)
109+
target_win_width = int(target_win_height * cam_aspect_ratio)
110+
cv2.namedWindow(_win.name, cv2.WINDOW_NORMAL)
111+
cv2.resizeWindow(_win.name, target_win_width, target_win_height)
112+
cv2.moveWindow(_win.name, x, y)
113+
114+
_win.initial_height = target_win_height
115+
_win.camera_native_aspect_ratio = cam_aspect_ratio
96116

97-
native_h, native_w = image.shape[:2]
98-
aspect_ratio = native_w / native_h
99117

100-
target_width = int(target_height * aspect_ratio)
101118

102-
if not getattr(show_image, "setup_done", False):
103-
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
104-
cv2.resizeWindow(window_name, target_width, target_height)
105-
cv2.moveWindow(window_name, x, y)
106-
show_image.setup_done = True
119+
def show_image(image: np.ndarray) -> None:
120+
_init_win_if_needed(image)
107121

108-
display_image = cv2.resize(image, (target_width, target_height))
122+
# Resizing actually seems to perform better than not (more responsive too)
123+
# We only need to resize the display image (not window), and can use the initial height,
124+
# user movements are still respected
125+
image_height = _win.initial_height
126+
image_width = int(image_height * _win.camera_native_aspect_ratio)
127+
display_image = cv2.resize(image, (image_width, image_height))
109128

110-
cv2.imshow(window_name, display_image)
129+
cv2.imshow(_win.name, display_image)
111130
key = cv2.waitKey(1)
112131

113132
if key == 27: # esc

marimapper/utils.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,14 @@ def __exit__(self, *_):
5454
self.errnull_file.close()
5555

5656

57-
CONFIG_DIR = Path.home() / ".config" / "marimapper" / "windows"
58-
_CACHE = {}
59-
60-
61-
def position_window(name: str, x: int, y: int, w: int, h: int) -> list[int]:
62-
"""
63-
Returns [x, y, width, height].
64-
Checks memory cache first.
65-
If not in cache, checks disk.
66-
If not on disk, returns defaults immediately.
67-
"""
68-
if name in _CACHE:
69-
return _CACHE[name]
70-
71-
clean_name = re.sub(r"[^\w\-_\. ]", "_", name)
72-
file_path = CONFIG_DIR / f"{clean_name}.json"
73-
74-
defaults = {"x": x, "y": y, "width": w, "height": h}
75-
config = defaults.copy()
76-
77-
if file_path.exists():
78-
try:
79-
with open(file_path, "r") as f:
57+
# Look for stored window position and size settings, otherwise return supplied defaults
58+
def window_config(name: str, x: int, y: int, w: int, h: int) -> list[int]:
59+
try:
60+
with open(Path.home() / ".config" / "marimapper.json", "r") as f:
8061
data = json.load(f)
81-
config.update(data)
82-
except Exception:
83-
pass
84-
85-
# Return as list [x, y, w, h]
86-
result = [config["x"], config["y"], config["width"], config["height"]]
87-
_CACHE[name] = result
88-
return result
62+
window_cfg = data["window"] and data["window"][name]
63+
if window_cfg:
64+
return [window_cfg["x"], window_cfg["y"], window_cfg["width"], window_cfg["height"]]
65+
except Exception:
66+
pass
67+
return [x, y, w, h]

marimapper/visualize_process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from multiprocessing import get_logger, Process, Event
44
from marimapper.queues import Queue3D
55
from marimapper.led import LED3D, View, get_next, get_distance
6-
from marimapper.utils import position_window
6+
from marimapper.utils import window_config
77
import time
88

99
logger = get_logger()
@@ -70,7 +70,7 @@ def run(self):
7070
def initialise_visualiser__(self):
7171
logger.debug("Renderer3D process initialising visualiser")
7272
window_name = "MariMapper"
73-
x, y, w, h = position_window(window_name, 0, 0, 640, 640)
73+
x, y, w, h = window_config(window_name, 0, 0, 640, 640)
7474

7575
self._vis = (
7676
open3d.visualization.Visualizer()

0 commit comments

Comments
 (0)