Ask Your Question
0

opencv3.3.1 with jdk 1.8 FaceDetect error

asked 2020-11-09 20:37:20 -0600

lsh235 gravatar image

updated 2020-11-14 05:38:21 -0600

Code:

public Computer() {
        System.out.println(Computer.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));
        initComponents();
    }

    private DaemonThread myThread = null;
    private VideoCapture webSource = null;
    private final Mat frame = new Mat(1000, 1000, 1);
    private final MatOfByte mem = new MatOfByte();
    int count = 0;
    CascadeClassifier faceDetector = new CascadeClassifier(Computer.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));
    MatOfRect faceDetections = new MatOfRect();

    private class DaemonThread implements Runnable {
        protected volatile boolean runnable = false;
        private JLabel display;
        public DaemonThread(JLabel displayLabel) {
            this.display = displayLabel;
        }
        @Override
        public void run() {
            synchronized (this) {
                while (runnable) {
                    if (webSource.grab()) {
                        try {
                            webSource.retrieve(frame);
                            Graphics g = display.getGraphics();
                            //add
                            faceDetector.detectMultiScale(frame, faceDetections); //(Computer.java:73)
                            for (Rect rect : faceDetections.toArray()) {
                               // System.out.println("ttt");
                                Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                                        new Scalar(0, 255,0));
                            }
                            //
                            Imgcodecs.imencode(".bmp", frame, mem);
                            Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
                            BufferedImage buff = (BufferedImage) im;// buff = display
                            //buff = display
                            if (g.drawImage(buff, 0, 0, getWidth(), getHeight()-150 , 0, 0, buff.getWidth(), buff.getHeight(), null)) {
                                if (runnable == false) {
                                    System.out.println("Paused ..... ");
                                    this.wait();
                                }
                            }
                        } catch (Exception e) {
                                       System.out.println("Error!!");
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

I've only uploaded a portion of the code

I keep getting errors in making FaceDetect using opencv.

faceDetector.detectMultiScale(frame, faceDetections);

There's an error in this area.

CascadeClassifier faceDetector = new CascadeClassifier(Computer.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));

I revised this to solve it, but it didn't work.

I'm studying Java and opencv alone for the first time and I would appreciate it if you could help me!

Below is the sentence of error.

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698 CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale ] at org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(Native Method) at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:162) at test.Computer$DaemonThread.run(Computer.java:73) at java.lang.Thread.run(Thread.java:748)
edit retag flag offensive close merge delete

Comments

U can't used arguement faceDetections inside perimeter faceDetector.detectMultiScale(frame, faceDetections);

supra56 gravatar imagesupra56 ( 2020-11-10 04:17:04 -0600 )edit
1

Thank you for your comment! So...MatOfRect faceDetections = new MatOfRect();this is the wrong part? If not, is that code all wrong? faceDetector.detectMultiScale(frame, faceDetections); Is it impossible to modify this part to make the program possible?

lsh235 gravatar imagelsh235 ( 2020-11-10 09:32:46 -0600 )edit

Apologized. I didn't see tag that u r using java.

faceDetector.detectMultiScale(frame, faceDetections, 1.3, 3, 0, new Size(), new Size());
supra56 gravatar imagesupra56 ( 2020-11-10 21:03:44 -0600 )edit

No problem:D But there's another issue...

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
]
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale_0(Native Method)
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:153)

This issue arises. Can CPP be used in Java? Or is it another problem?

lsh235 gravatar imagelsh235 ( 2020-11-10 22:36:05 -0600 )edit

I don't know a thing because it's my first studying. I'm sorry to keep asking. And after that issue comes the same issue as before.

at test.Computer$DaemonThread.run(Computer.java:73)
at java.lang.Thread.run(Thread.java:748)
lsh235 gravatar imagelsh235 ( 2020-11-10 22:38:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2020-11-14 03:57:35 -0600

berak gravatar image

updated 2020-11-14 03:58:10 -0600

your cascade xml file did not load. please try to replace the

Computer.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1)

part with an absolute path like

c:\opencv\data\haarcascades\haarcascade_frontalface_alt.xml
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-11-09 20:33:56 -0600

Seen: 409 times

Last updated: Nov 14 '20