An Object Detection Application using YOLOv3 (PyTorch and Django Implementation). Django implementation for Webpage as well as REST API
A web application that provides object detection using YOLOv3 and also generates REST API. It's implemented using Django framework and PyTorch (for YOLO model). I have developed Django API which accepts an image as request. The input image is converted to float32 type NumPy array and passed on to the YOLOv3 object detection model. The model performs object detection for the image and generates a JSON object with names of all the objects and their respective frequency in the image. The Response of the API is the JSON object.
The libraries required along with thier version are mentioned below:
- Python (3.7)
- Django (3.0.3)
- PyTorch (1.3.1)
- Pillow (7.1.2)
- OpenCV (4.2.0)
- NumPy (1.18.5)
For object detection using Pre-Trained model, we need three important files :
- yolov3.cfg - The cfg file describes the layout of the network, block by block. The official cfg file is available in darknet github repository. However, I have made few changes in the configuration file in order to get better performance.
- yolov3.weights - We use weights from the darknet53 model.
- coco.names - The coco.names file contains the names of the different objects that our model has been trained to identify.
This repository can do two things:
- Implementation web-application
- Generation of REST API (API testing is done using POSTMAN)
-
Clone the GitHub repository
-
Change the directory to the cloned Repository folder.
-
The .cfg and coco.names files are already available in this repository. Now, all we need to do is download the weights file.
Download yolov3.weights using the following command in your Command Prompt:
wget https://pjreddie.com/media/files/yolov3.weights
-
Install all the required libraries.
-
Execute the code below: (This command needs to be executed only once)
python manage.py collectstatic
This command initiates Django and collects all the static files.
- Then, Execute:
python manage.py runserver
This command starts Django server. Now we are all set to run the application.
- After executing the above code, you will see something like this:
-
Click on the link. This will direct you to the web browser.
-
Select an image via Drag-&-Drop or Browse mode.
- Click on ”Detect Object”
- Input to Django Web-app is an image. This input image is converted to float32 type NumPy array and passed on to the YOLOv3 model. The model performs object detection for the image and generates a JSON object with names of all the objects and their respective frequency in the image.
-
The Form Response is the JSON object. This JSON object is displayed as shown above.
-
Now, click on “Show Predictions” to see the image with Bounding Boxes.
- To try on other images, click on “Choose a New File”.
Postman is a scalable API testing tool. The steps to be followed are:
-
Follow the first 6 steps as mentioned above.
-
Make sure the server runs properly
- Open POSTMAN and select POST option. Enter the server link shown above and append /object_detection/api_request/ to it.
For Example : 127.0.0.1:8000/object_detection/api_request/
-
Click on Body. Enter key value as “Image”. Choose the image file and click on “SEND”.
-
The input image is converted to float32 type NumPy array and passed on to the YOLOv3 model. The model performs object detection for the image and generates a JSON object with names of all the objects and their respective frequency in the image.
- The HttpResponse is the JSON object. This JSON object is displayed as shown above.