Hi guys, I'm very vey new to opencv. i'm trying to make a Java program that can detect my face through a webcam. i don't know which library i should import and i don't know how to make it able to detect my webcam. Based on this tutorial
i try to write my java code for detecting face through a webcam, but i got so many errors, can someone tell me why the error occur? I'm new in opencv and java.
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.core.Size;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import java.util.Vector;
import java.io.*;
import java.lang.Enum;
/**
* @author Lycosa
*
*/
org.opencv.objdetect.CascadeClassifier;
public class detedctAndDisplay {
/** Global variables */
private CamCapture {
/**
* @param args
*/
public static final int CV_HAAR_SCALE_IMAGE = 0;
private static void main(String[] args) {
//load opencv native library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String face_cascade_name = "haarcascade_frontalface_alt.xml";
private static String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
private static CascadeClassifier face_cascade = new CascadeClassifier();
private static CascadeClassifier eyes_cascade = new CascadeClassifier();
private static String window_name = "Capture - Face detection";
private static void detectAndDisplay(Mat frame)
{
Vector<Rect> faces = new Vector<Rect>();
detection.jpg";
System.out.println("capture through camera "+Core.VERSION);
//load the face xml cascade
if(!face_cascade.load(face_cascade_name))
{
System.out.println("Error loading face cascade");
}
else
{
System.out.println("Success loading face cascade");
}
//load the eyes xml cascade
if(!eyes_cascade.load(eyes_cascade_name))
{
System.out.println("Error loading eyes cascade");
}
else
{
System.out.println("Success loading eyes cascade");
}
//detect default camera
VideoCapture capture = new VideoCapture(0);
if(!capture.isOpened())
{
System.out.println("Did not connected to camera.");
}
else
{
System.out.println("Conected to camera: "+capture.toString());
}
//create new Mat image
Mat frame = new Mat();
capture.retrieve(frame);
Mat frame_gray = new Mat();
Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGRA2GRAY);
Imgproc.equalizeHist(frame_gray, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale( frame_gray,
MatOfRect faces = new MatOfRect();
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, 0, new Size(30, 30) )
for (int i = 0; i < faces.size(); 30), new Size() );
Rect[] facesArray = faces.toArray();
for(int i=0; i<facesArray.length; i++)
{
{
Point center = new Point(faces.get(i).x Point(facesArray[i].x + faces.get(i).width facesArray[i].width * 0.5, faces.get(i).y facesArray[i].y + faces.get(i).height facesArray[i].height * 0.5);
ellipse(frame, Core.ellipse(frame, center, new Size(faces.get(i).width Size(facesArray[i].width * 0.5, faces.get(i).height facesArray[i].height * 0.5), 0, 0, 360, new Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray(faces.get(i));
Vector<Rect> frame_gray.submat(facesArray[i]);
MatOfRect eyes = new Vector<Rect>();
MatOfRect();
Rect[] eyesArray = eyes.toArray();
//-- In each face, detect eyes
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, new 0,new Size(30, 30));
30), new Size());
for (int j = 0; j < eyes.size(); eyesArray.length; j++)
{
Point center1 = new Point(faces.get(i).x Point(facesArray[i].x + eyes.get(j).x eyesArray[i].x + eyes.get(j).width eyesArray[i].width * 0.5, faces.get(i).y facesArray[i].y + eyes.get(j).y eyesArray[i].y + eyes.get(j).height eyesArray[i].height * 0.5);
int radius = cvRound((eyes.get(j).width (int) Math.round((eyesArray[i].width + eyes.get(j).height) eyesArray[i].height) * 0.25);
circle(frame, Core.circle(frame, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- Show what you got
imshow(window_name, }
Highgui.imwrite(window_name, frame);
}
/**
* @param args
*/
private static void Main(String[] args)
{
CvCapture capture;
Mat frame = new Mat();
//-- 1. Load the cascades
if (!face_cascade.load(face_cascade_name))
{
System.out.print("--(!)Error loading\n");
return -1;
};
if (!eyes_cascade.load(eyes_cascade_name))
{
System.out.print("--(!)Error loading\n");
return -1;
};
//-- 2. Read the video stream
capture = cvCaptureFromCAM(-1);
if (capture != null)
{
while (true)
{
frame = cvQueryFrame(capture);
//-- 3. Apply the classifier to the frame
if (!frame.empty())
{
detectAndDisplay(frame);
}
else
{
System.out.print(" --(!) No captured frame -- Break!");
break;
}
int c = waitKey(10);
if ((byte)c == 'c')
{
break;
}
}
}
}}
capture.release();
}
}