Enhance HVideoTool's detection and restoration capabilities: introduced per-model overlay threshold settings and cross-model non-maximum suppression (NMS) to improve detection accuracy. Updated configuration management to support these features, and refined the UI for better user experience. Documentation in CLAUDE.md has been updated to reflect these changes.
This commit is contained in:
@@ -17,6 +17,7 @@ project folder.
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from .types import Detection
|
||||
@@ -24,16 +25,43 @@ from .types import Detection
|
||||
_VERSION = 1
|
||||
|
||||
|
||||
def make_key(models: list[str], yolo_conf: float, yolo_imgsz: int) -> dict:
|
||||
def _atomic_write_text(path: Path, text: str) -> None:
|
||||
"""Write ``text`` to ``path`` crash-safely: write a sibling .tmp, then os.replace.
|
||||
|
||||
``os.replace`` is atomic on the same filesystem (incl. NTFS), so a crash mid-write
|
||||
leaves the previous file intact instead of a truncated/corrupt one — important for
|
||||
a large detections.json that holds tens of thousands of entries.
|
||||
"""
|
||||
tmp = path.with_name(path.name + ".tmp")
|
||||
try:
|
||||
tmp.write_text(text, encoding="utf-8")
|
||||
os.replace(tmp, path)
|
||||
finally:
|
||||
if tmp.exists():
|
||||
try:
|
||||
tmp.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def make_key(
|
||||
models: list[str], yolo_conf: float, yolo_imgsz: int, nms_iou: float | None = None
|
||||
) -> dict:
|
||||
"""Identity of the detector set that produced a cache; cache is only valid for a match.
|
||||
|
||||
Keyed by the (sorted) model **basenames** so it's portable across machines/paths.
|
||||
``nms_iou`` is only added to the key when cross-model NMS is enabled — so the default
|
||||
(NMS off) key is unchanged and existing caches stay valid; turning NMS on yields a
|
||||
distinct key (its merged results differ) without invalidating the non-NMS cache.
|
||||
"""
|
||||
return {
|
||||
key = {
|
||||
"models": sorted(Path(m).name for m in models),
|
||||
"yolo_conf": round(float(yolo_conf), 4),
|
||||
"yolo_imgsz": int(yolo_imgsz),
|
||||
}
|
||||
if nms_iou is not None:
|
||||
key["nms_iou"] = round(float(nms_iou), 4)
|
||||
return key
|
||||
|
||||
|
||||
def save_results(cache_file: Path, key: dict, results: dict[str, list[Detection]]) -> bool:
|
||||
@@ -48,9 +76,7 @@ def save_results(cache_file: Path, key: dict, results: dict[str, list[Detection]
|
||||
}
|
||||
try:
|
||||
cache_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
cache_file.write_text(
|
||||
json.dumps(payload, ensure_ascii=False), encoding="utf-8"
|
||||
)
|
||||
_atomic_write_text(cache_file, json.dumps(payload, ensure_ascii=False))
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user