Ever wondered which dog breed you resemble? This fun web application uses a deep learning model to analyze a photo of your face and find your canine twin from a dataset of over 100 dog breeds!
This project is a hands-on introduction to building a full-stack machine learning application, covering everything from data processing and feature extraction to building a web interface with Flask.
- Simple Web Interface: Easy-to-use webpage for uploading your photo.
- AI-Powered Face Detection: Uses OpenCV to automatically detect and crop the human face from the uploaded image, ensuring more accurate comparisons.
- Deep Learning Feature Extraction: Employs a powerful pre-trained Convolutional Neural Network (VGG16) to understand the unique visual features of a face.
- Efficient Similarity Search: Compares facial features using Cosine Similarity to find the best match quickly.
The application's logic is broken down into a simple but powerful machine learning pipeline:
-
Data Pre-processing (One-time step): The
process_dataset.pyscript first runs through the entire Stanford Dogs Dataset. For each dog image, it performs a center-crop and uses the VGG16 model to extract a feature vector (a numerical representation of the image). All these vectors are saved to a single file,dog_features.pkl, for fast lookups later. -
User Upload: A user uploads their photo via the Flask web application.
-
Human Face Detection: The backend uses an OpenCV Haar Cascade classifier to find the user's face in the uploaded photo and crops the image to that region.
-
Feature Extraction: The cropped face image is then passed to the same VGG16 model to generate its unique feature vector.
-
Comparison: The application calculates the Cosine Similarity between the user's feature vector and every dog vector loaded from
dog_features.pkl. The dog with the highest similarity score (lowest cosine distance) is the winner! -
Display Results: The original photo and the winning dog photo are displayed to the user.
- Backend: Python, Flask
- Machine Learning: TensorFlow, Keras
- Computer Vision: OpenCV, Pillow
- Scientific Computing: NumPy, SciPy
- Frontend: HTML, CSS
Follow these steps to get the project running on your local machine.
- Python 3.8+
pip(Python package installer)
git clone https://github.com/jasperlawrencee/watda-dog-r-u.git
cd watda-dog-r-uIt's highly recommended to use a virtual environment to keep project dependencies isolated.
# For macOS/Linux
python3 -m venv venv
source venv/bin/activate
# For Windows
python -m venv venv
.\venv\Scripts\activateInstall all the required Python libraries from the requirements.txt file.
pip install -r requirements.txt-
Stanford Dogs Dataset:
- Download the dataset from Kaggle.
- Unzip the file and place the
images/Imagesfolder into the root of the project directory. Make sure the folder is namedImages.
-
Haar Cascade Classifier:
- Download the
haarcascade_frontalface_default.xmlfile from OpenCV's GitHub repository. - Place this file in the root of the project directory.
- Download the
This is a crucial one-time step. Run the processing script to generate the feature vectors for the entire dog dataset. This will create the dog_features.pkl file.
Note: This process can take 15-30 minutes depending on your computer's performance.
python process_dataset.pyOnce the setup is complete, you can start the Flask web server.
flask --app app runOpen your web browser and navigate to: http://127.0.0.1:5000
You should now see the application's homepage. Upload a photo and enjoy!
.
├── app.py # Main Flask application
├── feature_extractor.py # Core ML/CV functions (cropping, feature extraction)
├── process_dataset.py # Script to pre-process the dog dataset
├── requirements.txt # Project dependencies
├── haarcascade_frontalface_default.xml # OpenCV face detector
├── dog_features.pkl # Generated file of dog feature vectors
├── Images/ # Folder for the Stanford Dogs Dataset
│ └── n02085620-Chihuahua/
│ └── ...
├── templates/ # Flask HTML templates
│ ├── index.html
│ └── result.html
└── uploads/ # Stores user-uploaded images temporarily
- This project uses the Stanford Dogs Dataset.
- Feature extraction is powered by the VGG16 model, pre-trained on ImageNet.
- Face detection is performed using OpenCV.