2013-10-19 137 views
0

我用C++編寫了一些使用opencv庫進行人臉檢測的代碼。在這裏,我正在用臉控制鼠標指針。當我寫Qt中相同的代碼,它不會加載haarcascade XML文件Qt中的人臉檢測和opencv

這裏是我的C++代碼:

#include<opencv2/opencv.hpp> 
#include<opencv2/highgui/highgui.hpp> 
#include<time.h> 
#include<stdio.h> 
#include<stdlib.h> 
using namespace std; 
using namespace cv; 

CascadeClassifier faceCade; 

String faceCascadeName = "lbpcascade_frontalface.xml"; 
String FaceDetectWindow = "Face Detector Window"; 
String FaceDetectGrayWindow = "Face Detector Gray Window"; 


int main() { 

    VideoCapture cap(0); 
    Mat camFrames, grayFrames; 
    vector<Rect> faces; 
    long imageIndex = 0; 

    int preX=0,preY=0; 
    int j=0; 

    if(!faceCade.load(faceCascadeName)){ cout<<"--(!)Error loading\n"; return -1; }; 

    while (1) { 
     cap >> camFrames; 

     cvtColor(camFrames, grayFrames, CV_BGR2GRAY); 
     equalizeHist(grayFrames, grayFrames); 

     faceCade.detectMultiScale(grayFrames, faces, 1.1, 2, 0, Size(160, 160)); 

     for (int i = 0; i < faces.size(); i++) 
     { 
      Mat faceROI = grayFrames(faces[i]); 

      rectangle(camFrames, Rect(faces[i].x - 25,faces[i].y - 25,faces[i].width + 35 ,faces[i].height + 35), Scalar(0, 0, 255), 1, 1, 0); 

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


      cout<<"\nx = "<<faces[i].x<<"\ty = "<<faces[i].y; 

      //while(j==10) 
      //{ 
      if((faces[i].y<125)&&(faces[i].x<255)) 
      { 
       printf(" RU"); 
       system("xte 'mousermove 10 -10'"); 
       } 
      else if((faces[i].y>185)&&(faces[i].x<255)){ 
        printf(" RD"); 
        system("xte 'mousermove 10 10'"); 
        } 
      else if((faces[i].y>185)&&(faces[i].x>305)){ 
        printf(" LD"); 
        system("xte 'mousermove -10 10'"); 
        } 
      else if((faces[i].y<125)&&(faces[i].x<255)){ 
        printf(" LU"); 
        system("xte 'mousermove -10 -10'"); 
        } 
       else if((faces[i].y>185)&&(faces[i].x>255)&&(faces[i].x<305)){ 
        printf(" Down"); 
        system("xte 'mousermove 0 10'"); 
        } 
       else if((faces[i].y<125)&&(faces[i].x>255)&&(faces[i].x<305)){ 
        printf(" up"); 
        system("xte 'mousermove 0 -10'"); 
        } 
       else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x<255)){ 
        printf(" L"); 
        system("xte 'mousermove -10 0'"); 
        } 
      else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x>305)){ 
        printf(" R"); 
        system("xte 'mousermove 10 0'"); 
        } 
       preX = faces[i].x; 
       preY = faces[i].y; 
       j=0; 
       } 
     //j++; 
     //} 

     imshow(FaceDetectWindow, camFrames); 
     imshow(FaceDetectGrayWindow, grayFrames); 
     if(waitKey(30) >= 0) break; 

    } 

} 

這裏檢測到面部,但在它下面的Qt代碼不工作

#include<opencv2/opencv.hpp> 
#include<opencv2/highgui/highgui.hpp> 
#include<time.h> 
#include<stdio.h> 
#include<stdlib.h> 
using namespace std; 
using namespace cv; 

CascadeClassifier faceCade; 

String faceCascadeName = ":/new/prefix1/lbpcascade_frontalface.xml"; 
String FaceDetectWindow = "Face Detector Window"; 
String FaceDetectGrayWindow = "Face Detector Gray Window"; 


int main() { 

    VideoCapture cap(0); 
    Mat camFrames, grayFrames; 
    vector<Rect> faces; 
    long imageIndex = 0; 

    int preX=0,preY=0; 
    int j=0; 

    if(!faceCade.load("lbpcascade_frontalface.xml")){ cout<<"--(!)Error loading\n"; return -1; }; 

    while (1) { 
     cap >> camFrames; 

     cvtColor(camFrames, grayFrames, CV_BGR2GRAY); 
     equalizeHist(grayFrames, grayFrames); 

     faceCade.detectMultiScale(grayFrames, faces, 1.1, 2, 0, Size(80, 80)); 

    /* for (int i = 0; i < faces.size(); i++) 
     { 
      Mat faceROI = grayFrames(faces[i]); 

      rectangle(camFrames, Rect(faces[i].x - 25,faces[i].y - 25,faces[i].width + 35 ,faces[i].height + 35), Scalar(0, 0, 255), 1, 1, 0); 

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


      cout<<"\nx = "<<faces[i].x<<"\ty = "<<faces[i].y; 

      //while(j==10) 
      //{ 
      if((faces[i].y<125)&&(faces[i].x<255)) 
      { 
       printf(" RU"); 
       system("xte 'mousermove 10 -10'"); 
       } 
      else if((faces[i].y>185)&&(faces[i].x<255)){ 
        printf(" RD"); 
        system("xte 'mousermove 10 10'"); 
        } 
      else if((faces[i].y>185)&&(faces[i].x>305)){ 
        printf(" LD"); 
        system("xte 'mousermove -10 10'"); 
        } 
      else if((faces[i].y<125)&&(faces[i].x<255)){ 
        printf(" LU"); 
        system("xte 'mousermove -10 -10'"); 
        } 
       else if((faces[i].y>185)&&(faces[i].x>255)&&(faces[i].x<305)){ 
        printf(" Down"); 
        system("xte 'mousermove 0 10'"); 
        } 
       else if((faces[i].y<125)&&(faces[i].x>255)&&(faces[i].x<305)){ 
        printf(" up"); 
        system("xte 'mousermove 0 -10'"); 
        } 
       else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x<255)){ 
        printf(" L"); 
        system("xte 'mousermove -10 0'"); 
        } 
      else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x>305)){ 
        printf(" R"); 
        system("xte 'mousermove 10 0'"); 
        } 
       preX = faces[i].x; 
       preY = faces[i].y; 
       j=0; 
       } 
      //j++; 
     //}*/ 

     imshow(FaceDetectWindow, camFrames); 
     imshow(FaceDetectGrayWindow, grayFrames); 
     if(waitKey(30) >= 0) break; 

    } 

} 

請幫我做,在QT平臺 這裏Qt代碼所示的輸出是 - (!)錯誤加載 這意味着級聯文件未加載

+1

String faceCascadeName =「:/new/prefix1/lbpcascade_frontalface.xml」; < - 那是*不是*我知道的任何機器上的有效路徑 – berak

回答

0

下面的路徑似乎並不正確:

String faceCascadeName = ":/new/prefix1/lbpcascade_frontalface.xml"; 
2
String faceCascadeName = ":/new/prefix1/lbpcascade_frontalface.xml"; 

這是一個有效的路徑,但你需要在Qt的資源文件來創建它。

http://qt-project.org/doc/qt-5.0/qtcore/resources.html

否則,如果你不想處理資源,走並使其相對路徑。

例如:

"./file.xml"將尋求從工作目錄中的XML文件。

"../file.xml"將查找工作目錄上一級的xml文件。

希望有所幫助。

+0

哦,謝謝你,不知道。所以,':'似乎是相對的,但是是什麼?謹慎解釋? – berak

+1

閱讀[qt資源](http://qt-project.org/doc/qt-5.0/qtcore/resources.html)上的鏈接。這是一個嵌入到exe文件。 – phyatt