Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e4b4846
性能优化
haruDT Dec 18, 2025
2a08aa6
性能优化
haruDT Dec 18, 2025
34f8b40
删除仓库根目录下的site文件夹
haruDT Dec 19, 2025
bd5eba8
新增requirements.txt文件
haruDT Dec 19, 2025
c82b068
修改README文件,新增requirements文件
haruDT Dec 19, 2025
3b507d4
修改README文件,新增requirements文件
haruDT Dec 19, 2025
6bb9e4d
Merge branch 'main' into main
haruDT Dec 19, 2025
e117740
模块重组
haruDT Dec 19, 2025
b1f4240
Merge branch 'main' of https://github.com/haruDT/nn模块重组
haruDT Dec 19, 2025
f7a8321
目标ID颜色编码
haruDT Dec 19, 2025
1d74b0a
新增实时统计面板
haruDT Dec 19, 2025
f6b0785
修复问号问题
haruDT Dec 20, 2025
80f38cf
新增实时统计面板
haruDT Dec 20, 2025
99cbf97
Merge branch 'main' into main
haruDT Dec 20, 2025
e80d7a1
新增实时统计面板
haruDT Dec 21, 2025
dae1d5a
Merge branch 'main' into main
haruDT Dec 21, 2025
7e07ea8
新增实时统计面板
haruDT Dec 21, 2025
40958b0
完成远程main分支的合并,解决代码冲突
haruDT Dec 21, 2025
20b3e8f
恢复误修改的文件
haruDT Dec 21, 2025
bb08dbe
修改相对路径
haruDT Dec 21, 2025
d935aa2
修改绝对路径
haruDT Dec 21, 2025
026b433
随机地点生成主车
haruDT Dec 21, 2025
490eb1e
随机地点生成自车
haruDT Dec 22, 2025
e2659d2
新增卫星视角跟随主车
haruDT Dec 22, 2025
18ac7ff
Merge branch 'main' into main
haruDT Dec 22, 2025
1fd8e11
使用配置文件切换视角
haruDT Dec 22, 2025
321ceb9
Merge branch 'main' into main
haruDT Dec 22, 2025
1da7a1c
优化检测范围
haruDT Dec 23, 2025
fb43939
优化检测范围
haruDT Dec 23, 2025
572858f
Merge branch 'main' into main
haruDT Dec 23, 2025
4e4dbb4
多余部分优化
haruDT Dec 24, 2025
15b527f
多余部分优化
haruDT Dec 24, 2025
f1b6c7c
修复问题
haruDT Dec 24, 2025
7ec6d5e
Merge branch 'main' into main
haruDT Dec 24, 2025
d4461de
自适应帧率
haruDT Dec 24, 2025
d74c81d
Merge branch 'main' of https://github.com/haruDT/nn
haruDT Dec 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 17 additions & 59 deletions src/tracking_car/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ port: 2000
timeout: 20.0

# ======================== 传感器配置 ========================
# 增大分辨率和视野,提升远距离检测能力
img_width: 1280 # ↑ 从640提升到1280,让远处车辆更清晰
img_height: 960 # ↑ 从480提升到960
fov: 110 # ↑ 从90提升到110,扩大视野范围
img_width: 1280
img_height: 960
fov: 110
sensor_tick: 0.05
use_lidar: true # 保持LiDAR开启,用于距离验证
use_lidar: true

# ======================== LiDAR配置 ========================
lidar_channels: 32
lidar_range: 100.0
lidar_points_per_second: 500000

# ======================== 检测模型配置 ========================
# 关键优化:降低阈值,增大输入尺寸
yolo_model: "yolov8n.pt" # 可选:换成 yolov8m.pt 效果更好
conf_thres: 0.25 # ↓ 从0.5降低到0.25,检测更多远处目标
iou_thres: 0.3 # 保持
device: "cuda" if torch.cuda.is_available() else "cpu"
yolo_imgsz_max: 640 # ↑ 从320提升到640,提升小目标检测能力
yolo_iou: 0.45
yolo_quantize: false
yolo_model: "yolov8n.pt"
conf_thres: 0.25
iou_thres: 0.3
device: "cpu"
yolo_imgsz_max: 640

# ======================== 跟踪算法配置 ========================
max_age: 5
Expand All @@ -39,63 +35,25 @@ display_fps: 30

# ======================== 轨迹历史配置 ========================
track_history_len: 20
track_line_width: 2
track_alpha: 0.6

# ======================== 行为分析配置 ========================
# 针对远距离检测调整参数
stop_speed_thresh: 0.8 # ↓ 降低停车速度阈值
stop_speed_thresh: 0.8
stop_frames_thresh: 5
overtake_speed_ratio: 1.5
overtake_dist_thresh: 60.0 # ↑ 增大超车距离阈值
overtake_dist_thresh: 60.0
lane_change_thresh: 0.4
brake_accel_thresh: 2.0
turn_angle_thresh: 15.0
danger_dist_thresh: 15.0 # ↑ 增大危险距离阈值,适应更远检测
danger_dist_thresh: 15.0
predict_frames: 10

# ======================== 天气与NPC配置 ========================
default_weather: "clear" # 使用清晰天气,有利于远距离检测
default_weather: "clear"
num_npcs: 20

# ======================== 性能优化配置 ========================
auto_adjust_detection: true # 自动调整检测参数
smooth_alpha: 0.2
fps_window_size: 15

# ======================== 传感器融合配置 ========================
fuse_lidar_vision: true # 启用传感器融合

# ======================== 数据记录配置 ========================
record_data: true
record_dir: "track_records"
record_format: "csv"
record_fps: 10
save_screenshots: false

# ======================== 3D可视化配置 ========================
use_3d_visualization: true
pcd_view_size: 800

# ======================== 检测框样式配置 ========================
det_box_color: [255, 0, 0] # 蓝色框
det_box_thickness: 2
det_label_bg_color: [0, 0, 0] # 黑色背景
det_label_text_color: [255, 255, 255] # 白色文字
det_label_font_scale: 0.5
det_label_padding: 5

# ======================== 视角控制配置 ========================
view:
default_mode: "satellite" # 默认视角模式
satellite_height: 50.0 # 卫星视角高度
behind_distance: 10.0 # 后方视角距离
first_person_height: 1.6 # 第一人称视角高度

# ======================== 新增:远距离检测专用配置 ========================
far_detection:
enabled: true # 启用远距离检测模式
min_confidence: 0.2 # 远距离目标最低置信度
min_pixel_size: 15 # 最小检测像素尺寸(宽或高)
scale_factor: 1.5 # 图像缩放因子用于小目标检测
enable_multi_scale: true # 启用多尺度检测
default_mode: "satellite"
satellite_height: 50.0
behind_distance: 10.0
first_person_height: 1.6
49 changes: 22 additions & 27 deletions src/tracking_car/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
print(" - tracker.py")
sys.exit(1)


# ======================== Configuration Management ========================

def load_config(config_path=None):
Expand Down Expand Up @@ -115,7 +114,6 @@ def load_config(config_path=None):

return default_config


def setup_carla_client(config):
"""
Setup CARLA client
Expand Down Expand Up @@ -157,7 +155,6 @@ def setup_carla_client(config):
logger.error(f"[ERROR] Failed to connect to CARLA server: {e}")
return None, None


def set_weather(world, weather_name):
"""
Set weather
Expand All @@ -182,7 +179,6 @@ def set_weather(world, weather_name):
else:
logger.warning(f"Unknown weather: {weather_name}, using clear weather")


# ======================== Visualization (Enhanced: Independent stats window) ========================

class Visualizer:
Expand Down Expand Up @@ -1117,7 +1113,6 @@ def destroy(self):
cv2.destroyAllWindows()
logger.info("[OK] All visualization windows closed")


# ======================== Main Program ========================

class CarlaTrackingSystem:
Expand Down Expand Up @@ -1534,33 +1529,35 @@ def _handle_keyboard_input(self, key):
logger.info(f"[VIEW] 切换到 {mode_name}")

def _control_frame_rate(self, current_fps):
"""Control frame rate"""
"""自适应帧率控制(简单版)"""
import time
target_fps = self.config.get('display_fps', 30)
import psutil

# 获取当前系统负载
cpu_usage = psutil.cpu_percent()

# 根据CPU使用率动态调整目标FPS
if cpu_usage > 85: # 非常高负载
target_fps = max(10, self.config.get('display_fps', 30) * 0.5)
elif cpu_usage > 70: # 高负载
target_fps = max(15, self.config.get('display_fps', 30) * 0.7)
elif cpu_usage < 40: # 低负载,可以尝试更高FPS
target_fps = min(60, self.config.get('display_fps', 30) * 1.5)
elif cpu_usage < 60: # 中等负载
target_fps = min(45, self.config.get('display_fps', 30) * 1.2)
else: # 正常负载
target_fps = self.config.get('display_fps', 30)

if target_fps <= 0:
return

target_interval = 1.0 / target_fps
# If frame rate is too high, sleep appropriately
if current_fps > target_fps * 1.2: # Allow 20% fluctuation

# 如果帧率过高,适当休眠
if current_fps > target_fps * 1.2: # 允许20%波动
sleep_time = max(0, target_interval - (1.0 / current_fps))
time.sleep(sleep_time)

def _save_screenshot(self):
"""Save screenshot"""
try:
import time
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"screenshot_{timestamp}_{self.frame_count:06d}.png"

# Get currently displayed image
screenshot = self.sensor_manager.get_camera_image()
if utils.valid_img(screenshot):
utils.save_image(screenshot, filename)
logger.info(f"[SCREENSHOT] Screenshot saved: {filename}")
except Exception as e:
logger.warning(f"Screenshot save failed: {e}")

def _print_status(self, stats_data):
"""Print system status"""
Expand Down Expand Up @@ -1613,7 +1610,6 @@ def cleanup(self):

logger.info("[OK] Resource cleanup complete")


# ======================== Main Function ========================

def main():
Expand Down Expand Up @@ -1692,7 +1688,6 @@ def main():
logger.info("[END] Program ended")
logger.info("=" * 50)


if __name__ == "__main__":
# 检查配置
try:
Expand Down
21 changes: 11 additions & 10 deletions src/tracking_car/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# 核心依赖
# 可选依赖

carla>=0.9.14
opencv-python>=4.8.0
loguru>=0.7.0
matplotlib>=3.7.0
numba>=0.57.0
numpy>=1.24.0
torch>=2.0.0
ultralytics>=8.0.0
open3d>=0.17.0
opencv-python>=4.8.0
pandas>=2.0.0
psutil>=5.9.0
scipy>=1.10.0
scikit-learn>=1.2.0
numba>=0.57.0
loguru>=0.7.0
pyyaml>=6.0.0
# 可选依赖
matplotlib>=3.7.0
pandas>=2.0.0
scikit-learn>=1.2.0
scipy>=1.10.0
torch>=2.0.0
ultralytics>=8.0.0
15 changes: 1 addition & 14 deletions src/tracking_car/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
包含:相机、LiDAR传感器封装和管理
"""

import random # 添加随机模块支持
import carla
import cv2
import numpy as np
Expand All @@ -23,7 +24,6 @@
import open3d as o3d
from sklearn.cluster import DBSCAN


class CameraManager:
"""相机管理器"""

Expand Down Expand Up @@ -147,7 +147,6 @@ def destroy(self):
logger.warning(f"销毁相机失败: {e}")
self.camera = None


class LiDARManager:
"""LiDAR管理器"""

Expand Down Expand Up @@ -368,8 +367,6 @@ def destroy(self):
logger.warning(f"销毁LiDAR失败: {e}")
self.lidar = None

# ======================== 视角管理器 ========================

class SpectatorManager:
"""CARLA视角管理器 - 提供卫星视角跟随"""

Expand Down Expand Up @@ -681,9 +678,6 @@ def destroy(self):

logger.info("✅ 所有传感器已销毁")


# ======================== 工具函数 ========================

def create_ego_vehicle(world, config, spawn_points=None):
"""
创建自车
Expand Down Expand Up @@ -797,7 +791,6 @@ def create_ego_vehicle(world, config, spawn_points=None):
traceback.print_exc()
return None


def spawn_npc_vehicles(world, config, count=None):
"""
生成NPC车辆
Expand Down Expand Up @@ -848,7 +841,6 @@ def spawn_npc_vehicles(world, config, count=None):
continue

# 随机选择车辆蓝图
import random
vehicle_bp = random.choice(vehicle_bps)

# 尝试生成
Expand All @@ -874,7 +866,6 @@ def spawn_npc_vehicles(world, config, count=None):
logger.error(f"生成NPC车辆失败: {e}")
return 0


def clear_all_actors(world, exclude_ids=None):
"""
清理所有演员(车辆和传感器)
Expand Down Expand Up @@ -927,9 +918,6 @@ def clear_all_actors(world, exclude_ids=None):
except Exception as e:
logger.warning(f"清理演员时出错: {e}")


# ======================== 测试函数 ========================

def test_sensor_manager():
"""测试传感器管理器"""
print("=" * 50)
Expand All @@ -953,6 +941,5 @@ def test_sensor_manager():

return True


if __name__ == "__main__":
test_sensor_manager()
Loading
Loading