[JAVA] Bureau: OpenCV FaceDetector par 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

Bureau: OpenCV FaceDetector par WebCam
Bureau: OpenCV FaceDetector
Bureau: OpenCV Trouver les coins de l'échiquier par WebCam
Bureau: aperçu de la webcam Opencv
Bureau: OpenCV Trouver les coins de l'échiquier par WebCam
Bureau: Tutoriel de prévisualisation de la webcam OpenCV
Bureau: OpenCV Subdiv2d Make Delaunay For Face By WebCam
Bureau: seuil OpenCV
Bureau: OpenCV Dilate
Bureau: OpenCV Expand
Bureau: vérification du rectangle OpenCV à l'intérieur par ClipLine
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
Bureau: OpenCV HDR
Bureau: Polylignes OpenCV
Bureau: OpenCV Denoise
Bureau: netteté OpenCV
Bureau: OpenCV Concat
Bureau: OpenCV OpenCV_SalonUseBlurAddWeighted
Bureau: OpenCV Mosaic
Bureau: OpenCV Erode
Bureau: OpenCV Denoise
Bureau: Rectangle OpenCV
Bureau: OpenCV Watershed
Bureau: Texte OpenCV
Bureau: OpenCV Inpaint
Bureau: OpenCV NormalizeBlur
Bureau: OpenCV StereoSGBM
Bureau: OpenCV Spot
Bureau: OpenCV Canny
Bureau: OpenCV Denoise3
Bureau: histogramme OpenCV
Bureau: OpenCV Dft
Bureau: OpenCV Decolor
Bureau: OpenCV Denoise2
Bureau: OpenCV StereoBM
Bureau: Filtre OpenCV Kirsch
Bureau: Filtre laplacien OpenCV 2
Bureau: OpenCV Add WaterMark
Bureau: OpenCV Fill ConvexPoly
Bureau: OpenCV Grab Cut
Bureau: OpenCV Sharpness Gui
Bureau: Changement de couleur OpenCV
Bureau: Filtre Freichennel OpenCV
Bureau: seuil adaptatif OpenCV
Bureau: OpenCV Draw Circle
Bureau: OpenCV Fill Poly
Bureau: OpenCV Mean Filter
Bureau: référentiel Java OpenCV
Bureau: OpenCV Sobel Filter2
Bureau: OpenCV pyrMeanShift Filter
Bureau: OpenCV OpticalFlow PyrLK
Bureau: Piano virtuel OpenCV
Bureau: image de fusion OpenCV
Bureau: Filtre OpenCV Scharr
Bureau: Filtre laplacien OpenCV
Bureau: Flou médian OpenCV
Bureau: OpenCV Add Broad
Bureau: OpenCV Robinson Filter