A simple but powerful Java project that performs face detection using OpenCV. The program scans an image, detects human faces using a pretrained Haar Cascade model, and highlights them using rectangles.
Java • openCV • GitHub •ideaL
This project demonstrates:
- Reading images in Java using OpenCV
- Detecting faces using Haar Cascade Model (
haarcascade_frontalface_default.xml) - Drawing rectangles around detected faces
- Saving the output image with detected faces
💡 Perfect beginner project for learning Java + Computer Vision.
FaceDetectionProject/
├── lib/
│ └── opencv_java4120.dll # Native library (NOT pushed to GitHub)
├── src/
│ ├── resources/
│ │ └── haarcascade_frontalface_default.xml
│ └── FaceDetector.java # Main program
├── face.jpg # Input image
└── README.md
⚠ Note: Large OpenCV binaries (.dll, .jar) should NOT be pushed to GitHub.
Instead, users must download them manually.
Download the latest release from: 🔗 https://opencv.org/releases/
Find these files inside the extracted folder:
opencv-4120.jaropencv_java4120.dll
- Add opencv-4120.jar to your Build Path
- Add the folder containing the
.dllto:
-Djava.library.path=lib
Place inside:
src/resources/haarcascade_frontalface_default.xml
Place any image as:
face.jpg
(or change the filename inside the code)
javac -cp ".;lib/opencv-4120.jar" src/FaceDetector.java
java -cp ".;lib/opencv-4120.jar" -Djava.library_path=lib FaceDetectorA new face.jpg file is generated with rectangles drawn around the detected faces.
✔ Integrating OpenCV with Java
✔ Understanding Haar Cascade classifier
✔ Reading & writing images using OpenCV (Mat, Imgcodecs)
✔ Drawing on images (Imgproc.rectangle())
✔ Handling native OpenCV libraries (.dll)
✔ Debugging vision errors (empty Mat, wrong path, detection issues)
✔ Organizing Java projects with external resources
✔ Using Git/GitHub with large-file exclusions
- Accuracy depends on lighting, face size, and angle
- Haar cascade should be the official OpenCV XML
.dll/.exefiles must NOT be pushed to GitHub- Use a
.gitignorefor binaries
Example:
*.dll
*.exe
*.jar