Detect human faces from static images and live webcam feeds in real time, with bounding boxes and a live face count.
Face detection is a foundational building block of modern computer vision — powering everything from smart cameras to attendance systems. This project uses OpenCV's Haar Cascade Classifier, wired up in Java, to detect faces in two modes:
- 🖼️ Image mode — scan a static photo and mark every face found
- 🎥 Webcam mode — detect faces frame-by-frame from a live camera feed
Detected faces are outlined with green bounding boxes, and the total number of faces is shown live on screen.
- ✅ Face detection in static images
- ✅ Real-time face detection via webcam
- ✅ Bounding boxes drawn around every detected face
- ✅ Live on-screen face count
- ✅ Clean, modular Java implementation (easy to extend)
| Component | Details |
|---|---|
| Language | Java (JDK 11+) |
| Computer Vision | OpenCV 4.1.2 |
| Detection Model | Haar Cascade Classifier (haarcascade_frontalface_default.xml) |
| Build Tool | Maven |
| IDE | VS Code (or any Java IDE) |
| OS | Windows |
FaceDetection/
│
├── src/
│ ├── FaceDetectionImage.java # Detects faces in a static image
│ └── FaceDetectionWebcam.java # Detects faces from a live webcam feed
│
├── models/
│ └── haarcascade_frontalface_default.xml # Pre-trained Haar Cascade model
│
├── images/
│ └── test.jpg # Sample test image
│
├── pom.xml # Maven build configuration
├── run.bat # Windows script to build/run the project
└── README.md
- JDK 11 or higher — verify with:
java -version
- Maven installed and added to
PATH - OpenCV 4.1.2 native library (
opencv_java412.dll) if not resolved automatically via Maven — download from opencv.org/releases
# Clone the repository
git clone https://github.com/krushna1845/FaceDetection.git
cd FaceDetection
# Build the project with Maven
mvn clean installThe OpenCV dependency is pulled in automatically via
pom.xml. If you hit a native library (UnsatisfiedLinkError) issue on Windows, add the OpenCVbuild/java/x64folder to your systemPATHand restart your IDE/terminal.
run.batImage face detection:
javac -cp ".;lib/opencv-4120.jar" src/FaceDetectionImage.java
java -cp ".;lib/opencv-4120.jar;src" FaceDetectionImageWebcam face detection:
javac -cp ".;lib/opencv-4120.jar" src/FaceDetectionWebcam.java
java -cp ".;lib/opencv-4120.jar;src" FaceDetectionWebcamPress ESC to close the webcam window.
- OpenCV loads a frame — either from an image file or a webcam stream.
- The frame is converted to grayscale for faster, more accurate processing.
- The Haar Cascade Classifier scans the frame using pre-trained facial-feature patterns.
- Every detected face is outlined with a green rectangle.
- The live face count is calculated and rendered on screen in real time.
- Green bounding boxes drawn around every detected face
- Live counter overlay:
Faces Detected: 3
- 🔐 Security & surveillance systems
- 🕒 Attendance and access-control systems
- 🙋 Face-based authentication
- 🤖 Human–computer interaction
- Face recognition (identify who, not just detect a face)
- Eye detection
- Mask detection
- Automated attendance system using face recognition
- Emotion detection
Contributions, issues, and feature requests are welcome!
- Fork the repo
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Krushna Malode GitHub: @krushna1845
- OpenCV community & documentation
- Java documentation
This project is intended for educational purposes. Feel free to fork, learn from, and build on it.
If this project helped you, consider giving it a ⭐!