7.9 KiB
comments | description | keywords |
---|---|---|
true | Quick start guide to setting up YOLO on a Raspberry Pi with a Pi Camera using the libcamera stack. Detailed comparison between Raspberry Pi 3, 4 and 5 models. | Ultralytics, YOLO, Raspberry Pi, Pi Camera, libcamera, quick start guide, Raspberry Pi 4 vs Raspberry Pi 5, YOLO on Raspberry Pi, hardware setup, machine learning, AI |
Quick Start Guide: Raspberry Pi and Pi Camera with YOLOv5 and YOLOv8
This comprehensive guide aims to expedite your journey with YOLO object detection models on a Raspberry Pi using a Pi Camera. Whether you're a student, hobbyist, or a professional, this guide is designed to get you up and running in less than 30 minutes. The instructions here are rigorously tested to minimize setup issues, allowing you to focus on utilizing YOLO for your specific projects.
Watch: Raspberry Pi 5 updates and improvements.
Prerequisites
- Raspberry Pi 3, 4 or 5
- Pi Camera
- 64-bit Raspberry Pi Operating System
Connect the Pi Camera to your Raspberry Pi via a CSI cable and install the 64-bit Raspberry Pi Operating System. Verify your camera with the following command:
libcamera-hello
You should see a video feed from your camera.
Choose Your YOLO Version: YOLOv5 or YOLOv8
This guide offers you the flexibility to start with either YOLOv5 or YOLOv8. Both versions have their unique advantages and use-cases. The choice is yours, but remember, the guide's aim is not just quick setup but also a robust foundation for your future work in object detection.
Hardware Specifics: Raspberry Pi 3 vs Raspberry Pi 4
Raspberry Pi 3 and Raspberry Pi 4 have distinct hardware specifications, and the YOLO installation and configuration process can vary slightly depending on which model you're using.
Raspberry Pi 3
- CPU: 1.2GHz Quad-Core ARM Cortex-A53
- RAM: 1GB LPDDR2
- USB Ports: 4 x USB 2.0
- Network: Ethernet & Wi-Fi 802.11n
- Performance: Generally slower, may require lighter YOLO models for real-time processing
- Power Requirement: 2.5A power supply
- Official Documentation: Raspberry Pi 3 Documentation
Raspberry Pi 4
- CPU: 1.5GHz Quad-core 64-bit ARM Cortex-A72 CPU
- RAM: Options of 2GB, 4GB or 8GB LPDDR4
- USB Ports: 2 x USB 2.0, 2 x USB 3.0
- Network: Gigabit Ethernet & Wi-Fi 802.11ac
- Performance: Faster, capable of running more complex YOLO models in real-time
- Power Requirement: 3.0A USB-C power supply
- Official Documentation: Raspberry Pi 4 Documentation
Raspberry Pi 5
- CPU: 2.4GHz Quad-core 64-bit Arm Cortex-A76 CPU
- GPU: VideoCore VII, supporting OpenGL ES 3.1, Vulkan 1.2
- Display Output: Dual 4Kp60 HDMI
- Decoder: 4Kp60 HEVC
- Network: Gigabit Ethernet with PoE+ support, Dual-band 802.11ac Wi-Fi®, Bluetooth 5.0 / BLE
- USB Ports: 2 x USB 3.0, 2 x USB 2.0
- Other Features: High-speed microSD card interface with SDR104 mode, 2 × 4-lane MIPI camera/display transceivers, PCIe 2.0 x1 interface, standard 40-pin GPIO header, real-time clock, power button
- Power Requirement: Specifics not yet available, expected to require a higher amperage supply
- Official Documentation: Raspberry Pi 5 Documentation
Please make sure to follow the instructions specific to your Raspberry Pi model to ensure a smooth setup process.
Quick Start with YOLOv5
This section outlines how to set up YOLOv5 on a Raspberry Pi 3 or 4 with a Pi Camera. These steps are designed to be compatible with the libcamera camera stack introduced in Raspberry Pi OS Bullseye.
Install Necessary Packages
-
Update the Raspberry Pi:
sudo apt-get update sudo apt-get upgrade -y sudo apt-get autoremove -y
-
Clone the YOLOv5 repository:
cd ~ git clone https://github.com/Ultralytics/yolov5.git
-
Install the required dependencies:
cd ~/yolov5 pip3 install -r requirements.txt
-
For Raspberry Pi 3, install compatible versions of PyTorch and Torchvision (skip for Raspberry Pi 4):
pip3 uninstall torch torchvision pip3 install torch==1.11.0 torchvision==0.12.0
Modify detect.py
To enable TCP streams via SSH or the CLI, minor modifications are needed in detect.py
.
-
Open
detect.py
:sudo nano ~/yolov5/detect.py
-
Find and modify the
is_url
line to accept TCP streams:is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://', 'tcp://'))
-
Comment out the
view_img
line:# view_img = check_imshow(warn=True)
-
Save and exit:
CTRL + O -> ENTER -> CTRL + X
Initiate TCP Stream with Libcamera
-
Start the TCP stream:
libcamera-vid -n -t 0 --width 1280 --height 960 --framerate 1 --inline --listen -o tcp://127.0.0.1:8888
Keep this terminal session running for the next steps.
Perform YOLOv5 Inference
-
Run the YOLOv5 detection:
cd ~/yolov5 python3 detect.py --source=tcp://127.0.0.1:8888
Quick Start with YOLOv8
Follow this section if you are interested in setting up YOLOv8 instead. The steps are quite similar but are tailored for YOLOv8's specific needs.
Install Necessary Packages
-
Update the Raspberry Pi:
sudo apt-get update sudo apt-get upgrade -y sudo apt-get autoremove -y
-
Install YOLOv8:
pip3 install ultralytics
-
Reboot:
sudo reboot
Modify build.py
Just like YOLOv5, YOLOv8 also needs minor modifications to accept TCP streams.
-
Open
build.py
located in the Ultralytics package folder:sudo nano /home/pi/.local/lib/pythonX.X/site-packages/ultralytics/build.py
-
Find and modify the
is_url
line to accept TCP streams:is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://', 'tcp://'))
-
Save and exit:
CTRL + O -> ENTER -> CTRL + X
Initiate TCP Stream with Libcamera
-
Start the TCP stream:
libcamera-vid -n -t 0 --width 1280 --height 960 --framerate 1 --inline --listen -o tcp://127.0.0.1:8888
Perform YOLOv8 Inference
To perform inference with YOLOv8, you can use the following Python code snippet:
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
results = model('tcp://127.0.0.1:8888', stream=True)
while True:
for result in results:
boxes = result.boxes
probs = result.probs
Next Steps
Congratulations on successfully setting up YOLO on your Raspberry Pi! For further learning and support, visit Ultralytics and KashmirWorldFoundation.
Acknowledgements and Citations
This guide was initially created by Daan Eeltink for Kashmir World Foundation, an organization dedicated to the use of YOLO for the conservation of endangered species. We acknowledge their pioneering work and educational focus in the realm of object detection technologies.
For more information about Kashmir World Foundation's activities, you can visit their website.