feat: 修复 AI 风险语义并隔离 Mock 数据
This commit is contained in:
@@ -5,6 +5,52 @@ from abc import ABC, abstractmethod
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def _label_set(labels) -> set[str]:
|
||||
return {label.strip().lower() for label in labels if label.strip()}
|
||||
|
||||
|
||||
def abnormal_probability(detections: list[dict], labels: tuple[str, ...] | list[str]) -> float:
|
||||
"""单帧异常概率:取异常类检测的最高置信度,避免多框求和造成虚高。"""
|
||||
abnormal = _label_set(labels) - {"healthy"}
|
||||
best = 0.0
|
||||
for detection in detections:
|
||||
class_name = str(detection.get("class_name", "")).strip().lower()
|
||||
if class_name in ("", "healthy", "unknown"):
|
||||
continue
|
||||
if abnormal and class_name not in abnormal:
|
||||
continue
|
||||
try:
|
||||
confidence = float(detection.get("confidence", 0))
|
||||
except (TypeError, ValueError):
|
||||
confidence = 0.0
|
||||
best = max(best, min(1.0, max(0.0, confidence)))
|
||||
return best
|
||||
|
||||
|
||||
def detection_status(detections: list[dict], labels: tuple[str, ...] | list[str]) -> str:
|
||||
"""空检测或 unknown 类不再被当作 healthy。"""
|
||||
if not detections:
|
||||
return "unknown"
|
||||
abnormal = _label_set(labels) - {"healthy"}
|
||||
healthy_seen = False
|
||||
unknown_seen = False
|
||||
for detection in detections:
|
||||
class_name = str(detection.get("class_name", "")).strip().lower()
|
||||
if abnormal and class_name in abnormal:
|
||||
return "abnormal"
|
||||
if not abnormal and class_name not in ("", "healthy", "unknown"):
|
||||
return "abnormal"
|
||||
if class_name == "healthy":
|
||||
healthy_seen = True
|
||||
elif class_name in ("", "unknown"):
|
||||
unknown_seen = True
|
||||
else:
|
||||
unknown_seen = True
|
||||
if healthy_seen and not unknown_seen:
|
||||
return "healthy"
|
||||
return "unknown"
|
||||
|
||||
|
||||
class Detector(ABC):
|
||||
@abstractmethod
|
||||
def detect(self, image_bytes: bytes) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user