
In this computer vision project tutorial, I will show you to make Motorcycle helmet detection using YOLOv8 deep learning model in Python and OpenCV.
For better explanation, I will break this entire motorcycle helmet detection project into some steps. Before we start, if you are new to computer vision I will suggest you to check out this Udemy course: Python for Computer Vision with OpenCV and Deep Learning.
Step1: Download Helmet Dataset
For this tutorial, I am going to use Bike Helmet Detection Computer Vision Project dataset from Roboflow. You can download this dataset from this link. It has 3735 total images of motorcycle and cycle helmets.
This dataset is available in different formats. Since I am going to use YOLO v8 deep learning model for this motorcycle helmet detection project, I am going to download this dataset in YOLOv8 format.

As you can see in the above image, this dataset comes with Train, Test, and validation split. You no need to do any further pre-processing. Just download and use it to train your YOLOv8 custom model.
Once you download this dataset as .zip format, change the name of this folder as per your convenience. I am giving this folder name as “Bike_Helmet_Dataset“.
After unzipping this training data, place it inside your working directory. So now the folder structure of your working directory should look like below:
Woking directory
├── Bike_Helmet_Dataset
├── train
| ├── images
| └── labels
├── test
| ├── images
| └── labels
├── valid
| ├── images
| └── labels
└── data.yaml
Note: data.yaml file is most important to train our custom YOLOv8 model. This file contains all the information about our training dataset. So please don’t mess with this file.
Step2: Setup YOLOv8 Environment
So we are going to use YOLOv8 deep learning model to train our motorcycle helmet detection model. To do that, first we need to configure our system environment according to that.
To set up entire your environment to train YOLO v8 model, please read this tutorial: Train YOLOv8 on Custom dataset in Windows GPU. It will take no more than 5 minutes to install all required libraries. It is always a good practice to create an isolated conda environment for this kind of project.
Step3: Train Helmet Detection Model
After configuring your virtual environment for YOLOv8, create a new Python file or open a Jupyter notebook inside your root working directory. In my case, I am creating a Jupyter Notebook. So now my folder structure looks like below:
Woking directory
├── helmet_detection.ipynb
├── datasets
├── train
| ├── images
| └── labels
├── test
| ├── images
| └── labels
├── valid
| ├── images
| └── labels
└── data.yaml
Now in your Jupyter notebook, first check whether all your configurations are correctly installed or not. To do that, run below Python code.
from IPython import display
display.clear_output()
import ultralytics
ultralytics.checks()
Ultralytics YOLOv8.0.145 Python-3.7.12 torch-1.13.1 CUDA:0 (NVIDIA GeForce GTX 1050 Ti, 4096MiB)
Setup complete (12 CPUs, 23.8 GB RAM, 108.6/117.9 GB disk)
As you can see in the output, ultralytics library is correctly installed and using GPU (CUDA) as its backend. So now we are ready to train our motorcycle helmet detection model using YOLOv8. Just run below Python code below to train your very own helmet detection model.
%%time
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
from ultralytics import YOLO, settings
# Training dataset path
dataset_path = 'C:/Computer Vision/Helmet Detection/Bike_Helmet_Dataset'
# Updata global dataset path in ultralytics settings.yaml file
settings.update({'datasets_dir': dataset_path})
# Path to training data.yaml
dataset_yaml = dataset_path+'/data.yaml'
# Load a model
model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
# Train the motorcycle helmet detection custom yolov8 model
model.train(data=dataset_yaml, epochs=40, imgsz=640, device=0, batch=8)
optimizer: AdamW(lr=0.001667, momentum=0.9) with parameter groups 57 weight(decay=0.0), 64 weight(decay=0.0005), 63 bias(decay=0.0)
Image sizes 640 train, 640 val
Using 8 dataloader workers
Logging results to runs\detect\train
Starting training for 40 epochs...
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
1/40 1.17G 1.866 2.367 1.587 8 640: 100%|██████████| 444/444 [02:06<00:00,
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 8/8 [00:01<0
all 126 299 0.567 0.503 0.509 0.163
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
2/40 1.13G 1.729 1.809 1.495 11 640: 100%|██████████| 444/444 [02:00<00:00,
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 8/8 [00:01<0
all 126 299 0.512 0.469 0.493 0.178
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
3/40 1.14G 1.718 1.679 1.493 9 640: 100%|██████████| 444/444 [01:59<00:00,
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 8/8 [00:01<0
all 126 299 0.585 0.602 0.616 0.256
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
4/40 1.16G 1.683 1.577 1.473 9 640: 100%|██████████| 444/444 [01:59<00:00,
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 8/8 [00:01<0
all 126 299 0.701 0.638 0.668 0.285
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
5/40 1.14G 1.672 1.49 1.464 3 640: 100%|██████████| 444/444 [01:59<00:00,
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 8/8 [00:01<0
all 126 299 0.597 0.545 0.556 0.198
.....
.....
.....
Here in the above code Line 3 & 4 is to avoid this error:
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results.
In line 9, I am defining my helmet detection dataset (Bike_Helmet_Dataset) folder path.
In line 12, I am updating global dataset path of ultralytics. To do that you need to change or update dataset path in settings.yaml. Otherwise, you may get below error while running YOLOv8 model for any other project or dataset:
RuntimeError: Dataset 'Bike_Helmet_Dataset/data.yaml' error
Dataset 'Bike_Helmet_Dataset/data.yaml' images not found , missing path 'C:\Computer Vision\Helmet Detection\Bike_Helmet_Dataset\valid\images'
Note dataset download directory is 'C:\Computer Vision\bottle_datasets'. You can update this in 'C:\Users\Anindya\AppData\Roaming\Ultralytics\settings.yaml'
This error occurred because, in my last project I configured YOLO v8 environment and trained bottle detection model. At that time a settings.yaml file generated (inside this directory: C:\Users\Anindya\AppData\Roaming\Ultralytics\settings.yaml). This settings.yaml
file looks like below:
settings_version: 0.0.4
datasets_dir: C:\Computer Vision\Helmet Detection\Bike_Helmet_Dataset
weights_dir: weights
runs_dir: runs
uuid: c4cec302062e239598045f77d3d082533c84e0ff2908ec99ef8362c428d76f76
sync: true
api_key: ''
clearml: true
comet: true
dvc: true
hub: true
mlflow: true
neptune: true
raytune: true
tensorboard: true
wandb: true
The root cause of this issue is this settings.yaml
file generated by Ultralytics. It gets created when you run YOLOv8 for the first time or updated last time and assumes that all the time you will run YOLOv8 model, your dataset folder should be placed at the same place as per the last project.
So to avoid this error, you need to modify datasets_dir
path in this settings.yaml
file. I tried changing this path manually, but it was not working. When I tried with this line of code (line 12), it works for me.
Rest of the code, I hope you can understand. If you have any issues understanding above model training code, I will suggest you to first read below listed tutorials:
- Train YOLOv8 on Custom dataset in Windows GPU
- Train YOLO Custom object detection model in Windows GPU
After successfully training your custom dataset with the YOLOv8 model, a ‘runs’ folder will be generated within your current working directory. The ‘best.pt’ file inside that folder, represents your final trained YOLOv8 model. So now your folder structure should looks like below:
Woking directory
├── runs
| └── detect
| └── train
| ├── Some statistical images
| └── weights
| ├── best.pt
| └── last.pt
|
├── datasets
| ├── train
| | ├── images
| | └── labels
| ├── test
| | ├── images
| | └── labels
| ├── valid
| | ├── images
| | └── labels
| └── data.yaml
└── helmet_detection.ipynb
Step4: Test on New Image
So we are ready with our trained motorcycle helmet detection deep learning model (YOLOv8). Let’s now test how our custom mode detects motorcycle helmets in a new image.
from ultralytics import YOLO
# Load our custom helmet detection deep learning model
model = YOLO("runs/detect/train/weights/best.pt")
# Use the model to detect object - Motorcycle helmet
model.predict(source="input_image.jpg", save=True, show=True)

You can find output image inside runs -> detect -> predict folder. As you can see in the above output, our model is correctly detecting motorcycle helmet (with helmet and without helmet) from our input image.
Realtime Helmet Detection
So we have seen that our model is correctly working for the input image, let’s now see how our custom motorcycle helmet detection deep learning model (YOLO v8) is performing on real-time video.
# For video
from ultralytics import YOLO
# Load custom trained YOLOv8 model
model = YOLO("runs/detect/train/weights/best.pt")
# Use the model to detect object
model.predict(source="test_video.mp4", show=True)
Improvements
In this tutorial, I just showed you the building blocks to make a realtime motorcycle or bike helmet detection system by training YOLOv8 deep learning model in Python. This type of project can be applied to traffic management systems.
You can take this project further by adding number plate detection to it. That means the end project work like a complete traffic and road management system where if a motorcycle rider is not wearing a helmet, automatically it will detect number plate of that motorcycle and file a case.
You can also use this project structure to detect safety helmets for construction workers. Also, I have used only 3735 training images. As you know more images you have, the better your machine learning model will perform. So try increasing your training dataset.
So this is it for this tutorial. Again if you are new to computer vision I will suggest you to check out this Udemy course: Python for Computer Vision with OpenCV and Deep Learning.
Similar Read:
- Train YOLOv8 on Custom dataset in Windows GPU
- Object Detection and More using YOLOv8
- Train YOLOv3 Custom object detection model in Windows GPU
- YOLO v3 object detection using deep learning OpenCV | Real-time
- Real-time Plastic Bottle Detection with Deep Learning & Python
- Realtime Number Plate Detection using Deep Learning
- Learn CNN from scratch with Python and Numpy

Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.