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)