opencv face detection system project in java netbeans with source code

This opencv face detection system project developed to identify and count human faces. In this tutorial, I show you how to detect and count face with the webcam. This opencv face detection system project has developed using java OpenCV. Complete project source code includes in this post.

opencv face detection system source Code

package syntech;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import static org.opencv.objdetect.Objdetect.CASCADE_SCALE_IMAGE;
import org.opencv.videoio.VideoCapture;

 * @author ccs
public class Home extends javax.swing.JFrame {

     * Creates new form Home
    String source = "C:\javacv resource\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml"; //paste here
  //  String source = "C:\Users\ccs\Desktop\trafic\vehicle_detection_haarcascades-master\cars.xml";
    CascadeClassifier faceDetector = new CascadeClassifier(source);
    public Home() {


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        (new Thread(){
            public void run(){
                VideoCapture capture = new VideoCapture(0);//0 mean your default web cam
         //   VideoCapture capture = new VideoCapture("C:\Users\ccs\Desktop\videoplayback.mp4");
                MatOfRect rostros = new MatOfRect();
                MatOfByte mem = new MatOfByte();
                Mat frame = new Mat();
                Mat frame_gray = new Mat();
                Rect[] facesArray;      
                Graphics g;
                BufferedImage buff = null;

                        System.out.println("No detection");
                        try {
                            g = jPanel1.getGraphics();
                            Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);
                            Imgproc.equalizeHist(frame_gray, frame_gray);
                            double w = frame.width();
                            double h = frame.height();
                            faceDetector.detectMultiScale(frame_gray, rostros, 1.1, 2, 0|CASCADE_SCALE_IMAGE, new Size(30, 30), new Size(w, h) );
                            facesArray = rostros.toArray();
                            System.out.println("No of faces: "+facesArray.length);
                            for (int i = 0; i < facesArray.length; i++) {
                                Point center = new Point((facesArray[i].x + facesArray[i].width * 0.5), 
                                        (facesArray[i].y + facesArray[i].height * 0.5));
                                        new Size(facesArray[i].width * 0.5, facesArray[i].height * 0.5), 
                                        new Scalar(255, 0, 255), 4, 8, 0);
                                Mat faceROI = frame_gray.submat(facesArray[i]);
                                        new Point(facesArray[i].x,facesArray[i].y),
                                        new Point(facesArray[i].x+facesArray[i].width,facesArray[i].y+facesArray[i].height),
                                        new Scalar(123, 213, 23, 220));
                              //  Imgproc.putText(frame, "Width: "+faceROI.width()+" Height: "+faceROI.height()+" X = "+facesArray[i].x+
                               //         " Y = "+facesArray[i].y, new Point(facesArray[i].x, facesArray[i].y-20), 1, 1, new Scalar(255,255,255));
                                Imgproc.putText(frame, "This is human "
                                     , new Point(facesArray[i].x, facesArray[i].y-20), 1, 1, new Scalar(255,255,255));
                            int no= facesArray.length;
                             Imgcodecs.imencode(".bmp", frame, mem);
                            Image im = ByteArrayInputStream(mem.toArray()));
                            buff = (BufferedImage) im;
                            if(g.drawImage(buff, 0, 0, jPanel1.getWidth(), jPanel1.getHeight() , 0, 0, buff.getWidth(), buff.getHeight(), null)){
                        } catch (Exception ex) {

The technologies used in opencv face detection system project

  • Java: All codes have been written using the java programming language.
  • Netbeans: This project has developed inside of the Netbeans IDE.

How to use this project
You can download the complete project file and database using the below link.

The goals of providing this project:

  • To provide an example for students to develop their own projects.
  • helping people create their projects.
  • sharing knowledge and codes for educational purpose.

This project is not for:

  • You cannot use this project or project source codes for commercial purposes
  • You cannot re-upload this project or project source code to web internet or any kind of space.

Copyright © codeguid

Watch my previous video regarding OpenCV to get a clear idea.

Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact

Download opencv face detection system project


Check more projects>>

1 Comment

Leave a Reply

Your email address will not be published.