STDC/tools/check/check_lane_offset.py
charlie880624 7716a0060f
Some checks failed
deploy / build-n-publish (push) Has been cancelled
lint / lint (push) Has been cancelled
build / build_cpu (3.7, 1.5.1, torch1.5, 0.6.1) (push) Has been cancelled
build / build_cpu (3.7, 1.6.0, torch1.6, 0.7.0) (push) Has been cancelled
build / build_cpu (3.7, 1.7.0, torch1.7, 0.8.1) (push) Has been cancelled
build / build_cpu (3.7, 1.8.0, torch1.8, 0.9.0) (push) Has been cancelled
build / build_cpu (3.7, 1.9.0, torch1.9, 0.10.0) (push) Has been cancelled
build / build_cuda101 (3.7, 1.5.1+cu101, torch1.5, 0.6.1+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.6.0+cu101, torch1.6, 0.7.0+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.7.0+cu101, torch1.7, 0.8.1+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.8.0+cu101, torch1.8, 0.9.0+cu101) (push) Has been cancelled
build / build_cuda102 (3.6, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.7, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.8, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.9, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / test_windows (windows-2022, cpu, 3.8) (push) Has been cancelled
build / test_windows (windows-2022, cu111, 3.8) (push) Has been cancelled
feat: add golf dataset, kneron configs, and tools
- Add golf1/2/4/7/8 dataset classes for semantic segmentation
- Add kneron-specific configs (meconfig series, kn_stdc1_golf4class)
- Organize scripts into tools/check/ and tools/kneron/
- Add kneron_preprocessing module
- Update README with quick-start guide
- Update .gitignore to exclude data dirs, onnx, nef outputs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 13:14:30 +08:00

71 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
import numpy as np
# === 1. 檔案與參數設定 ===
img_path = r'C:\Users\rd_de\kneronstdc\work_dirs\vis_results\good\pic_0441_jpg.rf.6e56eb8c0bed7f773fb447b9e217f779_leftImg8bit.png'
# 色彩轉 label IDRGB
CLASS_RGB_TO_ID = {
(128, 64, 128): 3, # road
(0, 255, 0): 1, # grass
(255, 0, 255): 9, # background or sky可忽略
}
ROAD_ID = 3
GRASS_ID = 1
# === 2. 讀圖並轉為 label mask ===
bgr_img = cv2.imread(img_path)
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
height, width, _ = rgb_img.shape
label_mask = np.zeros((height, width), dtype=np.uint8)
for rgb, label in CLASS_RGB_TO_ID.items():
match = np.all(rgb_img == rgb, axis=-1)
label_mask[match] = label
# === 3. 分析畫面中下區域 ===
y_start = int(height * 0.6)
x_start = int(width * 0.4)
x_end = int(width * 0.6)
roi = label_mask[y_start:, x_start:x_end]
total_pixels = roi.size
road_pixels = np.sum(roi == ROAD_ID)
grass_pixels = np.sum(roi == GRASS_ID)
road_ratio = road_pixels / total_pixels
grass_ratio = grass_pixels / total_pixels
# === 4. 重心偏移分析 ===
road_mask = (label_mask == ROAD_ID).astype(np.uint8)
M = cv2.moments(road_mask)
center_x = width // 2
offset = 0
cx = center_x
if M["m00"] > 0:
cx = int(M["m10"] / M["m00"])
offset = cx - center_x
# === 5. 結果輸出 ===
print(f"🔍 中央 ROI - road比例: {road_ratio:.2f}, grass比例: {grass_ratio:.2f}")
if road_ratio < 0.5:
print("⚠️ 偏離道路ROI 中道路比例過少)")
if grass_ratio > 0.3:
print("❗ 車輛壓到草地!")
if abs(offset) > 40:
print(f"⚠️ 道路重心偏移:{offset} px")
else:
print("✅ 道路重心正常")
# === 6. 可視化 ===
vis_img = bgr_img.copy()
cv2.rectangle(vis_img, (x_start, y_start), (x_end, height), (0, 255, 255), 2) # 黃色框 ROI
cv2.line(vis_img, (center_x, 0), (center_x, height), (255, 0, 0), 2) # 藍色中心線
cv2.circle(vis_img, (cx, height // 2), 6, (0, 0, 255), -1) # 紅色重心點
# 輸出圖片
save_path = r'C:\Users\rd_de\kneronstdc\work_dirs\vis_results\good\visual_check.png'
cv2.imwrite(save_path, vis_img)
print(f"✅ 分析圖儲存成功:{save_path}")