How to run YOLOv5 successfully on Raspberry Pi

Elven Kim
6 min readJun 1, 2023

--

What is YOLOv5 and why is it so popular? YOLOv5 is an object detection algorithm developed by Ultralytics. It is an evolution of the YOLO (You Only Look Once) series of real-time object detection models. YOLOv5 builds upon the earlier versions, such as YOLOv4 and YOLOv3, by introducing several improvements in terms of accuracy and speed.

The primary goal of YOLOv5 is to achieve state-of-the-art performance in object detection tasks while maintaining real-time processing speeds. The algorithm uses a single neural network to simultaneously predict bounding boxes and class probabilities for multiple objects in an image. This approach allows for efficient and fast object detection in real-world scenarios.

Some key features and improvements in YOLOv5 are YOLOv5 utilizes a more streamlined architecture compared to its predecessors, reducing computational complexity while improving accuracy. The varying model size allows flexibility.

YOLOv5 introduces a range of pre-trained models with varying sizes, allowing users to choose between smaller and faster models or larger and more accurate models based on their specific requirements.

Definitely the sell point is improved accuracy as YOLOv5 achieves higher accuracy on object detection tasks by incorporating novel techniques such as anchor-based and anchor-free bounding box prediction, advanced data augmentation strategies, and better training procedures.

Finally, YOLOv5 provides an improved training pipeline that simplifies the process of training custom object detection models on new datasets, making it more accessible to researchers and developers. As always, anything open source is always most welcomed. YOLOv5 is an open-source project, which means that the code and pre-trained models are freely available for use and modification.

The easiest way to get YOLOv5 running is to look for official sources. Therefore, to run YOLOv5 successfully, go to Ultralytics official github and look for Colab.

Before we modify anything, let’s quickly setup the Raspberry Pi.

1. Install VNC Viewer for Windows on your laptop

2. Install VNC Server on Raspberry Pi

Download the VNC-Server-7.5.0-Linux-ARM.deb and run it in Raspberry Pi.

Remember to enable the VNC under the Raspberry Pi Configuration.

Menu -> Preferences -> Raspberry Pi Configuration

You see a VNC logo appear next to the bluetooth logo on the upper right hand corner.

3. Access Raspberry Pi from your computer

Click on the logo and key in the address 192.168.0.89 in VNC Viewer on your laptop.

You will see the connection and access the Raspberry Pi without attaching a keyboard and mouse on Raspberry Pi. The Raspberry Pi now is known as headless.

3. Setup of Raspberry Pi for YOLOv5

Here are the steps to install YOLOv5 on Raspberry Pi.

This is to to upgrade Raspberry Pi and after that, install virtual environment by this command to prevent packages conflict that may render our project useless later.

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install python3-pip python3-virtualenv

mkdir project

cd project

python3 -m pip install virtualenv

python3 -m virtualenv env source env/bin/activate

A virtual/isolated environment is created. All packages are named here and hence there will be no clash in the packages version later.

install packages

sudo apt install -y build-essential cmake pkg-config libjpeg-dev libtiff5-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran libhdf5-dev libhdf5-serial-dev libhdf5–103 libqt5gui5 libqt5webkit5 libqt5test5 python3-pyqt5 python3-dev

4. Install OpenCV

2 possible installation of opencv:

pip install opencv-python for main modules

pip install opencv-contrib-python for main and additional modules

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It provides a wide range of tools and functions that enable developers to process and analyze visual data, such as images and videos.

OpenCV is written in C++ and offers interfaces for various programming languages, including Python and Java. The library includes a comprehensive collection of functions for image and video I/O (input/output), image processing, geometric transformations, filtering, feature detection and description, machine learning algorithms, camera calibration, and camera calibration. It also supports interoperability with other popular libraries and frameworks like NumPy, TensorFlow, and PyTorch.

OpenCV has been widely adopted in both academia and industry and is used in various applications, including robotics, surveillance systems, autonomous vehicles, medical imaging, and video editing. Its open-source nature allows developers to contribute to its development and take advantage of a vast community-driven ecosystem.

This will take a long time. I chose opencv-contrib-python and it took me 1.5 hours. I use the command Time to monitor the time.

5. Install all other packages

sudo apt-get install libatlas-base-dev

The command sudo apt-get install libatlas-base-dev is used to install the libatlas-base-dev package. libatlas-base-dev is a package that provides the reference implementation of the Basic Linear Algebra Subprograms (BLAS) and the Basic Linear Algebra Communication Subprograms (BLACS). These libraries are optimized for performance and are commonly used in scientific and numerical computing applications.

pip3 install tqdm

pip3 install pyyaml

pip3 install matplotlib

pip3 install seaborn

pip3 install cython

pip3 install thop

Go detect.py, comment out the line 166 that says check requirements

6. Test if OpenCV is working

Make sure you are in the correct path. Activate the env by

cd project

source env/bin/activate

python

import cv2

cv2.__version_

7. Install YOLOv5 API

git clone https://github.com/ultralytics/yolov5

4. Test out the prediction

Test out the inferences to check if the github is installed correctly.

python3 detect.py — source data/images — weights yolov5s.pt — conf 0.25

python3 detect.py — source 0 — weights yolov5s.pt — conf 0.25

5. Upload the dataset to Colab using Roboflow

We need to modify the .yaml file. First download the coco128.yaml. We modify the file as per our own custom dataset.

6. Upload the data.yaml file to be under the data folder.

7. Modify the path under data.yaml

names:

- blue-cube

nc: 6

test: /content/images/test

train: /content/images/train

val: /content/images/valid

7. Train YOLOv5s on custom data for few epochs

Test out the codes to see if they are working well.

Remember to change data.yaml

!python train.py — img 640 — batch 16 — epochs 3 — data data.yaml — weights yolov5s.pt — cache

8. Test out prediction

python detect.py --source 0  # webcam
img.jpg # image
vid.mp4 # video
screen # screenshot
path/ # directory
'path/*.jpg' # glob
'https://youtu.be/Zgi9g1ksQHc' # YouTube
'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream

REFERENCES

VNC Viewer for Windows https://www.realvnc.com/en/connect/download/viewer/

How to install OpenCV for Raspberry Pi with virtual environment

https://youtu.be/QzVYnG-WaM4

YOLOv5 object detection on Raspberry pi

https://youtu.be/pIUFXGxR_-Q:

YOLOv5 training with custom data: https://youtu.be/GRtgLlwxpc4

https://github.com/ultralytics/yolov5

raspberry pi 4 yolov5 custom object detection | How to Train YOLO v5 on a Custom Dataset | yolov5

https://www.youtube.com/watch?v=fBHvyiXE0RY&ab_channel=FREEDOMTECH

--

--

Elven Kim

I am a researcher in the field of Robotics, Computer Vision and Artificial Intelligence.