
You only look once (YOLO) is a state-of-the-art, real-time Object Detection system. The latest version is YOLOV5. These YOLO models are well known for speed. They can detect multiple objects in video frames in a fraction of seconds. The inference time of YOLO model with compare to the other models like EfficientDet and RetinaNet is better. YOLO is known for its speed for detecting the objects in image/video. If your usecase is more concern about real time detection of multiple objects then YOLO is the most suitable. Now, We have YOLO V5 which has around 476 FPS in its small version of the model.
In this article, I will walk you through the steps to deploy your own custom YOLO model in localhost.
Let’s begin then
Here, I will walk through all the other steps in brief as our agenda is to deploy the model and use it on our website.
- Grab images for labeling: It is the first step. Here, you can go to google and search for the pictures you want to build your custom for. Or, you can go around and click photos of objects and gather them for the model. That is your choice how better you want your model to be.
- Label your images. Give a fair amount of data for this step, as it is essential for your accuracy. You can use any tools for labeling your data. There is no automatic approach to label your custom data. You have to do it manually. And this is the most frustrating and time-consuming part of the object detention, but surely you will get a fruitful result if you give your dedication to this part. I have used the "LabelImg" tool for labeling. To know more about how the "LabelImg" tool works, please follow this article How to Label image for object detection.
- Select object detection algorithm Here, we will be using YOLO-tiny. And, you can refer the custom object detection code here. https://github.com/pranjalAI/Yolo-tiny-insurance. There are many other approaches for object detection like SSD, Yolo etc.
- After getting your Yolo weight file from step 3, You need to convert the Yolo generated darknet model .weight file into TensorFlow serving so that we can serve it using TensorFlow to our webpage.
So, if you have reached till here. Cheers!! You have done a great job. You are ready to move your model to production.

To convert .weight file which is of darknet format to TensorFlow format, you need to follow the below steps.
Darknet to Tensorflow
Clone this directory https://github.com/pranjalAI/tensorflow-yolov4-tflite
Enter into the directory using command prompt.

Now, You need to install some dependencies/libraries.
Installing Libraries
If you have anaconda installed, then you need to follow this to install all the required libraries in an environment. This will create a new environment. Environment name can be changed in the .yml file.
# Tensorflow CPU
conda env create -f conda-cpu.yml
conda activate yolov4-cpu
# Tensorflow GPU
conda env create -f conda-gpu.yml
conda activate yolov4-gpu
If you don’t have conda installed, then follow this using pip command. I would suggest to use conda.
# TensorFlow CPU
pip install -r requirements.txt
# TensorFlow GPU
pip install -r requirements-gpu.txt
So, The new environment with respective dependencies got created. Now, to use the newly created environment you need to activate it.(This step is for those who are using conda to creating environment.)

Activating Environment
conda activate yolov4-gpu
here "yolov4-gpu" is environment name. You can change it in the .yml file. (This step is for those who are using conda to creating environment.) If you are using pip then directly proceed for the next step.
Now, if you are into the folder that you just cloned and also you have activated the environment, finally, you need to make two changes in your cloned folder.
First, Copy and paste your custom .weights file which you used for Your Yolo model training, into the ‘data’ folder and copy and paste your custom .names into the ‘data/classes/’ folder.
Second, The only change within the code you need to make for your custom model to work is on line 14 of ‘core/config.py’ file. Update the code to point at your custom .names file as seen below. (my custom .names file is called custom.names but yours might be named differently)

Note: If you are using the pre-trained yolov4 then make sure that line 14 remains coco.names.
Here we go!! Now, we need a single line of command to make code TensorFlow compatible
python save_model.py –weights ./data/your_weight_file.weights – output ./checkpoints/yolo-tiny-416 – input_size 416 – model yolov4
Here, 416 is the image size you defined in your config file while training yolo-tiny. After running the above command you will get a .pb file

Now, you need to give this folder location while detecting the objects. Now, take a cup of tea. Search for any HTML template you can use. We can discuss some HTML things here but, we will do it next time. Here, I assume that you have a demo web page ready.

Using flask to server TensorFlow model
So, Now we have our TensorFlow served model available. Now, We need to serve it using flask to our webpage.
Now, paste this final object detection and cropping code in your same root folder where you get your TensorFlow served model (Check above steps).
Now, your flask app will call it to detect image.
Final demo
So, Congrats! You just deployed your first custom object detection model over localhost using Flask. Try with your custom objects and comment down custom object detection which model you have deployed.
Before you go…
If you liked this article and want to stay tuned with more exciting articles on Python & Data Science – do consider becoming a medium member by clicking here https://pranjalai.medium.com/membership.
Please do consider signing up using my referral link. In this way, the portion of the membership fee goes to me, which motivates me to write more exciting stuff on Python and Data Science.
Also, feel free to subscribe to my free newsletter: Pranjal’s Newsletter.