-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
61 lines (51 loc) · 1.53 KB
/
monitor.py
File metadata and controls
61 lines (51 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import logging
from dataclasses import dataclass
log = logging.getLogger(__name__)
@dataclass
class Detection:
region: str
x: int
y: int
confidence: float
class RegionMonitor:
"""Pixel-based screen region monitor with numpy vectorized ops."""
def __init__(self, cfg: dict):
self.regions = cfg.get("regions", [])
self.threshold = cfg.get("color_threshold", 15)
self.min_area = cfg.get("min_area", 10)
self._history: dict[str, list] = {}
def analyze(self, frame: np.ndarray) -> list[dict]:
results = []
for region in self.regions:
name = region.get("name", "default")
x, y = region.get("x", 0), region.get("y", 0)
w, h = region.get("w", 50), region.get("h", 50)
target = np.array(region.get("target_color", [255, 0, 0]), dtype=np.float32)
tol = region.get("tolerance", self.threshold)
# Bounds check
y1, y2 = max(0, y), min(frame.shape[0], y + h)
x1, x2 = max(0, x), min(frame.shape[1], x + w)
if y2 <= y1 or x2 <= x1:
continue
roi = frame[y1:y2, x1:x2].astype(np.float32)
dist = np.sqrt(np.sum((roi - target) ** 2, axis=-1))
mask = dist <= tol
if np.any(mask):
match_y, match_x = np.where(mask)
cx = int(np.mean(match_x)) + x1
cy = int(np.mean(match_y)) + y1
conf = float(np.mean(mask))
# Stability filter
hist = self._history.setdefault(name, [])
hist.append(True)
if len(hist) > 5:
hist.pop(0)
if sum(hist) >= 3:
results.append({"region": name, "x": cx, "y": cy, "confidence": conf})
else:
hist = self._history.setdefault(name, [])
hist.append(False)
if len(hist) > 5:
hist.pop(0)
return results