analyze_web_cc.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """用 EvidenceV2 对采集的车型图片做结构特征分析(诊断,不参与训练)。
  2. 输出每张图的:立柱数 post_count、立柱间距 cv、地面比例 ground、边梁 deck、
  3. 箱底/车面带数、候选状态。用于判断侧面车型图的特征分布与算法口径差异。
  4. 用法:
  5. python scripts/analyze_web_cc.py [--out report.csv]
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. import csv
  10. import glob
  11. import os
  12. import sys
  13. import cv2
  14. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  15. sys.path.insert(0, ROOT)
  16. from portcv.config import load_config
  17. from portcv.io.capture import imread_any
  18. from portcv.logic.evidence_v2 import (
  19. EvidenceV2, horizontal_bands, pick_box_and_bed, detect_structure_v2,
  20. ground_like_ratio, _to_gray,
  21. )
  22. STATE_NAME = {0: "NOT_ON_TRAIN", 1: "BED", 2: "FLATBED", 255: "UNKNOWN"}
  23. def main() -> int:
  24. ap = argparse.ArgumentParser()
  25. ap.add_argument("--out", default=os.path.join(ROOT, "dataset", "web_cc", "analysis.csv"))
  26. args = ap.parse_args()
  27. cfg = load_config(os.path.join(ROOT, "configs", "edge-v2-front.json"))
  28. ev = EvidenceV2.from_config(cfg)
  29. p = ev.p
  30. files = glob.glob(os.path.join(ROOT, "dataset", "web_cc", "*", "*"))
  31. files = [f for f in files if f.lower().endswith((".jpg", ".jpeg", ".png"))]
  32. files.sort()
  33. rows = []
  34. for f in files:
  35. image = imread_any(os.path.abspath(f).replace("\\", "/"))
  36. if image is None:
  37. continue
  38. wtype = os.path.basename(os.path.dirname(f)) # 业务类别:敞车/平车/其他
  39. # 从新文件名 "C70E_001.jpg" 解析车型前缀
  40. import re as _re
  41. m = _re.match(r"^([A-Z]+\d*[A-Z]*)_\d+", os.path.basename(f))
  42. car_type = m.group(1) if m else wtype
  43. gray0 = _to_gray(image)
  44. h, w = gray0.shape[:2]
  45. scale = min(1.0, float(p.work_width) / float(w))
  46. gray = cv2.resize(gray0, (int(round(w * scale)), int(round(h * scale))),
  47. interpolation=cv2.INTER_AREA) if scale < 1 else gray0.copy()
  48. wh, ww = gray.shape[:2]
  49. window = (int(p.window[0] * ww), int(p.window[1] * wh),
  50. int(p.window[2] * ww), int(p.window[3] * wh))
  51. max_h = max(6, int(p.band_max_h_ratio * wh))
  52. min_h = max(1, int(p.band_min_h * scale))
  53. bright, dark = horizontal_bands(gray, window, min_h=min_h, max_h=max_h)
  54. box_hint = max(80.0, 0.40 * wh)
  55. box, bed = pick_box_and_bed(
  56. bright, dark, wh,
  57. box_bottom_max_ratio=p.box_bottom_max_ratio,
  58. bed_search_px=max(24.0, p.bed_search_ratio * box_hint),
  59. )
  60. if box is None:
  61. rows.append(dict(type=car_type, group=wtype, file=os.path.basename(f), post_count=-1,
  62. spacing_cv="", ground="", deck="", state="NO_BOX"))
  63. continue
  64. # 车体 ROI:箱底以下(侧面车型图没有箱,这里仅诊断箱底误检位置)
  65. est_h = max(60.0, box.y0 - 0.05 * wh)
  66. start = max(2.0, p.below_start_ratio * est_h)
  67. y0w = min(wh - 2, int(box.y1 + start))
  68. below_h = max(p.below_min_height_px * scale,
  69. min(p.below_height_ratio * est_h, 0.30 * wh))
  70. y1w = min(wh - 1, max(y0w + int(below_h), int(box.y1 + 0.10 * est_h)))
  71. x0w, x1w = max(0, int(0.05 * ww)), min(ww - 1, int(0.95 * ww))
  72. below = (x0w, y0w, x1w, max(y0w + 8, y1w))
  73. structure = detect_structure_v2(gray, below,
  74. min_post_len_ratio=p.min_post_len_ratio,
  75. cluster_gap_px=9.0, scale=scale)
  76. ground, _ = ground_like_ratio(gray, below, structure["lines"],
  77. block_px=p.ground_block_px,
  78. density_lo=p.ground_density_lo,
  79. density_hi=p.ground_density_hi)
  80. posts = int(structure["post_count"])
  81. opacity = max(0.0, min(1.0, 1.0 - ground))
  82. # 复用配置阈值做候选判定(仅诊断)
  83. bed_strong = (p.min_door_posts <= posts <= getattr(p, "vehicle_max_posts", 16)
  84. and opacity >= p.min_wall_opacity)
  85. flat_strong = (posts <= p.flat_max_door_posts)
  86. state = 1 if bed_strong else (2 if flat_strong else 0)
  87. rows.append(dict(
  88. type=car_type, group=wtype, file=os.path.basename(f),
  89. post_count=posts,
  90. spacing_cv="" if structure["post_spacing_cv"] is None else round(structure["post_spacing_cv"], 2),
  91. ground=round(ground, 3),
  92. deck=len(structure["horizontal"]),
  93. state=STATE_NAME[state],
  94. ))
  95. with open(args.out, "w", newline="", encoding="utf-8-sig") as fh:
  96. w = csv.DictWriter(fh, fieldnames=["type", "group", "file", "post_count", "spacing_cv", "ground", "deck", "state"])
  97. w.writeheader()
  98. w.writerows(rows)
  99. print("analyzed:", len(rows), "->", args.out)
  100. # 按车型汇总立柱数
  101. by_type: dict = {}
  102. for r in rows:
  103. if r["post_count"] >= 0:
  104. by_type.setdefault(r["type"], []).append(int(r["post_count"]))
  105. for t in sorted(by_type):
  106. v = by_type[t]
  107. print("%-6s n=%2d posts min=%2d max=%2d avg=%.1f" % (
  108. t, len(v), min(v), max(v), sum(v) / len(v)))
  109. return 0
  110. if __name__ == "__main__":
  111. raise SystemExit(main())