src.main.java.org.roomwatcher.watcher.Processor.java Source code

Java tutorial

Introduction

Here is the source code for src.main.java.org.roomwatcher.watcher.Processor.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package src.main.java.org.roomwatcher.watcher;

import java.awt.Component;
import java.awt.TextField;
import org.opencv.core.Core;
import org.opencv.core.Mat;
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.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

/**
 *
 * @author Waldema
 */
public class Processor {

    private CascadeClassifier face_cascade;

    // Create a constructor method  

    public Processor() {
        face_cascade = new CascadeClassifier(
                "C:\\java\\real-time-face-detection-using-opencv-with-java\\src\\gui\\haarcascade_frontalface_alt.xml");
        if (face_cascade.empty()) {
            System.out.println("--(!)Error loading A\n");
            return;
        } else {
            System.out.println("Face classifier loooaaaaaded up");
        }
    }

    public Mat detect(Mat inputframe) {
        Mat mRgba = new Mat();
        Mat mGrey = new Mat();
        MatOfRect faces = new MatOfRect();

        inputframe.copyTo(mRgba);
        inputframe.copyTo(mGrey);

        Imgproc.cvtColor(mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);
        Imgproc.equalizeHist(mGrey, mGrey);

        face_cascade.detectMultiScale(mGrey, faces);
        Window.setPeopleNumberLabelValue(String.valueOf(faces.toArray().length));

        for (Rect rect : faces.toArray()) {
            Point center = new Point(rect.x + rect.width * 0.5, rect.y + rect.height * 0.5);
            Core.ellipse(mRgba, center, new Size(rect.width * 0.5, rect.height * 0.5), 0, 0, 360,
                    new Scalar(255, 0, 255), 4, 8, 0);
        }

        return mRgba;
    }
}