使用opencv从视频中检测面部和眼睛

时间:2015-05-05 21:52:36

标签: opencv

我必须使用opencv从视频中检测面部和眼睛,首先我加载了视频,我可以看到视频作为输出,但是在编写了用于眼睛和面部检测的代码之后以及在运行代码之后,我不会有任何错误,但我不知道为什么输出窗口也会在一秒后快速打开和关闭,它似乎只运行了一秒钟,我可以看到打开控制台和输出窗口然后关闭它们。我把我的代码和我的一些东西作为输出,如果有任何人可以帮助我并指导我,如果在我的代码中使用命名空间std中有任何错误,我将非常感激;

#include opencv2/core/core.hpp>
#include opencv2/highgui/highgui.hpp>
#include iostream>
#include "stdafx.h"
#include "cxcore.h"
#include "highgui.h"
#include "cv.h"

using namespace std;

using namespace cv;

// Function Headers

void detectAndDisplay(Mat frame);


string face_cascade_name = "haarcascadeshaarcascade_frontalface_alt.xml";

string eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";




CascadeClassifier face_cascade;

CascadeClassifier eyes_cascade;




string window_name = "Face-eye detection";

// Function main

int main(int argc, char** argv)

{

string filename = "C:\\Users\\Rojin\\Desktop\\Erma.avi";

VideoCapture capture(filename);









if (!capture.isOpened())  // check if we succeeded
{

    cout << "Cannot open the video file" << endl;
    return -1;
}

  double fps = capture.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

cout << "Frame per seconds : " << fps << endl;

namedWindow("Face-eye detection", WINDOW_NORMAL); //create a window called "MyVideo"





// Load the cascade

if (!face_cascade.load(face_cascade_name))

{

    cout<<"--(!)Error loading\n"<< endl;

    return (-1);

};




if (!eyes_cascade.load(eyes_cascade_name))

{

    cout<<"--(!)Error loading\n"<< endl;

    return (-1);

};




// Read the video stream

//for (;;)
while(1)

{
    Mat frame;
    //capture >> frame;
    bool bsuccess = capture.read(frame);




    // Apply the classifier to the frame

    if (!frame.empty())

    {

        detectAndDisplay(frame);

    }




    else

    {

        cout << "Cannot read the frame from video file" << endl;
        break;




    }




    //int c = waitKey(10);

    //if ((char)c == 'c')


    imshow("MyVideo", frame); //show the frame in "MyVideo" 

    if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
    {
    cout << "esc key is pressed by user" << endl;
        break;
    }
}

 return 0;

}

// Function detectAndDisplay

void detectAndDisplay(Mat frame)

{

std::vector<Rect> faces;

Mat frame_gray;

Mat crop;

Mat res;

Mat gray;

string text;

stringstream sstm;




cvtColor(frame, frame_gray, COLOR_BGR2GRAY);

equalizeHist(frame_gray, frame_gray);







// Detect faces

face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));




for (size_t i = 0; i < faces.size(); i++)

{




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

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




    Mat faceRoi = frame_gray(faces[i]);

    std::vector<Rect> eyes;




    // in each face detect eyes

    eyes_cascade.detectMultiScale(faceRoi, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));




    for (size_t j = 0; j < eyes.size(); j++)

    {




        Point center(faces[i].x + eyes[j].x+ eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);

int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);

circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);

    }

}
}

输出:

'OpenCV1.exe' (Win32): Loaded 'C:\Users\Rojin\Documents\Visual Studio 2012\Projects\OpenCV1\x64\Debug\OpenCV1.exe'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\OPENCV\opencv\build\x64\vc11\bin\opencv_objdetect249d.dll'. Cannot find or open the PDB file.
'OpenCV1.exe' (Win32): Loaded 'C:\OPENCV\opencv\build\x64\vc11\bin\opencv_highgui249d.dll'. Cannot find or open the PDB file.
'OpenCV1.exe' (Win32): Loaded 'C:\OPENCV\opencv\build\x64\vc11\bin\opencv_imgproc249d.dll'. Cannot find or open the PDB file.
'OpenCV1.exe' (Win32): Loaded 'C:\OPENCV\opencv\build\x64\vc11\bin\opencv_core249d.dll'. Cannot find or open the PDB file.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\msvcp110d.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\msvcr110d.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\user32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\gdi32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\ole32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\oleaut32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\advapi32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_5.82.9600.17415_none_34aa3313958e7a52\comctl32.dll'. Cannot find or open the PDB file.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\msvfw32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\avifil32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\avicap32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\combase.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\msvcrt.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\rpcrt4.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\sechost.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\winmm.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\shell32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\msacm32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\version.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\winmmbase.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\shlwapi.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\cfgmgr32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\devobj.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\imm32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\msctf.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\OPENCV\opencv\build\x64\vc11\bin\opencv_ffmpeg249_64.dll'. Module was built without symbols.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\ws2_32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\wsock32.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\nsi.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\uxtheme.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\dwmapi.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\kernel.appcore.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\cryptbase.dll'. Symbols loaded.
'OpenCV1.exe' (Win32): Loaded 'C:\Windows\System32\bcryptprimitives.dll'.     Cannot find or open the PDB file.
The thread 0x2504 has exited with code 0 (0x0).
The thread 0x1f14 has exited with code 0 (0x0).
The thread 0x704 has exited with code 0 (0x0).
The thread 0x1140 has exited with code 0 (0x0).
The program '[10016] OpenCV1.exe' has exited with code -1 (0xffffffff).

1 个答案:

答案 0 :(得分:1)

以下代码行看起来好像不正确。具体来说,似乎你的级联名称错了。

string face_cascade_name = "haarcascadeshaarcascade_frontalface_alt.xml";

由于用于加载此级联的代码在找不到它时也返回-1,并且由于代码的输出表明程序以返回码-1终止,因此很可能是问题。

if (!face_cascade.load(face_cascade_name))
{
    cout<<"--(!)Error loading\n"<< endl;
    return (-1);
};

我不知道为什么cout无法记录消息,但这可能是Windows线程问题;也许代码在stdout写入之前终止。

无论哪种方式,一旦我更正了Haar级联文件名,代码就可以在OS X上运行。