Skip to content

Ph0enix22/cat-detector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cat Face Detector ฅ⁠^β β€’β ο»Œβ β€’β ^⁠ฅ

A beginner-friendly computer vision project that detects cat faces in images and videos using OpenCV and Haar Cascades.

Python OpenCV License

πŸ“‹ Table of Contents

🎯 Overview

This project demonstrates real-time cat face detection using computer vision techniques. Built as a beginner-friendly introduction to OpenCV, it can detect cat faces in:

  • Static images
  • Video files
  • Live webcam feeds

Perfect for learning the fundamentals of object detection and creating fun content with your pets!

✨ Features

  • βœ… Real-time detection in video streams
  • βœ… Batch processing of images and videos
  • βœ… Webcam support for live demonstrations
  • βœ… Automatic video saving with detections
  • βœ… Detection statistics (frames processed, cats detected)
  • βœ… Easy-to-use command-line interface

πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager

Step 1: Clone the Repository

git clone https://github.com/Ph0enix22/cat-detector.git
cd cat-detector

Step 2: Create Virtual Environment (Recommended)

# Windows
python -m venv venv
venv\Scripts\activate

# Mac/Linux
python3 -m venv venv
source venv/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Download Haar Cascade File

Download the haarcascade_frontalcatface.xml file from the OpenCV GitHub repository and place it in the project root directory.

πŸ’» Usage

Detect Cats in an Image

python cat_detector.py --input path/to/cat_image.jpg --mode image

Detect Cats in a Video

python cat_detector.py --input path/to/cat_video.mp4 --mode video

Use Webcam for Live Detection

python cat_detector.py --input 0 --mode video

Advanced Options

# Custom output directory
python cat_detector.py --input video.mp4 --mode video --output my_outputs

# Use different cascade file
python cat_detector.py --input video.mp4 --cascade custom_cascade.xml

# Preview only (don't save output)
python cat_detector.py --input 0 --mode video --no-save

All Command-Line Arguments

Argument Short Description Default
--input -i Input image/video path, or 0 for webcam Required
--mode -m Detection mode: 'image' or 'video' video
--cascade -c Path to Haar cascade XML file haarcascade_frontalcatface.xml
--output -o Output directory outputs
--no-save - Don't save output (preview only) False

πŸ“ Project Structure

cat-detector/
β”‚
β”œβ”€β”€ cat_detector.py                 # Main detection script
β”œβ”€β”€ haarcascade_frontalcatface.xml  # Haar cascade model
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ README.md                       # Project documentation
β”œβ”€β”€ .gitignore                      # Git ignore rules
β”œβ”€β”€ LICENSE                         # Project license
β”‚
β”œβ”€β”€ videos/                      # Input videos (cat footage)
β”‚   β”œβ”€β”€ cat_video1.mp4
β”‚   └── kitten_video1.mp4
β”‚
β”œβ”€β”€ outputs/                     # Detected outputs (images + videos)
β”‚   β”œβ”€β”€ detected_20251227_233739.mp4
β”‚   └── detected_cat1.jpeg
β”‚
└── screenshots/                 # Original cat images before detection
β”‚   β”œβ”€β”€ cat1.jpeg
β”‚   └── cat2.jpeg
└── demo/                           # Demo assets (GIFs, live detection screenshots, screen recordings)
    β”œβ”€β”€ demo_detection.gif
    └── demo_recording.mp4

πŸ” How It Works

1. Haar Cascade Classifiers

This project uses Haar Cascade Classifiers, a machine learning-based approach for object detection:

  • Pre-trained on thousands of positive (cat faces) and negative (non-cat) images
  • Uses multiple stages of weak classifiers to identify features
  • Fast and efficient for real-time detection
  • Works best with frontal face views

2. Detection Pipeline

Input (Image/Video)
    ↓
Convert to Grayscale
    ↓
Apply Haar Cascade Detector
    ↓
Find Regions of Interest (ROI)
    ↓
Draw Bounding Boxes
    ↓
Output Annotated Image/Video

3. Key Parameters

  • scaleFactor (1.3): How much the image size is reduced at each scale
  • minNeighbors (10): How many neighbors each candidate rectangle should have
  • minSize (75x75): Minimum object size to detect

πŸ“Š Results

Detection Statistics (Example)

Total frames processed: 450
Total cat detections: 412
Average detections per frame: 0.92
Detection accuracy: ~90% on frontal faces

Performance

  • Processing Speed: ~30 FPS on modern laptops
  • Best Results: Well-lit environments with frontal face views
  • Limitations: Reduced accuracy with side profiles or poor lighting

πŸŽ₯ Demo

Live Webcam Detection (GIF)

Webcam Demo

Image Detection

Detected Image

🚧 Future Improvements

Short-term

  • Add size-based classification (cat vs kitten)
  • Implement confidence scores
  • Create a simple GUI with Tkinter
  • Add support for multiple cascade types

Long-term

  • Upgrade to YOLO for better accuracy
  • Train custom model on specific pets
  • Deploy as web app with Streamlit
  • Add pet activity tracking over time

🀝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • OpenCV for the computer vision library
  • OpenCV Haar Cascade models for the pre-trained classifier
  • My cats, for patiently (and not so patiently) posing for the camera
  • My siblings, for helping with recordings, testing, and feedback

πŸ¦β€πŸ”₯ Author

SMJ


⭐ If you found this project helpful, please consider giving it a star!


πŸŽ“ Learning Resources

New to computer vision? Check these out:


Made with ❀️ by SMJ (for beginners in computer vision)