A computer vision application that automatically detects and counts the number of human faces in a group photo or selfie β built using deep learning-based object detection and deployed as a Flask web app.
Manually counting people in photos is slow, error-prone, and impossible to scale. This project tackles the People Detection and Head Counting challenge β a classical yet demanding computer vision problem with real-world applications across crowd management, event safety, and retail analytics.
Given any group photo or selfie, the system:
- Detects all human faces in the image automatically
- Counts the total number of faces with a bounding box drawn around each
- Returns the final count instantly via a web interface
This project was built as part of a hackathon computer vision challenge β testing skills in deep learning and object detection.
| Domain | Use Case |
|---|---|
| Event Management | Auto-count attendees from event photos |
| Retail Analytics | Count footfall in store snapshots |
| Security & Surveillance | Crowd density estimation in public spaces |
| Education | Automated attendance tracking from classroom photos |
| Social Media | Auto-tag count for group selfies |
| Public Safety | Crowd control β detect overcrowding at venues |
The system uses deep learning-based face detection to locate every face in an image, regardless of size, angle, or lighting conditions:
Input Image (Group Photo / Selfie)
β
Face Detection Model
(Deep Learning / OpenCV)
β
Bounding Boxes Drawn
Around Each Face
β
Total Face Count Returned
The key challenge in head counting is detecting small, partially occluded, or tilted faces in dense group photos β a much harder problem than single-face detection.
Face-Counting/
βββ static/
β βββ style.css # Flask frontend styling
βββ templates/
β βββ index.html # Web app interface
βββ uploads/ # Temporary storage for uploaded images
βββ Face Counting.ipynb # Development & experimentation notebook
βββ app.py # Flask application (main entry point)
βββ requirements.txt # Python dependencies
βββ Procfile # Heroku deployment config
βββ README.md
Step 1 β Image Input
- Accept image uploads via the Flask web interface
- Support common formats: JPG, JPEG, PNG
Step 2 β Preprocessing
- Resize image for optimal detection performance
- Convert to appropriate colour space (RGB/BGR as required by the model)
Step 3 β Face Detection
- Apply deep learning-based face detector to locate all faces
- Each detected face returns a bounding box
(x, y, width, height)with a confidence score - Filter detections by confidence threshold to reduce false positives
Step 4 β Annotation & Counting
- Draw bounding boxes around each detected face
- Count total number of valid detections
- Return annotated image + count to the user
| Approach | Method | Limitation |
|---|---|---|
| Traditional | Haar Cascades (Viola-Jones) | Poor on tilted, small, or occluded faces |
| Deep Learning β | CNN-based detector | Handles varied poses, sizes, lighting |
Deep learning-based detection significantly outperforms traditional Haar cascade methods, especially in dense group photos where faces may be small, partially covered, or at various angles.
| Category | Tools |
|---|---|
| Language | Python 3.7+ |
| Computer Vision | OpenCV |
| Deep Learning | DeepFace / CNN-based detection |
| Image Processing | PIL / Pillow, NumPy |
| Web Framework | Flask |
| Frontend | HTML, CSS |
| Deployment | Heroku β Render |
| Notebook | Jupyter Notebook |
# Clone the repository
git clone https://github.com/vicky60629/Face-Counting.git
cd Face-Counting
# Install dependencies
pip install -r requirements.txt
# Run the Flask app
python app.pyOpen http://localhost:5000 in your browser.
- Open the web app in your browser
- Upload a group photo or selfie (JPG/PNG)
- Click "Count Faces"
- View the annotated image with bounding boxes drawn around every detected face
- See the total face count displayed instantly
Example:
| Input | Output |
|---|---|
| Group selfie with 8 people | β 8 faces detected β bounding boxes drawn on each |
| Crowd photo with partial faces | β Detects even partially visible faces |
| Challenge | Solution |
|---|---|
| Small faces in large group photos | Deep learning model trained on varied scales |
| Partially occluded faces | CNN features capture partial face patterns |
| Varied lighting conditions | Preprocessing normalisation before detection |
| Multiple overlapping faces | Non-Maximum Suppression (NMS) to avoid double-counting |
| Different face angles / poses | Model trained on diverse multi-angle datasets |
What worked well:
- Deep learning-based detection significantly outperforms Haar cascades on group photos
- Flask made it easy to wrap the CV pipeline into an accessible web app
- Confidence threshold tuning was critical β too low causes false positives, too high misses small faces
Future enhancements:
- Add real-time video stream face counting via webcam
- Integrate YOLOv8 for faster, more accurate detection
- Build a crowd density heatmap overlay to show where faces are clustered
- Add age & gender estimation per detected face using DeepFace
- Deploy on Render with a permanent live demo link
- Extend to multi-frame video β count unique individuals across frames (de-duplicate repeated faces)
- Add emotion detection overlay on each bounding box
Face counting sits at the intersection of two active research areas:
Object Detection β locating and bounding instances of objects (here: faces) within images. Modern detectors like YOLO and RetinaNet achieve near-human accuracy on standard benchmarks.
Crowd Counting β estimating the number of people in dense scenes. Goes beyond simple detection β at extreme densities, individual face detection fails and density map estimation is used instead (e.g., CSRNet, MCNN). This project's approach works well for small-to-medium group sizes (2β50 people).
Vicky Gupta β Data Engineering Analyst @ Accenture (4.5 years) | Aspiring Data Scientist
Passionate about computer vision, NLP, and building end-to-end ML systems. Experienced in PySpark, ETL pipelines, and deploying ML models from notebook to production.
π§ vg60629@gmail.com
This project is licensed under the MIT License β see the LICENSE file for details.
β Found this useful? Star the repository to help others discover it!