"""用 EvidenceV2 对采集的车型图片做结构特征分析(诊断,不参与训练)。 输出每张图的:立柱数 post_count、立柱间距 cv、地面比例 ground、边梁 deck、 箱底/车面带数、候选状态。用于判断侧面车型图的特征分布与算法口径差异。 用法: python scripts/analyze_web_cc.py [--out report.csv] """ from __future__ import annotations import argparse import csv import glob import os import sys import cv2 ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, ROOT) from portcv.config import load_config from portcv.io.capture import imread_any from portcv.logic.evidence_v2 import ( EvidenceV2, horizontal_bands, pick_box_and_bed, detect_structure_v2, ground_like_ratio, _to_gray, ) STATE_NAME = {0: "NOT_ON_TRAIN", 1: "BED", 2: "FLATBED", 255: "UNKNOWN"} def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--out", default=os.path.join(ROOT, "dataset", "web_cc", "analysis.csv")) args = ap.parse_args() cfg = load_config(os.path.join(ROOT, "configs", "edge-v2-front.json")) ev = EvidenceV2.from_config(cfg) p = ev.p files = glob.glob(os.path.join(ROOT, "dataset", "web_cc", "*", "*")) files = [f for f in files if f.lower().endswith((".jpg", ".jpeg", ".png"))] files.sort() rows = [] for f in files: image = imread_any(os.path.abspath(f).replace("\\", "/")) if image is None: continue wtype = os.path.basename(os.path.dirname(f)) # 业务类别:敞车/平车/其他 # 从新文件名 "C70E_001.jpg" 解析车型前缀 import re as _re m = _re.match(r"^([A-Z]+\d*[A-Z]*)_\d+", os.path.basename(f)) car_type = m.group(1) if m else wtype gray0 = _to_gray(image) h, w = gray0.shape[:2] scale = min(1.0, float(p.work_width) / float(w)) gray = cv2.resize(gray0, (int(round(w * scale)), int(round(h * scale))), interpolation=cv2.INTER_AREA) if scale < 1 else gray0.copy() wh, ww = gray.shape[:2] window = (int(p.window[0] * ww), int(p.window[1] * wh), int(p.window[2] * ww), int(p.window[3] * wh)) max_h = max(6, int(p.band_max_h_ratio * wh)) min_h = max(1, int(p.band_min_h * scale)) bright, dark = horizontal_bands(gray, window, min_h=min_h, max_h=max_h) box_hint = max(80.0, 0.40 * wh) box, bed = pick_box_and_bed( bright, dark, wh, box_bottom_max_ratio=p.box_bottom_max_ratio, bed_search_px=max(24.0, p.bed_search_ratio * box_hint), ) if box is None: rows.append(dict(type=car_type, group=wtype, file=os.path.basename(f), post_count=-1, spacing_cv="", ground="", deck="", state="NO_BOX")) continue # 车体 ROI:箱底以下(侧面车型图没有箱,这里仅诊断箱底误检位置) est_h = max(60.0, box.y0 - 0.05 * wh) start = max(2.0, p.below_start_ratio * est_h) y0w = min(wh - 2, int(box.y1 + start)) below_h = max(p.below_min_height_px * scale, min(p.below_height_ratio * est_h, 0.30 * wh)) y1w = min(wh - 1, max(y0w + int(below_h), int(box.y1 + 0.10 * est_h))) x0w, x1w = max(0, int(0.05 * ww)), min(ww - 1, int(0.95 * ww)) below = (x0w, y0w, x1w, max(y0w + 8, y1w)) structure = detect_structure_v2(gray, below, min_post_len_ratio=p.min_post_len_ratio, cluster_gap_px=9.0, scale=scale) ground, _ = ground_like_ratio(gray, below, structure["lines"], block_px=p.ground_block_px, density_lo=p.ground_density_lo, density_hi=p.ground_density_hi) posts = int(structure["post_count"]) opacity = max(0.0, min(1.0, 1.0 - ground)) # 复用配置阈值做候选判定(仅诊断) bed_strong = (p.min_door_posts <= posts <= getattr(p, "vehicle_max_posts", 16) and opacity >= p.min_wall_opacity) flat_strong = (posts <= p.flat_max_door_posts) state = 1 if bed_strong else (2 if flat_strong else 0) rows.append(dict( type=car_type, group=wtype, file=os.path.basename(f), post_count=posts, spacing_cv="" if structure["post_spacing_cv"] is None else round(structure["post_spacing_cv"], 2), ground=round(ground, 3), deck=len(structure["horizontal"]), state=STATE_NAME[state], )) with open(args.out, "w", newline="", encoding="utf-8-sig") as fh: w = csv.DictWriter(fh, fieldnames=["type", "group", "file", "post_count", "spacing_cv", "ground", "deck", "state"]) w.writeheader() w.writerows(rows) print("analyzed:", len(rows), "->", args.out) # 按车型汇总立柱数 by_type: dict = {} for r in rows: if r["post_count"] >= 0: by_type.setdefault(r["type"], []).append(int(r["post_count"])) for t in sorted(by_type): v = by_type[t] print("%-6s n=%2d posts min=%2d max=%2d avg=%.1f" % ( t, len(v), min(v), max(v), sum(v) / len(v))) return 0 if __name__ == "__main__": raise SystemExit(main())