1. 安装基础工具
sudo apt install -y git wget curl build-essential cmake python3-pip python3-venv
2. 创建Python虚拟环境
python3 -m venv ~/yolo_env
source ~/yolo_env/bin/activate
3. 配置pip镜像
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF
# 配置多个pip镜像源
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
extra-index-url =
https://pypi.doubanio.com/simple/
https://mirrors.aliyun.com/pypi/simple/
https://pypi.mirrors.ustc.edu.cn/simple/
timeout = 120
retries = 5
[install]
trusted-host =
pypi.tuna.tsinghua.edu.cn
pypi.doubanio.com
mirrors.aliyun.com
pypi.mirrors.ustc.edu.cn
EOF
4. 安装Python依赖
pip install --upgrade pip
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
pip install ultralytics opencv-python numpy
5. 测试安装
python3 << 'EOF'
from ultralytics import YOLO
import torch
import cv2
print("YOLO安装成功")
print("PyTorch版本:", torch.__version__)
print("OpenCV版本:", cv2.__version__)
EOF
太棒了!🎉 模型文件都已经复制好了!
现在在树莓派上执行以下命令来测试:
1. 进入项目目录
cd ~/yolo_project/ros_package
2. 给脚本添加执行权限
chmod +x scripts/*.sh
3. 激活Python环境
source ~/yolo_env/bin/activate
4. 测试模型加载
./scripts/test_model.sh
如果模型测试成功,接下来测试摄像头:
测试摄像头
./scripts/test_camera.sh
最后运行检测:
选择相机4
玉米病害检测
./scripts/quick_detect.sh corn 0
请告诉我测试结果!
demo_detect.py
现在把修改后的文件复制到树莓派,然后重新运行。
cd ~/yolo_project/ros_package/scripts
python3 demo_detect.py corn 4
或者,你可以在树莓派上直接运行这个命令来测试显示是否正常:
先测试摄像头是否能显示
python3 -c "
import cv2
cap = cv2.VideoCapture(4)
while True:
ret, frame = cap.read()
if ret:
cv2.imshow('Test', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
"
如果这个也是黑屏,可能是树莓派的显示环境问题。试试:
设置显示环境变量后运行
export QT_QPA_PLATFORM=xcb
export DISPLAY=:0
python3 demo_detect.py corn 4
或者如果你是通过 VNC 远程连接的,可能需要:
export DISPLAY=:1
python3 demo_detect.py corn 4