在Windows中使用Java中的网络摄像头界面进行人脸检测

时间:2013-12-01 21:42:53

标签: java opencv webcam face-detection

我是OpenCV的新手,并致力于人脸检测项目。我想创建一个用于人脸检测的网络摄像头界面,我参考了教程:here 我对代码进行了以下更改(仅修改了CascadeClassifier位置和HaarCascade文件路径):

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.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 static com.googlecode.javacv.cpp.opencv_core.cvFlip;

public class detedctAndDisplay {
    /** Global variables */

    private static String face_cascade_name = "haarcascade_frontalface_alt.xml";
    private static String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";

    private static CascadeClassifier face_cascade;
    private static CascadeClassifier eyes_cascade;
    private static String window_name = "Capture - Face detection";

    public detedctAndDisplay(){
        face_cascade = new CascadeClassifier("C:\\Users\\Admin\\Desktop\\face detection\\FaceDetection\\src\\haarcascade_frontalface_alt.xml"); 
        eyes_cascade = new CascadeClassifier("C:\\Users\\Admin\\Desktop\\face detection\\FaceDetection\\src\\"+eyes_cascade_name);
    }

    public static void detectAndDisplay(Mat frame)
    {
        CascadeClassifier face_cascade= new CascadeClassifier("C:\\Users\\Admin\\Desktop\\face detection\\FaceDetection\\src\\haarcascade_frontalface_alt.xml");

        Mat frame_gray = new Mat();
        MatOfRect faces = new MatOfRect();

        Rect[] facesArray = faces.toArray();

        // Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGRA2GRAY);
        Imgproc.equalizeHist(frame_gray, frame_gray);

        //-- Detect faces


        face_cascade.detectMultiScale(frame_gray,faces );


        for (int i = 0; i < facesArray.length; i++)
        {

            Point center = new Point(facesArray[i].x + facesArray[i].width * 0.5, facesArray[i].y + facesArray[i].height * 0.5);

            Core.ellipse(frame, center, new Size(facesArray[i].width * 0.5, facesArray[i].height * 0.5), 0, 0, 360, new Scalar(255, 0, 255), 4, 8, 0);

            Mat faceROI = frame_gray.submat(facesArray[i]);

            MatOfRect eyes = new MatOfRect();

            Rect[] eyesArray = eyes.toArray();

            //-- In each face, detect eyes
            eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0,new Size(30, 30), new Size());

            for (int j = 0; j < eyesArray.length; j++)
            {

                Point center1 = new Point(facesArray[i].x + eyesArray[i].x + eyesArray[i].width * 0.5,   facesArray[i].y + eyesArray[i].y + eyesArray[i].height * 0.5);
                int radius = (int) Math.round((eyesArray[i].width + eyesArray[i].height) * 0.25);
                Core.circle(frame, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);

            }
        }
        //-- Show what you got
        Highgui.imwrite(window_name, frame);
    }

    /**
    * @param args
    */
    public static void main(String[] args)
    {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //face_cascade = new CascadeClassifier("C:\\Users\\Admin\\Desktop\\face detection\\FaceDetection\\src\\haarcascade_frontalface_alt.xml");
        CascadeClassifier face_cascade1 = new CascadeClassifier("C:\\Users\\Admin\\Desktop\\face detection\\FaceDetection\\src\\haarcascade_frontalface_alt.xml"); 
        //eyes_cascade = new CascadeClassifier("C:\\Users\\Admin\\Desktop\\face detection\\FaceDetection\\src\\"+eyes_cascade_name);
        VideoCapture capture;
        Mat frame = new Mat();


        capture = new VideoCapture(0);

        if(!capture.isOpened())
        {
            System.out.println("Did not connect to camera.");
        }
        else
        {

            capture.retrieve(frame);

            detectAndDisplay(frame);

            capture.release();

        }
    }

}

我收到以下错误:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type
in unknown function, file ..\..\..\..\opencv\modules\core\src\array.cpp, line 2482
Exception in thread "main" CvException [org.opencv.core.CvException:    
cv::Exception: ..\..\..\..\opencv\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or 
unsupported array type
]
at org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(Native Method)
at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:127)
at detedctAndDisplay.detectAndDisplay(detedctAndDisplay.java:55)
at detedctAndDisplay.main(detedctAndDisplay.java:125)

我无法解决错误。 另外,请告诉我要导入哪个jar以便可以使用cvtColor? 谢谢!

1 个答案:

答案 0 :(得分:3)

OpenCV抱怨“数组”(矩阵或图像)类型未知,因为您从未初始化frame_gray。您需要将图像实际转换为灰度。你实际上有一条线可以做到,但它被注释掉了:

// Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGRA2GRAY);

取消注释该行。