Skip to content

Commit

Permalink
Modify FaceRecognizer
Browse files Browse the repository at this point in the history
  • Loading branch information
KangLin committed Sep 28, 2017
1 parent 7761233 commit e0f1d0d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 27 deletions.
45 changes: 27 additions & 18 deletions Plugin/FaceRecognizer/DetectFaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
CDetectFaces::CDetectFaces(QObject *parent) : QObject(parent)
{
cv::String szCascades = "D:\\source\\RabbitIm\\ThirdLibrary\\windows_msvc\\etc\\haarcascades\\";
//cv::String szCascades = "D:\\source\\opencv\\data\\haarcascades_cuda\\";
cv::String szFaceCascade = szCascades + "haarcascade_frontalface_alt2.xml";
m_FaceCascade.load(szFaceCascade);
//cv::String eyes_cascade_name = szCascades + "haarcascade_eye_tree_eyeglasses.xml";
Expand All @@ -29,8 +30,8 @@ bool CDetectFaces::DetectFaces(cv::Mat frame, cv::Mat &frame_gray)
CV_HAAR_DO_ROUGH_SEARCH,
cv::Size(1, 1),
cv::Size(50,50));
//t = (double)cvGetTickCount() - t;
//qDebug() << "Detection face time = " << t / ((double)cvGetTickFrequency() * 1000) << "ms";
t = (double)cvGetTickCount() - t;
qDebug() << "Detection face time = " << t / ((double)cvGetTickFrequency() * 1000) << "ms";
for (size_t i = 0; i < faces.size(); i++)
{
cv::rectangle(frame, faces[i], cv::Scalar(0,0,255), 2, 8, 0);
Expand All @@ -43,8 +44,8 @@ bool CDetectFaces::DetectFaces(cv::Mat frame, cv::Mat &frame_gray)
m_EyesCascade.detectMultiScale(faceROI, eyes, 1.1, 3,
CV_HAAR_DO_ROUGH_SEARCH,
cv::Size(1, 1), cv::Size(50, 50));
//t = (double)cvGetTickCount() - t;
//qDebug() << "Detection eye time = " << t / ((double)cvGetTickFrequency() * 1000) << "ms";
t = (double)cvGetTickCount() - t;
qDebug() << "Detection eye time = " << t / ((double)cvGetTickFrequency() * 1000) << "ms";
for (size_t j = 0; j < eyes.size(); j++)
{
cv::Rect rect(faces[i].x + eyes[j].x,
Expand All @@ -58,38 +59,41 @@ bool CDetectFaces::DetectFaces(cv::Mat frame, cv::Mat &frame_gray)
return bFind;
}

bool CDetectFaces::DetectFaces(cv::Mat frame, cv::Mat &frame_gray, double scale)
bool CDetectFaces::DetectFaces(cv::Mat frame,
cv::Mat &frame_gray,
CV_OUT std::vector<cv::Rect>& faces,
double scale)
{
bool bFind = false;
double t = 0;
std::vector<cv::Rect> faces;
std::vector<cv::Rect> f;
cv::Mat smallImg(cvRound(frame.rows / scale), cvRound(frame.cols / scale), CV_8UC1);
cv::cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY);
resize(frame_gray, smallImg, smallImg.size(), 0, 0, cv::INTER_LINEAR);

cv::equalizeHist(smallImg, smallImg);
//t = (double)cvGetTickCount();
t = (double)cvGetTickCount();
m_FaceCascade.detectMultiScale(smallImg,
faces,
f,
1.1,
3,
CV_HAAR_DO_CANNY_PRUNING
|CV_HAAR_FIND_BIGGEST_OBJECT
|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_SCALE_IMAGE);
//t = (double)cvGetTickCount() - t;
t = (double)cvGetTickCount() - t;
//qDebug() << "Detection face time = " << t / ((double)cvGetTickFrequency() * 1000) << "ms";
for (size_t i = 0; i < faces.size(); i++)
for (size_t i = 0; i < f.size(); i++)
{
cv::Rect r;
r.x = cvRound(faces[i].x * scale);
r.y = cvRound(faces[i].y * scale);
r.width = cvRound(faces[i].width * scale);
r.height = cvRound(faces[i].height * scale);
r.x = cvRound(f[i].x * scale);
r.y = cvRound(f[i].y * scale);
r.width = cvRound(f[i].width * scale);
r.height = cvRound(f[i].height * scale);
faces.push_back(r);
cv::rectangle(frame, r, cv::Scalar(0,0,255), 2, 8, 0);

cv::Mat faceROI = smallImg(faces[i]);
frame_gray = faceROI.clone();
cv::Mat faceROI = smallImg(f[i]);
std::vector<cv::Rect> eyes;

//-- In each face, detect eyes
Expand Down Expand Up @@ -120,7 +124,11 @@ bool CDetectFaces::DetectFaces(cv::Mat frame, cv::Mat &frame_gray, double scale)

int CDetectFaces::AddImage(cv::Mat image, int lable)
{
m_Images.push_back(image);
cv::Mat img(50, 120, CV_8UC1);
if(image.empty())
return -1;
resize(image.clone(), img, img.size(), 0, 0, cv::INTER_LINEAR);
m_Images.push_back(img);
m_Lables.push_back(lable);
return 0;
}
Expand All @@ -139,7 +147,8 @@ int CDetectFaces::Train()

int CDetectFaces::Recognizer(cv::Mat image, CV_OUT int &label, CV_OUT double &confidence)
{
m_Model->predict(image, label, confidence);
//m_Model->predict(image, label, confidence);
label = m_Model->predict(image);
qDebug() << "label:" << label << " confidence:" << confidence;
return 0;
}
4 changes: 2 additions & 2 deletions Plugin/FaceRecognizer/DetectFaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#define DETECTFACES_H

#include <QObject>
#include <opencv2\opencv.hpp>
#include "opencv2\opencv.hpp"
#include "opencv2/face.hpp"

class CDetectFaces : public QObject
{
Q_OBJECT
public:
explicit CDetectFaces(QObject *parent = 0);
bool DetectFaces(cv::Mat frame, cv::Mat &frame_gray, double scale);
bool DetectFaces(cv::Mat frame, cv::Mat &frame_gray, CV_OUT std::vector<cv::Rect>&faces, double scale);
bool DetectFaces(cv::Mat frame, cv::Mat &frame_gray);

int AddImage(cv::Mat image, int lable);
Expand Down
22 changes: 17 additions & 5 deletions Plugin/FaceRecognizer/FrmFaceRecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,31 @@ void CFrmFaceRecognizer::on_pbSave_clicked()
void CFrmFaceRecognizer::slotCaptured(cv::Mat frame)
{
cv::Mat frame_gray;
m_DetectFace.DetectFaces(frame, frame_gray, 6);
std::vector<cv::Rect> faces;
m_DetectFace.DetectFaces(frame, frame_gray, faces, 6);

switch (m_Operator) {
case SAVE:
m_DetectFace.AddImage(frame_gray, ui->leLabel->text().toInt());
break;
{
std::vector<cv::Rect>::iterator it;
for(it = faces.begin(); it != faces.end(); it++)
{
cv::Mat ROI = frame(*it);
qDebug() << "ROI width:" << ROI.cols << " Height:" << ROI.rows;
m_DetectFace.AddImage(ROI, ui->leLabel->text().toInt());
}

}
break;
case TRAIN:
m_DetectFace.Train();
break;
case RECOGNIZER:
int label;
double confidence;
{
int label = -1;
double confidence = 0.0;
m_DetectFace.Recognizer(frame_gray, label, confidence);
}
break;
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion Plugin/FaceRecognizer/FrmFaceRecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private slots:
};
__OPERATOR m_Operator;

cv::Mat m_Frame;
cv::Mat m_Frame, m_FrameGray;
void ShowImage(cv::Mat image);
};

Expand Down
20 changes: 19 additions & 1 deletion Plugin/FaceRecognizer/FrmFaceRecognizer.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>658</width>
<width>726</width>
<height>572</height>
</rect>
</property>
Expand Down Expand Up @@ -39,6 +39,24 @@
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="lbGray">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lbHist">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
Expand Down

0 comments on commit e0f1d0d

Please sign in to comment.