
Imagine, an AI-driven system identifying dents or damages from accidental or non-accidental cars using a camera and marking that area, how would it be? It may prove a game-changer for the automotive and insurance industries. In this tutorial, we will create such a system with the help of Python programming language. We will create a Python project using YOLOv8 and OpenCV that will detect car dents and other damages in images and live video feeds.
Not only will it detect, but it will also identify the type of damage and where, and mark that area. Whether youāre interested in car dent detection, car damage detection using YOLO, or exploring OpenCV with Python, this article will help you understand the core concepts and implementation.
Recommended Article: Creating a Bike Helmet Detection Project in Python using YOLO
Introduction to the Project
In this project, we will develop two Python programs. The first program detects car dents and damages in images, while the second one handles live video feeds. Both use a trained YOLO model with custom weights to identify specific types of car damage, such as dents on body panels, windscreen cracks, headlight damage, bonnet dents, boot dents, pillar damage, and much more.
Key technologies used in this project:
- YOLOv8: A state-of-the-art object detection framework optimized for accuracy and speed.
- OpenCV: An open-source library for image processing and computer vision tasks.
- Deep Learning: To train and utilize a model capable of detecting car damages efficiently.
Requirements & Installations
Before we start coding, letās ensure Python (3.6 or later) is installed on your computer. If you donāt have Python, you can download it for free fromĀ https://www.python.org/downloads/.
Now download all the dependencies we require using the following commands:
pip install gitpython>=3.1.30 pip install matplotlib>=3.3 pip install numpy>=1.23.5 pip install opencv-python>=4.1.1 pip install pillow>=10.3.0 pip install psutil pip install PyYAML>=5.3.1 pip install requests>=2.32.0 pip install scipy>=1.4.1 pip install thop>=0.1.1 pip install torch>=1.8.0 pip install torchvision>=0.9.0 pip install tqdm>=4.64.0 pip install ultralytics>=8.2.34 pip install pandas>=1.1.4 pip install seaborn>=0.11.0 pip install setuptools>=65.5.1 pip install filterpy pip install scikit-image pip install lap
Alternative Installation
Installing the above utilities one by one might be a boring task. Instead, you can download the ārequirements.txtā file containing all the dependencies above. Just run the following command. It will automate the whole task in one go.
pip install -r requirements.txt
Training of YOLO Model on Custom Dataset
At the very first, we have to train our YOLO model on the custom dataset. Please follow the steps below:
Download the Dataset
Download the car dent/damage dataset fromĀ roboflow.com.
Now unzip the downloaded dataset. The folder should look like the following:

Training YOLOv8 Model with Custom Dataset using Colab
OpenĀ Google Colab, sign in with your Gmail account, and open a new notebook.
Now go to the āRuntimeā menu, select āChange runtime typeā, choose āT4 GPUā for the Hardware accelerator, andĀ save it.
Letās check whether the GPU is running perfectly or not using the following command:
!nvidia-smi
The output should look like the following:

Next, installĀ ultralyticsĀ on yourĀ colabĀ workspace using the following command:
!pip install ultralytics
Now open yourĀ Google DriveĀ and navigate to āMy Drive.ā Now create a folder named āDatasetsā under āMy Driveā and inside the āDatasetsā folder create one more folder āCarDent.ā
Letās open the unzipped dataset folder, select all items present there, and drop them into the āCarDentā folder on Google Drive. It may take a while so wait until it is finished. The final āCarDentā folder will look like the following:

Now open the ādata.yamlā file in the text editor and modify the path variable to: ā../drive/MyDrive/Datasets/CarDentā. The final ādata.yamlā file will look like the following:

Now, letās go back to our Google Colab dashboard. You need to mount your Google Drive with the Colab. Insert the following command in a new cell and run it:
from google.colab import drive drive.mount('/content/drive')
You should get a success message like this: āDrive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(ā/content/driveā, force_remount=True).ā
Now we will start training our YOLO model with our car damage/scratches detection dataset. Again, create a new cell, insert the command below, and run it.
!yolo task=detect mode=train model=yolov8l.pt data=../content/drive/MyDrive/Datasets/CarDent/data.yaml epochs=50 imgsz=640
Here, āepochs=50ā specifies the number of training epochs. An epoch is one complete pass through the entire training dataset. Here, the model will be trained for 50 epochs.
āimgsz=640ā sets the size of the input images on which the model will be trained. In this case, images will be resized toĀ 640Ć640Ā pixels before being fed into the model.
The whole training can take around 1 ā 2 hours even more to complete.
After, the completion of the training go to the āFilesā section in your Colab dashboard and navigate through these folders: ārunsā -> ādetectā -> ātrainā -> āweightsā. Inside the āweightsā folder you will see ābest.ptā and ālast.ptā these two files. Download ābest.ptā from there.
Setting Up the Environment
For this project, create a folder named āCarDentDetectorā on your computer. Under this folder, create two more folders named āWeightsā and āMediaā to store pre-trained YOLO models and images, respectively.
Place the Downloaded YOLO Model
In the previous section, we trained our YOLO model with a custom car dent detection dataset and downloaded a file named ābest.pt.ā Now place that file inside the āWeightsā folder.
Media Files
I have collected suitable images from the Internet for this project and recorded real video footage of car dents. These media files will help you check the projectās execution.
You can get those using the āDownloadā button below. All you have to do is, download the zip file, unzip it, and place those images inside the āMediaā folder.
Create Your Python Script
Weāre almost at the end of setting up the environment. Now choose your favorite text editor and open the entire project folder āCarDentDetector.ā Inside this folder, create a Python program file named āCarDentDetector.py.ā This is where youāll write the code.
Your final project file hierarchy should look like the following:
CarDentDetector/ āāā Weights/ ā āāā best.pt āāā Media/ ā āāā dent_1.jpg ā āāā dent_2.jpg ā āāā dent_3.jpeg ā āāā dent_4.jpg ā āāā dent_5.jpeg ā āāā CarDent.mp4 āāā CarDentDetector.py āāā CarDentDetectorLive.py
The Program ā Car Dent Detection from Images
First, we will create a Python program to detect damages in a car only in images. Letās start writing your code step-by-step and try to understand the logic.
Import Libraries
First, we need to import the necessary libraries. Here, āOpenCVā is used for image processing, ācvzoneā helps draw bounding boxes, and āYOLOā from the āultralyticsā library is used for object detection.
import cv2 import math import cvzone from ultralytics import YOLO
Load YOLO Model and Define Class Names
Next, load the YOLO model with the custom-trained weights and define the class names that YOLO can detect. Make sure you have downloaded the ābest.ptā weights and placed them in the correct directory.
# Load YOLO model with custom weights yolo_model = YOLO("Weights/best.pt") # Define class names class_labels = ['Bodypanel-Dent', 'Front-Windscreen-Damage', 'Headlight-Damage', 'Rear-windscreen-Damage', 'RunningBoard-Dent', 'Sidemirror-Damage', 'Signlight-Damage', 'Taillight-Damage', 'bonnet-dent', 'boot-dent', 'doorouter-dent', 'fender-dent', 'front-bumper-dent', 'pillar-dent', 'quaterpanel-dent', 'rear-bumper-dent', 'roof-dent']
Load the Image
Now, load the image you want to process using OpenCVās āimreadā method.
# Load the image image_path = "Media/dent_1.jpg" img = cv2.imread(image_path)
Perform Object Detection
Now use āyolo_modelā to detect objects in the loaded image.
# Perform object detection results = yolo_model(img)
Draw Bounding Boxes and Labels
Now we will loop through the detected objects and draw bounding boxes around them. The confidence score and class label will also be displayed.
# Loop through the detections and draw bounding boxes for r in results: boxes = r.boxes for box in boxes: x1, y1, x2, y2 = box.xyxy[0] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) w, h = x2 - x1, y2 - y1 conf = math.ceil((box.conf[0] * 100)) / 100 cls = int(box.cls[0]) if conf > 0.3: cvzone.cornerRect(img, (x1, y1, w, h), t=2) cvzone.putTextRect(img, f'{class_labels[cls]} {conf}', (x1, y1 - 10), scale=0.8, thickness=1, colorR=(255, 0, 0))
Display the Image
Finally, we will display the processed image using OpenCVās āimshowā method. The window will close when the āqā button is pressed.
# Display the image with detections cv2.imshow("Image", img) # Close window when 'q' button is pressed while True: if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() cv2.waitKey(1)
Output
Output 1

Output 2

Output 3

Output 4

The Program ā Car Dent Detection in Videos (Real-Time)
In the previous section, we developed a Python program that detects car damage in images. Now, weāll explore a different program forĀ live car dent detection in a video.
This program closely resembles the previous one, but here, weāll use the ācv2.VideoCapture()ā function to capture video frames and a while loop to process them continuously.
Here is the program:
import cv2 import math import cvzone from ultralytics import YOLO # Initialize video capture video_path = "Media/cardent.mp4" cap = cv2.VideoCapture(video_path) # Load YOLO model with custom weights model = YOLO("Weights/best.pt") # Define class names classNames = ['Bodypanel-Dent', 'Front-Windscreen-Damage', 'Headlight-Damage', 'Rear-windscreen-Damage', 'RunningBoard-Dent', 'Sidemirror-Damage', 'Signlight-Damage', 'Taillight-Damage', 'bonnet-dent', 'boot-dent', 'doorouter-dent', 'fender-dent', 'front-bumper-dent', 'pillar-dent', 'quaterpanel-dent', 'rear-bumper-dent', 'roof-dent'] while True: success, img = cap.read() results = model(img, stream=True) for r in results: boxes = r.boxes for box in boxes: x1, y1, x2, y2 = box.xyxy[0] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) w, h = x2 - x1, y2 - y1 conf = math.ceil((box.conf[0] * 100)) / 100 cls = int(box.cls[0]) if conf > 0.4: cvzone.cornerRect(img, (x1, y1, w, h), t=2) cvzone.putTextRect(img, f'{classNames[cls]} {conf}', (max(0, x1), max(35, y1)), scale=1, thickness=1) cv2.imshow("Image", img) if cv2.waitKey(1) & 0xFF == ord('q'): break
Output
Summary
In this tutorial, we developed a computer vision project that detects car dents or damages using Python, a custom Yolov8 object detection model, and OpenCV. We explored two Python programs: one that detects car dents in a single image and another that performs real-time video detection. From setting up the environment to training a custom YOLO model with a car dent and scratches dataset, weāve covered each step in detail here.
This project not only highlights the power of computer vision and deep learning but also shows how these technologies can be applied to real-world problems, like identifying car damages accurately and efficiently.
Recommended Article: Detecting Potholes on Roads Using Python and YOLOv8
For any queries related to this project, reach out to me at contact@pyseek.com.
Happy Coding!