[JAVA] Desktop: OpenCV FaceDetector By WebCam

Goal
Test OpenCV FaceDetector By WebCam.

OpenCV_FaceDetectorByWebCam.java


import javax.swing.JFrame;
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.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

public class OpenCV_FaceDetectorByWebCam {

    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }
    public static void main(String arg[]) {
        VideoCapture capture = new VideoCapture();
        capture.open(0);
        JFrame frame1 = new JFrame("show image");
        frame1.setTitle("¤HÁyÀË´ú");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setSize(640, 480);
        frame1.setBounds(0, 0, frame1.getWidth(), frame1.getHeight());
        Panel panel1 = new Panel();
        frame1.setContentPane(panel1);
        frame1.setVisible(true);


        if (!capture.isOpened()) {
            System.out.println("Error");
        } else {
            Mat webcam_image = new Mat();
            capture.read(webcam_image);
            frame1.setSize(webcam_image.width() + 10,webcam_image.height() + 20);

            CascadeClassifier faceDetector = new CascadeClassifier("D:\\projects\\Java\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt.xml");
            MatOfRect faceDetections = new MatOfRect();



            while (true) {
                capture.read(webcam_image);
                if( !webcam_image.empty() ){
                    faceDetector.detectMultiScale(webcam_image, faceDetections);
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                                new Scalar(0, 255, 0),3);
                    }

                    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

                    panel1.setimagewithMat(webcam_image);
                    frame1.repaint();



                }else{
                    capture.release();
                    break;
                }
            }
        }
    }

}

Result
![OpenCV_FaceDetectorByWebCam.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/2353f139-bb65-617a-d255-ff950fdf6604.jpeg)

Recommended Posts

Desktop: OpenCV FaceDetector By WebCam
Desktop: OpenCV FaceDetector
Desktop: OpenCV Find Chessboard Corners By WebCam
Desktop: Opencv webcam preview
Desktop: OpenCV Find Chessboard Corners By WebCam
Desktop: OpenCV webcam preview tutorial
Desktop: OpenCV Subdiv2d Make Delaunay For Face By WebCam
Desktop: OpenCV Threshold
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Rectangle Check Inside By ClipLine
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
Desktop: OpenCV Denoise
Desktop: OpenCV Sharpness
Desktop: OpenCV Concat
Desktop: OpenCV OpenCV_SalonUseBlurAddWeighted
Desktop: OpenCV Mosaic
Desktop: OpenCV Erode
Desktop: OpenCV Denoise
Desktop: OpenCV Rectangle
Desktop: OpenCV Watershed
Desktop: OpenCV Text
Desktop: OpenCV Inpaint
Desktop: OpenCV NormalizeBlur
Desktop: OpenCV StereoSGBM
Desktop: OpenCV Spot
Desktop: OpenCV Canny
Desktop: OpenCV Denoise3
Desktop: OpenCV Histogram
Desktop: OpenCV Dft
Desktop: OpenCV Decolor
Desktop: OpenCV Denoise2
Desktop: OpenCV StereoBM
Desktop: OpenCV Kirsch Filter
Desktop: OpenCV Laplacian Filter 2
Desktop: OpenCV Add WaterMark
Desktop: OpenCV Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Freichennel Filter
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Draw Circle
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV Sobel Filter2
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV OpticalFlow PyrLK
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
Desktop: OpenCV Laplacian Filter
Desktop: OpenCV Median Blur
Desktop: OpenCV Add Broad
Desktop: OpenCV Robinson Filter