refactor: 统一耗材视觉算法并扩展语音确认至全量候选清单
- 以 ConsumableVisionAlgorithmService 替代 consumable_classifier 与 tear_action; 可选手部检测权重,未配置时全帧分类;时间窗众数与 Excel 白名单配置。 - 语音待确认:ASR 先匹配 pending topk,再匹配本台 candidate_consumables; 记账 item_id 与 vision 一致使用 name_to_code。 - 更新 config、Compose、.env.example、依赖(pandas/openpyxl)与测试。 Made-with: Cursor
This commit is contained in:
@@ -7,7 +7,10 @@ from unittest.mock import MagicMock
|
||||
import pytest
|
||||
|
||||
from app.config import Settings
|
||||
from app.services.consumable_classifier import PredictionCandidate, PredictionResult
|
||||
from app.services.consumable_vision_algorithm import (
|
||||
PredictionCandidate,
|
||||
PredictionResult,
|
||||
)
|
||||
from app.surgery_errors import SurgeryPipelineError
|
||||
from app.services.video.session_manager import (
|
||||
CameraSessionManager,
|
||||
@@ -21,8 +24,7 @@ def test_live_consumption_requires_non_empty_details() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -33,13 +35,50 @@ def test_live_consumption_requires_non_empty_details() -> None:
|
||||
assert mgr.live_consumption_if_active("123456") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_voice_accepts_label_on_surgery_list_not_in_topk_options() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
st = SurgerySessionState(
|
||||
candidate_consumables=["纱布", "止血钳"],
|
||||
name_to_code={"纱布": "P1", "止血钳": "P2"},
|
||||
)
|
||||
pid = "test-confirm-id"
|
||||
st.pending_by_id[pid] = PendingConsumableConfirmation(
|
||||
id=pid,
|
||||
status="pending",
|
||||
options=[("纱布", 0.4)],
|
||||
prompt_text="请确认",
|
||||
created_at=datetime.now(timezone.utc),
|
||||
model_top1_label="unknown",
|
||||
model_top1_confidence=0.41,
|
||||
)
|
||||
st.pending_fifo.append(pid)
|
||||
mgr._active["123456"] = RunningSurgery(
|
||||
stop_event=asyncio.Event(), state=st, tasks=[]
|
||||
)
|
||||
|
||||
await mgr.resolve_pending_confirmation(
|
||||
"123456", pid, chosen_label="止血钳", rejected=False
|
||||
)
|
||||
|
||||
assert len(st.details) == 1
|
||||
assert st.details[0].item_id == "P2"
|
||||
assert st.details[0].item_name == "止血钳"
|
||||
assert st.details[0].source == "voice"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_pending_appends_voice_detail() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -74,8 +113,7 @@ async def test_resolve_reject_closes_without_detail() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -108,8 +146,7 @@ async def test_handle_skips_when_candidate_list_empty() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -120,7 +157,7 @@ async def test_handle_skips_when_candidate_list_empty() -> None:
|
||||
topk=[PredictionCandidate(label="纱布", confidence=0.99)],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert state.details == []
|
||||
assert state.pending_fifo == []
|
||||
@@ -131,8 +168,7 @@ async def test_archive_retry_loop_starts() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -153,8 +189,7 @@ async def test_handle_skips_below_voice_floor() -> None:
|
||||
settings.video_voice_confirm_min_confidence = 0.5
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -165,7 +200,7 @@ async def test_handle_skips_below_voice_floor() -> None:
|
||||
topk=[PredictionCandidate(label="纱布", confidence=0.4)],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert state.details == []
|
||||
assert state.pending_fifo == []
|
||||
@@ -176,8 +211,7 @@ async def test_handle_auto_vision_confirm() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -188,7 +222,7 @@ async def test_handle_auto_vision_confirm() -> None:
|
||||
topk=[PredictionCandidate(label="纱布", confidence=0.99)],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert len(state.details) == 1
|
||||
assert state.details[0].source == "vision"
|
||||
@@ -200,8 +234,7 @@ async def test_handle_high_conf_top1_not_in_candidates_enqueues_pending() -> Non
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -215,7 +248,7 @@ async def test_handle_high_conf_top1_not_in_candidates_enqueues_pending() -> Non
|
||||
],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert state.details == []
|
||||
assert len(state.pending_fifo) == 1
|
||||
@@ -230,8 +263,7 @@ async def test_handle_mid_confidence_enqueues_pending() -> None:
|
||||
settings.video_voice_confirm_min_confidence = 0.3
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -245,7 +277,7 @@ async def test_handle_mid_confidence_enqueues_pending() -> None:
|
||||
],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert len(state.pending_fifo) == 1
|
||||
|
||||
@@ -257,8 +289,7 @@ async def test_handle_voice_disabled_no_pending_for_mid_conf() -> None:
|
||||
settings.video_auto_confirm_confidence = 0.8
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -269,7 +300,7 @@ async def test_handle_voice_disabled_no_pending_for_mid_conf() -> None:
|
||||
topk=[PredictionCandidate(label="纱布", confidence=0.5)],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert state.pending_fifo == []
|
||||
assert state.details == []
|
||||
@@ -281,8 +312,7 @@ async def test_handle_vision_cooldown_skips_duplicate() -> None:
|
||||
settings.video_detail_cooldown_sec = 3600.0
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -293,10 +323,10 @@ async def test_handle_vision_cooldown_skips_duplicate() -> None:
|
||||
topk=[PredictionCandidate(label="纱布", confidence=0.99)],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert len(state.details) == 1
|
||||
|
||||
@@ -307,8 +337,7 @@ async def test_handle_pending_dedupe_cooldown() -> None:
|
||||
settings.video_detail_cooldown_sec = 3600.0
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -322,10 +351,10 @@ async def test_handle_pending_dedupe_cooldown() -> None:
|
||||
],
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
await mgr._handle_classification_result(
|
||||
state=state, cls_res=res, tear_label=""
|
||||
state=state, cls_res=res
|
||||
)
|
||||
assert len(state.pending_fifo) == 1
|
||||
|
||||
@@ -335,8 +364,7 @@ async def test_resolve_invalid_chosen_label() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -367,8 +395,7 @@ async def test_resolve_not_active() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -384,8 +411,7 @@ async def test_resolve_second_time_not_found() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
@@ -419,8 +445,7 @@ async def test_resolve_already_resolved_status() -> None:
|
||||
settings = Settings()
|
||||
mgr = CameraSessionManager(
|
||||
settings=settings,
|
||||
consumable_classifier=MagicMock(),
|
||||
tear_action=MagicMock(),
|
||||
vision_algorithm=MagicMock(),
|
||||
hikvision_runtime=None,
|
||||
result_repository=None,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user