问题分析
你的问题核心是:修改的 JSON 配置文件在启动时"变回"官方配置。这通常不是文件被覆盖,而是启动命令没有正确加载你的自定义配置文件。
从社区类似案例来看,这是配置路径或启动参数的问题。
解决方案
步骤 1:确认配置文件路径和权限
# 1. 确认你的配置文件位置
ls -la /opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json
# 2. 备份官方配置文件(重要!)
sudo cp /opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json \
/opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json.backup
# 3. 确认你的自定义模型 bin 文件位置
ls -la /path/to/your/custom_model.bin
步骤 2:正确修改配置文件
编辑配置文件,确保以下关键字段正确:
sudo nano /opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json
关键配置项:
{
"model_file": "/绝对路径/到你的/custom_model.bin",
"dnn_Parser": "yolov8",
"model_output_count": 6,
"reg_max": 16,
"class_num": 你的类别数,
"cls_names_list": "/绝对路径/到你的/obstacles.list",
"strides": [8, 16, 32],
"score_threshold": 0.25,
"nms_threshold": 0.7
}
注意:
- 路径必须是绝对路径
model_output_count 必须与你的模型输出头数量一致(通常 YOLOv8 是 6)
class_num 必须与你的训练类别数一致
步骤 3:使用正确的启动命令
关键: 启动时必须通过参数显式指定配置文件路径:
# 方式 1:直接指定配置文件路径(推荐)
source /opt/tros/humble/setup.bash
ros2 launch dnn_node_example dnn_node_example.launch.py \
dnn_example_config_file:=/opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json \
dnn_example_image_width:=1920 \
dnn_example_image_height:=1080 \
mipi_channel:=1
# 方式 2:先设置环境变量
source /opt/tros/humble/setup.bash
export CAM_TYPE=mipi
ros2 launch dnn_node_example dnn_node_example.launch.py
步骤 4:验证配置是否生效
启动后,检查日志确认加载的是你的配置文件:
# 查看日志中 config_file 的值
# 应该显示你的配置文件路径,而不是默认的 fcosworkconfig.json
如果日志显示 config_file: config/fcosworkconfig.json,说明你的参数没有生效。
常见问题排查
问题 1:配置文件"变回"官方
原因: dnn_example_config_file 参数路径错误或未传递成功。
解决:
# 检查 launch 文件是否接受该参数
ros2 launch dnn_node_example dnn_node_example.launch.py --show-args
# 确认参数名正确(注意是 dnn_example_config_file 不是 config_file)
问题 2:模型输出头不匹配
如果你的模型输出头格式与 dnn_node_example 期望的不一致,会导致 crash(exit code -11)。
验证模型输出:
python3 << 'EOF'
from hobot_dnn import pyeasy_dnn as dnn
models = dnn.load('/你的模型路径.bin')
model = models[0]
print(f"Model outputs: {len(model.outputs)}")
for i, out in enumerate(model.outputs):
p = out.properties
print(f"Output[{i}]: shape={p.shape}, layout={p.layout}")
EOF
期望输出(YOLOv8 标准格式):
Output[0]: shape=(1, 64, 80, 80), layout=NCHW # bbox_P3
Output[1]: shape=(1, 1, 80, 80), layout=NCHW # cls_P3
Output[2]: shape=(1, 64, 40, 40), layout=NCHW # bbox_P4
Output[3]: shape=(1, 1, 40, 40), layout=NCHW # cls_P4
Output[4]: shape=(1, 64, 20, 20), layout=NCHW # bbox_P5
Output[5]: shape=(1, 1, 20, 20), layout=NCHW # cls_P5
问题 3:MIPI 摄像头初始化失败
如果日志显示 mipi_cam init failure,需要:
# 1. 确认摄像头连接
v4l2-ctl --list-devices
# 2. 设置正确的摄像头类型
export CAM_TYPE=mipi # 或 usb/fb
# 3. 检查 mipi_channel 是否正确(通常是 0 或 1)
推荐参考资源
快速验证流程
# 1. 备份并修改配置
sudo cp /opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json \
/opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json.backup
# 2. 编辑配置文件(确保路径正确)
sudo nano /opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json
# 3. 启动并指定配置文件
source /opt/tros/humble/setup.bash
ros2 launch dnn_node_example dnn_node_example.launch.py \
dnn_example_config_file:=/opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json \
mipi_channel:=1
# 4. 检查日志确认配置加载
# 看到 config_file: /opt/tros/humble/lib/dnn_node_example/config/yolov8workconfig.json 即成功
如果还有问题,请提供:
- 完整的启动日志
- 你的
yolov8workconfig.json 内容
- 模型输出头验证结果