![]() |
VOOZH | about |
In this project, we will be integrating a deep learning model with the Django framework. This deep learning model converts greyscale images to colorful images.
We will store the user-given greyscale image in our database and then resize the image, submit this resized image, to our DL model, and then provide the user with a token. This token can be used to download the colorful image.
Note :
- This project implements the simplest frame/skeleton to integrate a deep learning model with the Django backend.
- This project receives an image input, submits that image to the model for processing and then returns a download token to download the processed image.
Table of Content
Step 1: Create a directory named as myDL_project.
Step 2: Open that directory in Vscode.
Step 3: Create a virtual environment. We will be using pipenv to create a virtual environment.
pipenv shellStep 4: Install Django, opencv and pillow required for this project by using command:
pipenv install django opencv pillowStep 5: Start a project in django by using command:
django-admin startproject DLWebAppStep 6: Create a new app home inside the project director (myDL_project/DLWebApp). It will handle the landing page and upload/download form for image.
cd DLWebApp
python manage.py startapp home
Step 7 : Register our new home app in the project settings.
In DLWebApp/DLWebApp/settings.py
Step 8: Create a template directory in the parent directory, at the same level as our project and app directories (DLWebApp/templates).
Setup the templates path in settings.py in the project directory.
In DLWebApp/templates/base.html
Create a upload_form.html in templates directory. This form will accept greyscale image from user.
Create a download_form.html in templates directory. This form will accept token for colored image from user.
Create an index view in home app to handle the loading of upload_form page.
In DLWebApp/home /views.py
We will add logic to handle image processing later.
In DLWebApp/home /urls.py
Include home.urls. in projects urls.py.
In DLWebApp/DLWebApp/urls.py
Run the development server using command:
python manage.py runserverCreate a model which can store greyscale image and its id. Create another model which can store colored image and its id. Create one more model which can store resized image.(DLWebApp/home/models.py)
Run migration commands to migrate these models into the data base.
python manage.py makemigrations
python manage.py migrate
Design a view logic which will handle the greyscale post event.
In DLWebApp/home/views.py
Inside upload.html to display these signature and download tokens as alert messages.
Step 1: In settings.py file of the project update the media file root, url and startic_url.
In DLWebApp/DLWebApp /settings.py
Step 2 : Add url for media directory in the projects urls.py.
Step 3: Upload an image through the form and check if it is saved in the DLWebApp/media/grayscale_images directory.
Deep Learning Model :
- The deep learning model is converting a grey image to colorized image.
- We have used Caffe framework to develop my model so .caffemodel file contains the weight of my model. You can also use .h5 format to store your model weights.
- Integration of a deep learning model and a machine learning model with Django follows different procedures.
Step 1 : Create a DL directory in same level as the project and home app.
Step 2 : Upload all model files to this directory. We are the Caffe framework for model training, so the model weights are saved in .caffemodel files, and the network architecture is defined in .prototxt. Additionally, we may have some data saved in .npy format.
Step 3 : Import all the dependencies we will need to handle image processing and your model. Additionally import os module.
In DLWebApp/home/views.py
Step 4: Load the model
In DLWebApp/home/views.py
Step 5 : Complete the colorize_image function which converts the resized grey image to colorful image.
In DLWebApp/home/views.py
Step 6 : Upload a greyscale image and check if our backend is producing a colourful image in media/colorized_images directory.
We should receive alerts containing the token for signature and download.
In DLWebApp/home/views.py
Step 1 : Create a Download class in home/views.py which will render download_form.html for get request.
Step 2 : Create POST method in Download class to handle the form submission. This method will search and return a colorized image for the provided token.
URL setup: Add path to post the token to Download class.
In DLWebApp/home/urls.py
Step 3 : Add the URL of download class to the action of the download_form.html .
Step 4 : Create a download link in the upload form which will redirect user to the download form.
In DLWebApp/templates/upload_form.html
Output:
Step 5 : Upload a grey scale image to the upload form. It will return you two tokens signature and download. Copy download token and go to download form, paste the download token and you will receive a colored image.