Real-time custom object detection using Tiny-YoloV3 and OpenCV.

Javeed Basha

2 min read

Computer vision technology of today is powered by deep learning convolutional neural networks. To detect objects, we can use many different algorithms like R-CNN, Faster RCNN, SSD, YOLO, etc. Here, I have chosen tiny-yoloV3 over others as it can detect objects faster without compromising the accuracy. We have a trained model that can detect objects in COCO dataset. But, how can we train to detect other custom objects?. We will see that in this post.

So, let us build a tiny-yoloV3 model to detect licence plates.

Project Structure:
├── custom_cfg
│   ├── darknet53.conv.74
│   ├── licence_plate.cfg
│   ├──
│   ├── licence_plate.names
│   ├── test.txt
│   └── train.txt
├── custom_dataset
│   └── (training images and object boundings)
│   └── (.weights files)


  1. prepare your dataset
  2. prepare config files
  3. train your model using darknet
  4. detect objects

1. Prepare your dataset

Your model is as good as the data you provide for training. You need to make sure you provide quality data. I have bulk downloaded google images of licence plates and stored in a folder, say ‘custom_dataset’. See how to bulk download (here).

Install LabelImg, graphical image annotation tool and label object bounding boxes in images. Once installed, clear contents in ‘LabelImg/data/predefined_classes.txt’.

Run labelImg and open the folder ‘custom_dataset’. Select the output as yolo and draw the bounding box over the object(Licence Plate). Set it as ‘licence_plate’ and save.

You can see a .txt file with the same name as your image is created in the same directory.
0 0.687109 0.379167 0.255469 0.158333
This is yolo format for storing bounding boxes. Do this for every image. I have done for about 200 images.

2. Prepare Config files

We will use Darknet neural network framework to train our model. darknet requires the following files.
1. train.txt
2. test.txt
3. .cfg file
4. .names file
5. .data file
6. darknet53.conv.74 (download here)

train.txt, test.txt:
We need to split our dataset for training and testing. Run to split into two files. Place in the same project directory and run.

.cfg file:
Copy yoloV3-tiny cfg from here. And make the following changes
1. change line no: 3 to ‘batch=24’, this means we will be using 24 images for every training step
2. change line no: 4 to ‘subdivisions=8’, the batch will be divided by 8 to decrease GPU requirements.
3. change in line no: 135,177 to ‘classes=1’ (this value denotes the number of object classes we have. In our case its only licence_plate)
4. change line no: 127,171 to filters=(classes+5)*3 in our case ‘filter=18’

.names file:
Create a .names file with a list of all class names that we want to train. In our case its only licence_plate


.data file:


Weights will be saved in the backup folder for every 100 iterations until 900, then saves for every 10,000.

3. Train your model using darknet

Now that we have our dataset and config files ready, we can now train the model using darknet in Google Colab. Check out my other tutorial on how to train your Tiny-YoloV3 model in Google Colab.

Once you got the .weights file you can proceed further

4. Detect objects

Copy Load weights and cfg appropriately and run the following respective command

python3 --image test.jpg
python3 --video test.mp4
python3 (for the webcam)

Happy detecting!!

Related posts:

One Reply to “Real-time custom object detection using Tiny-YoloV3 and OpenCV.”

Leave a Reply

Your email address will not be published. Required fields are marked *