|
|
@@ -148,6 +148,17 @@ class EvidenceV2Params:
|
|
|
empty_min_post_len_ratio: float = 0.30
|
|
|
# 空车判据的斜撑门(与 `min_diagonal_braces` 分开,便于现场按车型单独放宽)。
|
|
|
empty_min_diagonal_braces: int = 1
|
|
|
+ # ---- P0-Q10:空平车(平车、车面裸露)→ 也输出 state=3 ----
|
|
|
+ # 平车没有侧墙,空载时直接看到**车底板**:薄车面边梁 + 车底透空 + 边梁上方是
|
|
|
+ # 一片光滑亮面(车面),且**没有箱底暗带紧贴边梁**(载车的箱底阴影必然贴着边梁,
|
|
|
+ # 实测 01/img00/img01 三张载车样本 gap=0)。
|
|
|
+ empty_flatbed_enable: bool = True
|
|
|
+ # 车面纵深下限(原图像素):近侧边梁 → 车面远端边缘线。载车 gap≈0 → 直接排除。
|
|
|
+ empty_flatbed_min_gap_px: float = 16.0
|
|
|
+ # 车面 ROI 的边缘覆盖率上限(竖向/横向):车面几乎无纹理,集装箱侧壁有瓦楞/立柱。
|
|
|
+ empty_flatbed_max_plane_edge: float = 0.12
|
|
|
+ # 车面灰度相对边梁的最大下降(灰阶):车面是亮面,车底/阴影/地面会明显更暗。
|
|
|
+ empty_flatbed_max_gray_drop: float = 80.0
|
|
|
# 车体顶相对搜索窗顶的最小余量(×画幅高)。0 = 不设门槛:车把画面填满
|
|
|
# (`02_C70E` 实测 texture_top≈2% 画幅)时也用整个窗口做侧墙 ROI,靠
|
|
|
# `empty_*` 那组门来判断——否则“空车占满画面”这一类永远进不来。
|
|
|
@@ -299,6 +310,12 @@ class EvidenceV2Params:
|
|
|
empty_min_wall_h_ratio=float(raw.get("empty_min_wall_h_ratio", 0.25)),
|
|
|
empty_min_post_len_ratio=float(raw.get("empty_min_post_len_ratio", 0.30)),
|
|
|
empty_min_diagonal_braces=int(raw.get("empty_min_diagonal_braces", 1)),
|
|
|
+ empty_flatbed_enable=bool(raw.get("empty_flatbed_enable", True)),
|
|
|
+ empty_flatbed_min_gap_px=float(raw.get("empty_flatbed_min_gap_px", 16.0)),
|
|
|
+ empty_flatbed_max_plane_edge=float(
|
|
|
+ raw.get("empty_flatbed_max_plane_edge", 0.12)),
|
|
|
+ empty_flatbed_max_gray_drop=float(
|
|
|
+ raw.get("empty_flatbed_max_gray_drop", 35.0)),
|
|
|
empty_top_margin_ratio=float(raw.get("empty_top_margin_ratio", 0.0)),
|
|
|
allow_state3=bool(raw.get("allow_state3", True)),
|
|
|
min_diagonal_braces=int(raw.get("min_diagonal_braces", 1)),
|
|
|
@@ -1244,13 +1261,9 @@ class EvidenceV2:
|
|
|
roi = (bx0, wy0, bx1, wy1)
|
|
|
b = self._measure_body(gray, roi, scale,
|
|
|
post_len_ratio=p.empty_min_post_len_ratio)
|
|
|
- # 低分辨率/低对比素材上,严格竖棱门可能“一根都数不到”(03_C70EA 512 px
|
|
|
- # 实测 0 根),而宽松门能数到 3 根;反过来 02_C70E 用宽松门会数到 20 根
|
|
|
- # (>max_door_posts)。所以**先严格、数不够再回退宽松**,其余门不变。
|
|
|
- if (int(b["posts"]) < p.empty_min_posts
|
|
|
- and p.body_min_post_len_ratio < p.empty_min_post_len_ratio):
|
|
|
- b = self._measure_body(gray, roi, scale,
|
|
|
- post_len_ratio=p.body_min_post_len_ratio)
|
|
|
+ # 不再“严格数不够就回退宽松”:实测 `img01`(NX70A 平车+蓝箱)在宽松门下会被
|
|
|
+ # 聚成 7 根“立柱” → 越过空车门变成 3(危险误判)。而空 C70(02=9 根)、
|
|
|
+ # 空 C70EA(03=5 根)在严格门下本来就够数,回退没有收益。
|
|
|
stats = {
|
|
|
"posts": int(b["posts"]),
|
|
|
"span": float(b["span"]),
|
|
|
@@ -1305,6 +1318,107 @@ class EvidenceV2:
|
|
|
out["hits"] = min(1.0, 0.45 + 0.04 * b["posts"] + 0.25 * b["opacity"])
|
|
|
return out
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ def _plane_stats(gray: np.ndarray, roi: Tuple[int, int, int, int],
|
|
|
+ block: int = 16, th: float = 50.0,
|
|
|
+ hit: float = 0.03) -> Optional[Dict[str, float]]:
|
|
|
+ """ROI 的灰度均值 + 竖向/横向边缘块覆盖率(与 ``find_texture_top`` 同口径)。
|
|
|
+
|
|
|
+ 车底板几乎无纹理(两项覆盖率都低);集装箱侧壁有瓦楞/立柱(覆盖率高)。
|
|
|
+ """
|
|
|
+
|
|
|
+ x0, y0, x1, y1 = [int(v) for v in roi]
|
|
|
+ x0, y0 = max(0, x0), max(0, y0)
|
|
|
+ x1 = min(int(gray.shape[1]), x1)
|
|
|
+ y1 = min(int(gray.shape[0]), y1)
|
|
|
+ if x1 - x0 < 16 or y1 - y0 < 6:
|
|
|
+ return None
|
|
|
+ crop = gray[y0:y1, x0:x1]
|
|
|
+ gx = np.abs(cv2.Sobel(crop, cv2.CV_32F, 1, 0, ksize=3))
|
|
|
+ gy = np.abs(cv2.Sobel(crop, cv2.CV_32F, 0, 1, ksize=3))
|
|
|
+ nb = max(1, crop.shape[1] // block)
|
|
|
+ vhit = 0
|
|
|
+ hhit = 0
|
|
|
+ for b in range(nb):
|
|
|
+ xs = b * block
|
|
|
+ xe = min(crop.shape[1], xs + block)
|
|
|
+ if xe <= xs:
|
|
|
+ continue
|
|
|
+ if float((gx[:, xs:xe] >= th).mean()) >= hit:
|
|
|
+ vhit += 1
|
|
|
+ if float((gy[:, xs:xe] >= th).mean()) >= hit:
|
|
|
+ hhit += 1
|
|
|
+ return {
|
|
|
+ "mean": float(crop.mean()),
|
|
|
+ "vcov": vhit / float(nb),
|
|
|
+ "hcov": hhit / float(nb),
|
|
|
+ }
|
|
|
+
|
|
|
+ def _detect_empty_flatbed(self, gray: np.ndarray,
|
|
|
+ window: Tuple[int, int, int, int],
|
|
|
+ box, bed, deck_edge: bool, under_deck: bool,
|
|
|
+ gap_px: Optional[float]) -> Optional[Dict[str, object]]:
|
|
|
+ """空平车(平车、车面裸露)证据 —— P0-Q10,判据见 ``EvidenceV2Params``。
|
|
|
+
|
|
|
+ 与载车的本质差别:**载车必然有箱底暗带紧贴(或很近)车面边梁**,空平车的
|
|
|
+ “箱底”其实是车底板**远端边缘线**,与近侧边梁之间隔着整片车面(纵深
|
|
|
+ ≥ ``empty_flatbed_min_gap_px``)。再要求那片车面**光滑 + 亮**,排除集装箱
|
|
|
+ 侧壁(瓦楞/立柱 → 边缘覆盖率高)和地面(暗且纹理多)。
|
|
|
+ """
|
|
|
+
|
|
|
+ p = self.p
|
|
|
+ if not p.empty_flatbed_enable:
|
|
|
+ return None
|
|
|
+ if bed is None or float(bed.height) <= 0.0:
|
|
|
+ return None
|
|
|
+ if not under_deck:
|
|
|
+ return None
|
|
|
+ # 边梁必须是“薄边”(厚亮面是地面/月台/下层箱顶)
|
|
|
+ est_h = max(60.0, (float(box.y0) - 0.05 * float(gray.shape[0]))
|
|
|
+ if box is not None else 0.45 * float(gray.shape[0]))
|
|
|
+ if float(bed.height) > p.contact_max_h_ratio * est_h:
|
|
|
+ return {"ok": False, "reason": "empty_flatbed_thick_sill",
|
|
|
+ "stats": {}, "roi": (0, 0, 0, 0)}
|
|
|
+ # 载车:箱底暗带紧贴/贴近边梁(实测 01/img00/img01 三张载车样本 gap≈0)
|
|
|
+ if box is not None and (gap_px is None
|
|
|
+ or float(gap_px) < p.empty_flatbed_min_gap_px):
|
|
|
+ return None
|
|
|
+ wh, ww = gray.shape[:2]
|
|
|
+ sill = float(bed.y0)
|
|
|
+ depth = float(gap_px) if gap_px is not None else 0.0
|
|
|
+ plane_h = int(round(min(max(depth, p.empty_flatbed_min_gap_px), 0.25 * wh)))
|
|
|
+ top = max(int(window[1]), int(round(sill)) - plane_h)
|
|
|
+ bot = int(round(sill))
|
|
|
+ if bot - top < 6:
|
|
|
+ return None
|
|
|
+ roi = (int(window[0]), top, int(window[2]), bot)
|
|
|
+ stats = self._plane_stats(gray, roi)
|
|
|
+ if stats is None:
|
|
|
+ return None
|
|
|
+ sill_mean = _row_mean(gray, int(window[0]), int(window[2]),
|
|
|
+ int(round(bed.y0)), max(int(round(bed.y1)), int(round(bed.y0)) + 1))
|
|
|
+ out: Dict[str, object] = {
|
|
|
+ "ok": False,
|
|
|
+ "reason": "empty_flatbed_plane",
|
|
|
+ "stats": dict(stats, sill_mean=sill_mean, plane_h=bot - top, depth=depth),
|
|
|
+ "roi": roi,
|
|
|
+ }
|
|
|
+ if stats["vcov"] > p.empty_flatbed_max_plane_edge:
|
|
|
+ out["reason"] = "empty_flatbed_texture"
|
|
|
+ return out
|
|
|
+ if stats["hcov"] > p.empty_flatbed_max_plane_edge * 1.5:
|
|
|
+ out["reason"] = "empty_flatbed_texture"
|
|
|
+ return out
|
|
|
+ if stats["mean"] < sill_mean - p.empty_flatbed_max_gray_drop:
|
|
|
+ out["reason"] = "empty_flatbed_dark"
|
|
|
+ return out
|
|
|
+ edge_ratio = min(1.0, stats["vcov"] / max(1e-6, p.empty_flatbed_max_plane_edge))
|
|
|
+ out["ok"] = True
|
|
|
+ out["reason"] = "empty_flatbed"
|
|
|
+ out["confidence"] = min(1.0, 0.65 + 0.20 * (1.0 - edge_ratio))
|
|
|
+ out["hits"] = min(1.0, 0.62 + 0.25 * (1.0 - edge_ratio))
|
|
|
+ return out
|
|
|
+
|
|
|
def _measure_body(self, gray: np.ndarray, roi: Tuple[int, int, int, int],
|
|
|
scale: float,
|
|
|
post_len_ratio: Optional[float] = None) -> Dict[str, float]:
|
|
|
@@ -1735,6 +1849,7 @@ class EvidenceV2:
|
|
|
body_roi = None
|
|
|
empty_flag = False
|
|
|
empty_reason = ""
|
|
|
+ flat_stats: Optional[Dict[str, float]] = None
|
|
|
# P0-Q9:空车识别(state=3)。判据只看 texture_top 以下那一片**敞车侧墙**:
|
|
|
# 完整的一节空敞车侧墙 → 3;只是“看得见车体结构但不够完整” → 保持旧的
|
|
|
# 弃权口径(255 ambiguous_empty_vehicle),绝不判 1/2。
|
|
|
@@ -1770,6 +1885,29 @@ class EvidenceV2:
|
|
|
hits = 0.0
|
|
|
reason = "ambiguous_empty_vehicle"
|
|
|
body_stats = estats
|
|
|
+ # P0-Q10:空平车(平车、车面裸露)→ 也输出 3。
|
|
|
+ # 与空敞车**互斥**:上面已先判敞车侧墙;这里是“薄车面边梁 + 车底透空 +
|
|
|
+ # 边梁上方是一片光滑亮车面 + 没有箱底紧贴边梁”。载车(01/img00/img01)
|
|
|
+ # 的箱底暗带 gap≈0 → 直接排除。
|
|
|
+ if not empty_flag and state != 1:
|
|
|
+ flat = self._detect_empty_flatbed(
|
|
|
+ gray, window,
|
|
|
+ box if box is not None and float(box.height) > 0.0 else None,
|
|
|
+ bed if bed is not None and float(bed.height) > 0.0 else None,
|
|
|
+ bool(deck_edge), bool(under_deck), gap_px,
|
|
|
+ )
|
|
|
+ if flat is not None:
|
|
|
+ if flat.get("ok"):
|
|
|
+ state = 3
|
|
|
+ conf = float(flat["confidence"])
|
|
|
+ hits = float(flat["hits"])
|
|
|
+ reason = "empty_flatbed"
|
|
|
+ gap_px = 0.0
|
|
|
+ empty_flag = True
|
|
|
+ empty_reason = "empty_flatbed"
|
|
|
+ flat_stats = dict(flat["stats"]) # type: ignore[arg-type]
|
|
|
+ elif not empty_reason:
|
|
|
+ empty_reason = str(flat.get("reason") or "")
|
|
|
gap_m = (
|
|
|
None if gap_px is None or px_per_m is None or px_per_m <= 0.0
|
|
|
else gap_px / px_per_m
|
|
|
@@ -1858,6 +1996,7 @@ class EvidenceV2:
|
|
|
"body_fallback": bool(body),
|
|
|
"empty_vehicle": bool(empty_flag),
|
|
|
"empty_reason": empty_reason,
|
|
|
+ "empty_flatbed": flat_stats,
|
|
|
"body_wall_h_ratio": (
|
|
|
None if not body_stats else body_stats.get("wall_h_ratio")
|
|
|
),
|