mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-11-30 00:33:41 +08:00
Confirm approaching STOP displays without relaxing braking evidence
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import Counter, deque
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
import hashlib
|
||||
@@ -801,7 +802,8 @@ class VisionModelCore:
|
||||
return speed_limit, confidence
|
||||
|
||||
|
||||
def _detect_sign_from_detector_classifier(self, frame_bgr, *, proposals=None):
|
||||
def _detect_sign_from_detector_classifier(
|
||||
self, frame_bgr, *, proposals: Iterable[tuple[float, int, tuple[int, int, int, int]]] | None = None):
|
||||
frame_height, frame_width = frame_bgr.shape[:2]
|
||||
best_detection = None
|
||||
best_score = 0.0
|
||||
|
||||
@@ -102,8 +102,22 @@ def _matches(a: StopBox, b: StopBox) -> bool:
|
||||
abs(by + bb - ay - ab) <= 3 * max(ah, bh))
|
||||
|
||||
|
||||
def _display_pair(old: StopBox, new: StopBox) -> bool:
|
||||
if old.confidence >= STOP_CONFIRM_CONFIDENCE and new.confidence >= STOP_CONFIRM_CONFIDENCE:
|
||||
return True
|
||||
old_width, old_height = old.bbox[2] - old.bbox[0], old.bbox[3] - old.bbox[1]
|
||||
new_width, new_height = new.bbox[2] - new.bbox[0], new.bbox[3] - new.bbox[1]
|
||||
return (new.confidence >= old.confidence and
|
||||
(old.confidence + new.confidence) / 2 >= STOP_CONFIRM_CONFIDENCE and
|
||||
new_width >= old_width and new_height >= old_height and
|
||||
new_width * new_height >= 2 * old_width * old_height)
|
||||
|
||||
|
||||
class StopConfirmation:
|
||||
def __init__(self):
|
||||
def __init__(self, *, display_only: bool = False):
|
||||
if type(display_only) is not bool:
|
||||
raise ValueError('Invalid STOP confirmation role')
|
||||
self._display_only = display_only
|
||||
self.reset()
|
||||
|
||||
def reset(self) -> None:
|
||||
@@ -157,10 +171,12 @@ class StopConfirmation:
|
||||
if previous is not None and (sample.observed_ns <= previous.observed_ns or sample.frame_id <= previous.frame_id):
|
||||
self.reset()
|
||||
return 0
|
||||
candidates = tuple(box for box in sample.boxes if box.confidence >= STOP_CONFIRM_CONFIDENCE)
|
||||
minimum = .6 if self._display_only else STOP_CONFIRM_CONFIDENCE
|
||||
candidates = tuple(box for box in sample.boxes if box.confidence >= minimum)
|
||||
matched = tuple(new for new in candidates if previous is not None and
|
||||
100_000_000 <= sample.observed_ns - previous.observed_ns < STOP_MAX_AGE_NS and
|
||||
any(_matches(old, new) for old in previous.boxes if old.confidence >= STOP_CONFIRM_CONFIDENCE))
|
||||
any(_matches(old, new) and _display_pair(old, new)
|
||||
for old in previous.boxes if old.confidence >= minimum))
|
||||
if not candidates:
|
||||
self.first_seen_ns = 0
|
||||
elif not matched:
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from openpilot.starpilot.speed_limits.vision.model import Detection, VisionModelCore
|
||||
from openpilot.starpilot.speed_limits.vision.speed_face import SpeedFaceVerifier
|
||||
from openpilot.starpilot.speed_limits.vision.stop_signs import StopDetection
|
||||
from openpilot.starpilot.speed_limits.vision.traffic_signs import TrafficSignModelCore, left_panel_region
|
||||
|
||||
|
||||
@@ -13,9 +14,9 @@ class TestMedianFallback(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.core = TrafficSignModelCore.__new__(TrafficSignModelCore)
|
||||
self.core.net = object()
|
||||
self.core.stop_detections = ('original STOP result',)
|
||||
self.core.stop_detections = (StopDetection(.8, (800, 200, 860, 260)),)
|
||||
self.frame = np.zeros((760, 1344, 3), np.uint8)
|
||||
self.face = SimpleNamespace(last_forward_count=1, last_forward_duration_s=.01)
|
||||
self.face = Mock(spec=SpeedFaceVerifier, last_forward_count=1, last_forward_duration_s=.01)
|
||||
def begin():
|
||||
self.face.last_forward_count = 0
|
||||
self.face.last_forward_duration_s = 0.
|
||||
@@ -55,7 +56,7 @@ class TestMedianFallback(unittest.TestCase):
|
||||
self.assertEqual(args[2:6], (377, 243, 1344, 760))
|
||||
self.assertEqual(self.face.last_forward_count, 2)
|
||||
self.assertAlmostEqual(self.face.last_forward_duration_s, .03)
|
||||
self.assertEqual(self.core.stop_detections, ('original STOP result',))
|
||||
self.assertEqual(self.core.stop_detections, (StopDetection(.8, (800, 200, 860, 260)),))
|
||||
|
||||
def test_failure_propagates_without_losing_cost(self):
|
||||
with patch.object(VisionModelCore, '_detect_sign_from_detector_classifier', return_value=None), \
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
from dataclasses import replace
|
||||
import unittest
|
||||
from typing import cast
|
||||
|
||||
from openpilot.starpilot.speed_limits.vision.stop_observation import StopBox, StopConfirmation, StopEvidence, STOP_MAX_AGE_NS
|
||||
|
||||
BASE = 100_000_000_000
|
||||
|
||||
|
||||
def sample(stamp, frame, confidence=.8, box=(780, 200, 840, 260)):
|
||||
return StopEvidence('camera', 'road', stamp, stamp + 2_000_000_000, frame, 1344, 760, (StopBox(confidence, box),))
|
||||
|
||||
|
||||
class StopDisplayConfirmationTests(unittest.TestCase):
|
||||
def test_compensated_pair_displays_with_control_confirmation_unchanged(self):
|
||||
for confidences in ((.67658, .74177), (.69997, .7953)):
|
||||
with self.subTest(confidences=confidences):
|
||||
display, control = StopConfirmation(display_only=True), StopConfirmation()
|
||||
first = sample(BASE, 10, confidences[0])
|
||||
second = sample(BASE + 1_100_000_000, 32, confidences[1], (900, 170, 1000, 270))
|
||||
self.assertEqual(display.observe(first, now_ns=first.observed_ns + 500_000_000), 0)
|
||||
self.assertEqual(control.observe(first, now_ns=first.observed_ns + 500_000_000), 0)
|
||||
self.assertEqual(display.observe(second, now_ns=second.observed_ns + 500_000_000), second.observed_ns)
|
||||
self.assertEqual(control.observe(second, now_ns=second.observed_ns + 500_000_000), 0)
|
||||
|
||||
def test_one_high_confidence_view_never_confirms(self):
|
||||
owner = StopConfirmation(display_only=True)
|
||||
first = sample(BASE, 10, .99)
|
||||
self.assertEqual(owner.observe(first, now_ns=BASE + 100_000_000), 0)
|
||||
self.assertEqual(owner.observe(first, now_ns=BASE + 500_000_000), 0)
|
||||
|
||||
def test_two_weak_views_cannot_borrow_confirmation(self):
|
||||
owner = StopConfirmation(display_only=True)
|
||||
for i in range(4):
|
||||
s = sample(BASE + i * 600_000_000, 10 + i * 12, .69)
|
||||
self.assertEqual(owner.observe(s, now_ns=s.observed_ns + 100_000_000), 0)
|
||||
|
||||
def test_expired_and_fresh_negative_clear_display(self):
|
||||
owner = StopConfirmation(display_only=True)
|
||||
first, second = sample(BASE, 10, .68), sample(BASE + 600_000_000, 22, .8, (900, 170, 1000, 270))
|
||||
owner.observe(first, now_ns=BASE + 100_000_000)
|
||||
self.assertEqual(owner.observe(second, now_ns=second.observed_ns + 100_000_000), second.observed_ns)
|
||||
self.assertEqual(owner.observe(second, now_ns=second.observed_ns + STOP_MAX_AGE_NS), 0)
|
||||
owner.observe(first, now_ns=BASE + 100_000_000)
|
||||
owner.observe(second, now_ns=second.observed_ns + 100_000_000)
|
||||
negative = replace(sample(BASE + 1_200_000_000, 34), boxes=())
|
||||
self.assertEqual(owner.observe(negative, now_ns=negative.observed_ns + 100_000_000), 0)
|
||||
|
||||
def test_matching_session_geometry_and_spacing_remain_required(self):
|
||||
first = sample(BASE, 10, .68)
|
||||
good = sample(BASE + 600_000_000, 22, .8, (900, 170, 1000, 270))
|
||||
for second in (replace(good, session='other'), replace(good, frame_id=9),
|
||||
replace(good, observed_ns=BASE + 50_000_000),
|
||||
replace(good, observed_ns=BASE + STOP_MAX_AGE_NS),
|
||||
replace(good, boxes=(StopBox(.8, (20, 20, 50, 50)),))):
|
||||
with self.subTest(second=second):
|
||||
owner = StopConfirmation(display_only=True)
|
||||
owner.observe(first, now_ns=BASE + 100_000_000)
|
||||
self.assertEqual(owner.observe(second, now_ns=second.observed_ns + 100_000_000), 0)
|
||||
|
||||
def test_weak_pair_needs_improving_confidence_and_approach(self):
|
||||
first = sample(BASE, 10, .68)
|
||||
for confidence, box in ((.8, (780, 200, 840, 260)),
|
||||
(.8, (780, 190, 850, 270)),
|
||||
(.67, (900, 170, 1000, 270))):
|
||||
owner = StopConfirmation(display_only=True)
|
||||
owner.observe(first, now_ns=BASE + 100_000_000)
|
||||
second = sample(BASE + 600_000_000, 22, confidence, box)
|
||||
self.assertEqual(owner.observe(second, now_ns=second.observed_ns + 100_000_000), 0)
|
||||
|
||||
def test_existing_strong_stationary_pair_still_confirms(self):
|
||||
for owner in (StopConfirmation(), StopConfirmation(display_only=True)):
|
||||
owner.observe(sample(BASE, 10), now_ns=BASE + 100_000_000)
|
||||
self.assertEqual(owner.observe(sample(BASE + 600_000_000, 22), now_ns=BASE + 700_000_000), BASE + 600_000_000)
|
||||
|
||||
def test_reset_preserves_explicit_display_role(self):
|
||||
owner = StopConfirmation(display_only=True)
|
||||
for _ in range(2):
|
||||
owner.reset()
|
||||
owner.observe(sample(BASE, 10, .68), now_ns=BASE + 100_000_000)
|
||||
self.assertEqual(owner.observe(sample(BASE + 600_000_000, 22, .8, (900, 170, 1000, 270)), now_ns=BASE + 700_000_000), BASE + 600_000_000)
|
||||
|
||||
def test_role_requires_boolean(self):
|
||||
for value in (1, None, 'true'):
|
||||
with self.assertRaises(ValueError):
|
||||
StopConfirmation(display_only=cast(bool, value))
|
||||
@@ -150,7 +150,9 @@ class TrafficSignModelCore(model.VisionModelCore):
|
||||
callback(tuple(self.stop_detections))
|
||||
return proposals
|
||||
|
||||
def _detect_sign_from_detector_classifier(self, frame_bgr):
|
||||
def _detect_sign_from_detector_classifier(self, frame_bgr, *, proposals=None):
|
||||
if proposals is not None:
|
||||
return super()._detect_sign_from_detector_classifier(frame_bgr, proposals=proposals)
|
||||
detection = super()._detect_sign_from_detector_classifier(frame_bgr)
|
||||
if detection is not None or self.net is None or model.DETECTOR_CLASSIFIER_REGION_MODE != 'right_roi':
|
||||
return detection
|
||||
|
||||
@@ -340,7 +340,7 @@ class RuntimeSnapshotAdapter:
|
||||
self._replay_stopped_history = False
|
||||
self._developer_metrics = DeveloperMetrics()
|
||||
self._reverse_driver_camera = ReverseDriverCamera()
|
||||
self._vision_stop = StopConfirmation()
|
||||
self._vision_stop = StopConfirmation(display_only=True)
|
||||
self._slc_sign_cache: tuple[int, tuple[int, int], str, SpeedLimitObservation] | None = None
|
||||
self._slc_last_sign: tuple[tuple[int, int], str, str, float] | None = None
|
||||
self._slc_sign_recovery_ns: int | None = None
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import unittest
|
||||
|
||||
from openpilot.starpilot.speed_limits.vision.stop_observation import StopBox, StopConfirmation
|
||||
from openpilot.starpilot.speed_limits.vision.tests.test_stop_observation import BASE, DRIVE, OFFSET, Input, evidence, message
|
||||
from openpilot.starpilot.ui.shell import ShellMode
|
||||
from openpilot.starpilot.ui.tests.test_runtime_snapshot import RuntimeSnapshotAdapter, ui_fake
|
||||
|
||||
|
||||
class TestStopDisplaySnapshot(unittest.TestCase):
|
||||
def test_actual_snapshot_accepts_approaching_pair_without_control_authority(self):
|
||||
for pair in (((.6765813, (874, 338, 914, 379)), (.7417721, (1018, 289, 1089, 358))),
|
||||
((.6999748, (794, 338, 830, 380)), (.7953088, (924, 302, 988, 367)))):
|
||||
with self.subTest(pair=pair):
|
||||
ui = ui_fake()
|
||||
ui.sm.messages['deviceState'].startedMonoTime = DRIVE
|
||||
clock = {'now': BASE}
|
||||
adapter = RuntimeSnapshotAdapter(ui, mono_clock=lambda clock=clock: clock['now'], boot_clock=lambda clock=clock: clock['now'] + OFFSET)
|
||||
control = StopConfirmation()
|
||||
for i, (confidence, box) in enumerate(pair):
|
||||
stamp = BASE + i * 1_100_000_000
|
||||
sample = evidence(stamp, frame=1 + i * 22, boxes=(StopBox(confidence, box),))
|
||||
now = stamp + 500_000_000
|
||||
event = message(sample, now=now)
|
||||
ui.sm.put('slcVisionObservation', event.slcVisionObservation, frame=3 + i)
|
||||
ui.sm.logMonoTime['slcVisionObservation'] = now
|
||||
ui.sm.recv_time['slcVisionObservation'] = now / 1e9
|
||||
clock['now'] = now
|
||||
result = adapter.build(ShellMode.ONROAD, now_ns=now)
|
||||
self.assertEqual(bool(result.onroad.stop_sign_observed_ns), i == 1)
|
||||
self.assertEqual(control.observe_message(Input(event), now_ns=now, now_boot_ns=now + OFFSET, drive_id=DRIVE), 0)
|
||||
ui.sm.alive['slcVisionObservation'] = False
|
||||
self.assertEqual(adapter.build(ShellMode.ONROAD, now_ns=clock['now']).onroad.stop_sign_observed_ns, 0)
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "9988b9f48f5eb10adaa44bf68da9d2009f3c6a659f0dc6d17a7f427e0ea03e47", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "87556c7d05150f64721dd455a59c52a7a879a616b7975b670c726e5ac8b2f682", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user