Building a Dog Breed Prediction App with Django and TensorFlow

Pankaj Kushwaha
4 min read3 days ago

--

Have you ever looked at a dog and wondered, “What breed is that?” Imagine an app that can help you identify dog breeds with a simple image upload. I created exactly that: a Dog Breed Prediction App powered by machine learning and built with Django and TensorFlow.

In this article, I’ll share my experience building this app, from data handling and model training to implementing a user-friendly interface. Let’s dive into how each part of this project came together.

Project Overview

The Dog Breed Prediction App is designed to identify the breed of a dog from an uploaded image. The core functionality relies on a convolutional neural network (CNN) model trained to classify a variety of popular dog breeds. The model is integrated into a Django web application, creating an accessible tool where users can easily upload a dog image and get an instant prediction.

Key Features

  • Image Upload and Preview: Users can upload and preview images before submission.
  • Breed Prediction: The app returns the breed name with a displayed image.
  • Clear Functionality: Allows users to reset the form and try a new image.
  • Responsive UI: Built with Bootstrap for a seamless experience on any device.

Technologies Used

The project leverages some of the best tools in web development and machine learning:

  • Django for backend structure and routing.
  • TensorFlow/Keras for the machine learning model.
  • OpenCV for image processing.
  • Bootstrap for responsive design and user interface styling.

How I Built It

1. Training the Machine Learning Model

The model training process was one of the most exciting parts of this project. I trained a CNN model using TensorFlow/Keras, which I chose for its effectiveness with image data. The model architecture consisted of convolutional layers to capture image features and pooling layers to reduce dimensions.

Using a dataset of labeled dog images, I tuned hyperparameters, adjusted the architecture, and ran multiple training sessions to get optimal results. The final model was saved as an .h5 file, which allowed me to easily load it into the Django application for real-time predictions.

2. Building the Backend with Django

Django served as the backbone of this project. I created an app in Django to handle image uploads, pass images through OpenCV for preprocessing, and make predictions using the trained model.

Here’s a simplified look at how the backend handles an uploaded image:

def preprocess_image(opencv_image):
opencv_image = cv2.resize(opencv_image, (224, 224)) # Resizing for model input
opencv_image = np.expand_dims(opencv_image, axis=0) # Adding batch dimension
opencv_image /= 255.0 # Normalize to [0, 1]
return opencv_image

After preprocessing, the app uses the loaded model to predict the breed with the highest probability and sends the result back to the frontend.

3. Designing the Frontend with Bootstrap

A key priority for this project was to make the interface intuitive and visually appealing. I used Bootstrap to create a layout that’s both clean and responsive. Key elements include:

  • Image Upload Button: Designed to let users easily upload a dog image.
  • Prediction Result Card: Displays the predicted breed and the processed image in a clean and attractive format.
  • Clear Button: Allows users to reset the form and try another image.

Here’s a snippet of the prediction result section:

<div class="card p-3" style="background-color: #f5f5f5; border-radius: 12px;">
<p class="fw-bold" style="font-size: 1.25rem; color: #6c5ce7;">
The Dog Breed is <span id="breedName">...</span>
</p>
</div>

4. Adding Interactivity with JavaScript

JavaScript was essential for adding interactive elements. For example, I used JavaScript to handle image previews before the user submits, and I also added a loading spinner to display during predictions.

Here’s how the image preview feature works:

const imageInput = document.getElementById("imageInput");
const previewImage = document.getElementById("previewImage");
imageInput.addEventListener("change", function () {
const file = imageInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
previewImage.src = e.target.result;
previewArea.style.display = "block";
};
reader.readAsDataURL(file);
}
});

5. Testing and Iterating

After implementing the core functionality, I extensively tested the app with various dog images. Fine-tuning and iterating were necessary to ensure a smooth user experience and accurate breed predictions.

Challenges and How I Solved Them

Image Preprocessing

Using OpenCV for image preprocessing in a web environment presented unique challenges. I needed to ensure that the images were resized and normalized correctly to match the model’s requirements. OpenCV’s resizing and normalizing functions worked well once the parameters were calibrated.

Handling Large Models

Model loading can slow down web applications, especially with large models. To address this, I saved the model in .h5 format, which streamlined the loading process in Django.

Model Accuracy

Ensuring the model was accurate enough for real-world use took some work. I adjusted the CNN architecture and experimented with various datasets to achieve the best accuracy possible.

What I Learned

Building the Dog Breed Prediction App was a fulfilling learning experience. It reinforced my skills in deploying machine learning models to a web application and gave me practical experience with Django and OpenCV. I also gained a deeper appreciation for the frontend, especially when creating interactive and user-friendly applications.

For anyone interested in web-based machine learning projects, this app is a great starting point. It combines the power of Django and TensorFlow and demonstrates how machine learning can be applied in fun, practical ways.

Future Enhancements

There’s always room to improve! Here are some ideas for future work:

  • Expand the Dataset: Adding more dog breeds to the dataset could increase the model’s utility.
  • Mobile-Friendly Enhancements: While the app is responsive, further optimizations could enhance the mobile experience.
  • Real-Time Predictions: Incorporating real-time video feed predictions would be an exciting addition.

Check Out the Project on GitHub

The full code for this project is available on GitHub: Dog Breed Prediction App Repository

--

--

Pankaj Kushwaha
0 Followers

On a mission to build Farm Assistance