How to show Text if no face is detected with OpenCV?

asked 2017-05-27 07:57:24 -0600

myCode gravatar image

updated 2017-05-27 07:59:34 -0600

How can I display a Text if no face is detected with openCV?

Here what I have tried

private void runLoop(){

    Mat webCamImg = new Mat();

    ConvertImage convertImage = new ConvertImage();


    if (capture.isOpened()){
        while (true){
            capture.read(webCamImg);

            FaceDect f = new FaceDect();

            f.faceDetect(webCamImg);
            MatOfRect textRect = new MatOfRect();

            if (f.faceDetect(webCamImg) == false){

                for (Rect rect:textRect.toArray()) {
                    Imgproc.putText(webCamImg, " no face ",
                            new Point(rect.x, rect.y - 5), 1, 1,
                            new Scalar(0, 0, 225));
                }
            }

            if (!webCamImg.empty()){
                tmpImg= convertImage.convertMatToImg(webCamImg);
                ImageIcon imageIcon = new ImageIcon(tmpImg,"Captured");
                label.setIcon(imageIcon);
                frame.pack();
            }
            else {
                System.out.println("no frame");
                break;
            }
        }
    }
    else {
        System.out.println("nop nop");
    }
}

other method

public class FaceDect {

    public boolean faceDetect(Mat matImg){

        String pathFace= "/opencv/data/haarcascades/haarcascade_frontalface_alt.xml";

        CascadeClassifier faceCascade = new CascadeClassifier(pathFace);
        MatOfRect face = new MatOfRect();

        faceCascade.detectMultiScale(matImg,face);

        for (Rect rect:face.toArray()) {
            Imgproc.putText(matImg, "face",
                    new Point(rect.x, rect.y - 5), 1, 1,
                    new Scalar(0, 0, 225));

            Imgproc camShow = new Imgproc();


            camShow.rectangle(matImg,
                    new Point(rect.x, rect.y),
                    new Point(rect.x + rect.width, rect.y + rect.height),
                    new Scalar(0, 100, 200), 5);
        }



        return true;
    }
}

i want to show a text if there is no face. how can i do it ? i made the extra method for face detection with a boolean type so that i can say it on if.

edit retag flag offensive close merge delete