Commit ยท
fead00d
0
Parent(s):
Initial commit for Face Counter & Density Estimator app
Browse files
README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
๐ฏ Project Title: Face Counting and Crowd Density Estimation using MTCNN
|
| 2 |
+
๐ Overview
|
| 3 |
+
This project addresses the problem of estimating the number of faces in an image, with the added goal of classifying crowd density as Sparse, Medium, or Dense. Initially approached as a regression task, we shifted strategies after empirical evaluation showed that using ground-truth bounding box annotations from the WIDER FACE dataset provided more reliable face counts than a trained regressor. We ultimately adopted the MTCNN (Multi-task Cascaded Convolutional Networks) approach for real-time face detection due to its robustness, accuracy, and speed in varying crowd scenarios.
|
| 4 |
+
|
| 5 |
+
๐ง Why MTCNN?
|
| 6 |
+
MTCNN is a popular face detection framework because:
|
| 7 |
+
|
| 8 |
+
It combines face detection and facial landmark localization, making it suitable for fine-grained analysis.
|
| 9 |
+
|
| 10 |
+
It performs well across scales, poses, and lighting conditions.
|
| 11 |
+
|
| 12 |
+
It runs in real-time, enabling its use in live applications like webcam feeds.
|
| 13 |
+
|
| 14 |
+
It's pretrained and optimized, saving time and training resources.
|
| 15 |
+
|
| 16 |
+
๐ก How It Works
|
| 17 |
+
A user uploads an image or uses their webcam to capture one.
|
| 18 |
+
|
| 19 |
+
The app runs MTCNN to detect faces and count bounding boxes.
|
| 20 |
+
|
| 21 |
+
Based on the count:
|
| 22 |
+
|
| 23 |
+
Sparse: 1โ10 faces
|
| 24 |
+
|
| 25 |
+
Medium: 11โ50 faces
|
| 26 |
+
|
| 27 |
+
Dense: 51+ faces
|
| 28 |
+
|
| 29 |
+
Results are overlaid directly on the image, offering a visual interpretation of the density.
|
| 30 |
+
|
| 31 |
+
๐ ๏ธ Tech Stack
|
| 32 |
+
Model: facenet-pytorch MTCNN
|
| 33 |
+
|
| 34 |
+
Dataset: WIDER FACE (for validation and benchmarking)
|
| 35 |
+
|
| 36 |
+
Framework: Python, Streamlit
|
| 37 |
+
|
| 38 |
+
Libraries: OpenCV, PIL, Torch, Matplotlib
|
| 39 |
+
|
| 40 |
+
Deployment: Streamlit (local or cloud-hosted)
|
| 41 |
+
|
| 42 |
+
๐ Real-Life Applications
|
| 43 |
+
Surveillance & Public Safety
|
| 44 |
+
Detect unusually dense crowds in public areas to trigger alerts.
|
| 45 |
+
|
| 46 |
+
Event Management
|
| 47 |
+
Monitor real-time foot traffic and optimize crowd control in concerts, rallies, etc.
|
| 48 |
+
|
| 49 |
+
Retail Analytics
|
| 50 |
+
Gauge customer distribution across zones in malls or stores.
|
| 51 |
+
|
| 52 |
+
Transportation Hubs
|
| 53 |
+
Analyze crowd density in airports or stations to deploy personnel dynamically.
|
| 54 |
+
|
| 55 |
+
Smart Cities
|
| 56 |
+
Integrated with CCTV systems, the app can be part of intelligent urban monitoring.
|
| 57 |
+
|
| 58 |
+
Pandemic Safety Enforcement
|
| 59 |
+
Identify when crowd limits are exceeded to enforce health protocols.
|
| 60 |
+
|
| 61 |
+
โ
Impact
|
| 62 |
+
This project demonstrates how a lightweight, pretrained architecture like MTCNN can replace heavyweight regression-based models when data annotation is reliable. It bridges computer vision with social and safety applications, showing how academic tools can translate into real-world solutions.
|
| 63 |
+
|
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
from facenet_pytorch import MTCNN
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load MTCNN model
|
| 7 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 8 |
+
mtcnn = MTCNN(keep_all=True, device=device)
|
| 9 |
+
|
| 10 |
+
# Face detection and density classification function
|
| 11 |
+
def detect_faces(image):
|
| 12 |
+
if image is None:
|
| 13 |
+
return None, "No image provided.", "No density calculated."
|
| 14 |
+
|
| 15 |
+
# Detect faces
|
| 16 |
+
boxes, _ = mtcnn.detect(image)
|
| 17 |
+
face_count = 0 if boxes is None else len(boxes)
|
| 18 |
+
|
| 19 |
+
# Classify density
|
| 20 |
+
if face_count <= 10:
|
| 21 |
+
density = "๐ข Sparse"
|
| 22 |
+
elif face_count <= 50:
|
| 23 |
+
density = "๐ก Medium"
|
| 24 |
+
else:
|
| 25 |
+
density = "๐ด Dense"
|
| 26 |
+
|
| 27 |
+
# Annotate image
|
| 28 |
+
annotated = image.copy()
|
| 29 |
+
draw = ImageDraw.Draw(annotated)
|
| 30 |
+
if boxes is not None:
|
| 31 |
+
for box in boxes:
|
| 32 |
+
draw.rectangle(box.tolist(), outline="red", width=3)
|
| 33 |
+
|
| 34 |
+
# Return annotated image and stats
|
| 35 |
+
return annotated, f"๐งฎ Face Count: {face_count}", f"๐ Crowd Density: {density}"
|
| 36 |
+
|
| 37 |
+
# Gradio UI
|
| 38 |
+
title = "๐ฏ Face Counter & Density Estimator"
|
| 39 |
+
description = """
|
| 40 |
+
Upload an image or use your webcam to detect faces and estimate crowd density.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=detect_faces,
|
| 45 |
+
inputs=gr.Image(sources=["upload", "webcam"], type="pil", label="Upload Image or use Webcam"),
|
| 46 |
+
outputs=[
|
| 47 |
+
gr.Image(type="pil", label="Detected Faces"),
|
| 48 |
+
gr.Textbox(label="Face Count"),
|
| 49 |
+
gr.Textbox(label="Crowd Density"),
|
| 50 |
+
],
|
| 51 |
+
title=title,
|
| 52 |
+
description=description,
|
| 53 |
+
allow_flagging="never",
|
| 54 |
+
theme="soft"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
iface.launch()
|
requirement.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
facenet-pytorch
|
| 4 |
+
Pillow
|
utils.py
ADDED
|
File without changes
|