"""与离线 demo main.py 同结构的段级文本报告(可选落盘)。""" from __future__ import annotations from pathlib import Path from app.services.tear_gated_segment_consumption.runner import TearGatedSegmentRecord def write_tear_segment_txt( *, path: Path, surgery_id: str, camera_id: str, labels_source: str, records: list[TearGatedSegmentRecord], ) -> None: path.parent.mkdir(parents=True, exist_ok=True) lines: list[str] = [] lines.append("耗材消耗推断(撕段模式 / FastAPI)") lines.append(f"手术: {surgery_id} camera: {camera_id}") lines.append(f"类名→label_id YAML: {labels_source}") lines.append( "说明: 人手检测 -> 撕二分类 -> 好帧门控(坏帧不判耗材) -> 41 类; " "段内为概率向量平均; 众数作展示对照" ) lines.append("") for rec in records: line = "\t".join( [ f"段{rec.segment_index}", f"时间戳(秒)={rec.mid_stream_sec:.3f};范围={rec.start_sec:.2f}~{rec.end_sec:.2f}", f"物品id={rec.item_id}", f"物品名称(模型top1)={rec.item_name}", f"top1置信度(段内平均)={rec.top1_conf:.4f}", f"top2={rec.top2_name}", f"top2_conf={rec.top2_conf:.4f}", f"top3={rec.top3_name}", f"top3_conf={rec.top3_conf:.4f}", f"段内众数(参考)={rec.majority_ref}", f"消耗数量=1", f"医生id=暂无", ] ) lines.append(line) lines.append("") path.write_text("\n".join(lines) + "\n", encoding="utf-8")