[JAVA] Bureau: OpenCV_FaceEyeNoseDetectorQuicklyByWebCam

Goal
Test OpenCV_Face Eye Nose Detector Quickly By WebCam.

OpenCV_FaceEyeNoseDetectorQuicklyByWebCam.java


import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

import javax.swing.*;

public class OpenCV_FaceEyeNoseDetectorQuicklyByWebCam {
    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("Humain 臉+Paupières+Mesure du nez de l'enfant");
        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();

            CascadeClassifier eyeDetector = new CascadeClassifier("D:\\projects\\Java\\OpenCV_Samples\\resource\\haarcascades\\haarcascade_mcs_eyepair_big.xml");
            CascadeClassifier noseDetector = new CascadeClassifier("D:\\projects\\Java\\OpenCV_Samples\\resource\\haarcascades\\haarcascade_mcs_nose.xml");
            MatOfRect eyeDetection = new MatOfRect();
            MatOfRect noseDetection = 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);


                        //Oeil 臉 2/3 處 supérieur
                        int eyeStartX=rect.x+10;
                        int eyeStartY=rect.y;

                        int eyeEndX=rect.x+rect.width-10;
                        int eyeEndY=rect.y+rect.height*2/3;


                        //Nez enfant 1/4 處 vers le bas,Constriction de position
                        int noseStartX=rect.x+20;
                        int noseStartY=rect.y+rect.height*1/4;
                        //int noseStartY=rect.y+20;

                        int noseEndX=rect.x+rect.width-20;
                        int noseEndY=rect.height-10;


                        Rect eyeRect=new Rect(new Point(eyeStartX,eyeStartY),new Point(eyeEndX,eyeEndY));
                        //Imgproc.rectangle(webcam_image, new Point(eyeStartX,eyeStartY),new Point(eyeEndX,eyeEndY),new Scalar(0, 25, 30),1);
                        Rect noseRect=new Rect(new Point(noseStartX,noseStartY),new Point(noseEndX,noseEndY));
                        try {
                            Mat subWebcamForEye=webcam_image.submat( eyeRect);
                            eyeDetector.detectMultiScale(subWebcamForEye, eyeDetection);
                            Rect subRealEyeRect=eyeDetection.toArray()[0];
                            Imgproc.rectangle(subWebcamForEye, new Point(subRealEyeRect.x, subRealEyeRect.y), new Point(subRealEyeRect.x + subRealEyeRect.width,subRealEyeRect.y + subRealEyeRect.height),
                                    new Scalar(255,0 , 0),2);

                            Mat subWebcamForNose=webcam_image.submat( noseRect);
                            noseDetector.detectMultiScale(subWebcamForNose, noseDetection);
                            Rect subRealNoseRect=noseDetection.toArray()[0];
                            Imgproc.rectangle(subWebcamForNose, new Point(subRealNoseRect.x, subRealNoseRect.y), new Point(subRealNoseRect.x + subRealNoseRect.width,subRealNoseRect.y + subRealNoseRect.height),
                                    new Scalar(0, 0,255 ),2);



                        } catch (Exception e) {
                            System.out.println(e);
                        }



                    }

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




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



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

}
Result
![opencv_face_eye_nose_detector_quickly_by_webcam.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/2344183b-4e13-dd7d-5441-cbad252f382f.jpeg)

Recommended Posts

Bureau: OpenCV_FaceEyeNoseDetectorQuicklyByWebCam
Bureau: RoiForRectangle RoiForRectangle
Bureau: GaussianBlurFix
Bureau: OpenCV_OpenCV_GaussianBlur
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Dilate
Bureau: OpenCV Expand
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: curseur Gui
Bureau: OpenCV Ellipse2Poly
Bureau: OpenCV HDR
Bureau: Polylignes OpenCV
Bureau: OpenCV Denoise
Bureau: netteté OpenCV
Bureau: bouton Gui
Bureau: OpenCV OpenCV_SalonUseBlurAddWeighted
Bureau: OpenCV Mosaic
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 FaceDetector
Bureau: OpenCV StereoBM