Skip to content

Commit

Permalink
adding face recognition class
Browse files Browse the repository at this point in the history
  • Loading branch information
youssef-shaban committed May 11, 2023
1 parent 3345e94 commit 5142371
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 24 deletions.
16 changes: 8 additions & 8 deletions CV-Toolbox.pro
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ FORMS += \



INCLUDEPATH += C:\opencv\build\include
INCLUDEPATH += C:\Users\youss\Documents\libraries\opencv\build\include

LIBS += C:\opencv\release\bin\libopencv_core470.dll
LIBS += C:\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\opencv\release\bin\libopencv_calib3d470.dll
LIBS += C:\opencv\release\bin\libopencv_objdetect470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_core470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_calib3d470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_objdetect470.dll


# Default rules for deployment.
Expand Down
78 changes: 68 additions & 10 deletions CV/face_recognition.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
#include "face_recognition.h"

void face_recognition:: performPCA(cv::Mat& dataPoints, cv::Mat& eigenvalues, cv::Mat& eigenvectors, cv:: Mat& convertedData,int numComponents)
void face_recognition::setModel(std::string filePath, cv::Mat images, std::vector<std::string> labels)
{
cv::FileStorage fs(filePath, cv::FileStorage::READ);
fs["eigenValues"] >> eigenValues;
fs["eigenVectors"] >> eigenVectors;
fs.release();
this->labels = labels;
this->images = images;
cv::Mat tempVectors = eigenVectors.rowRange(0, this->n_component);
multiplyEigen(tempVectors, images, this->transformedImages);

}

void face_recognition::train(cv::Mat images, std::vector<std::string> labels)
{
performPCA(images);
this->labels = labels;
this->images = images;
cv::Mat tempVectors = eigenVectors.rowRange(0, this->n_component);
multiplyEigen(tempVectors, images, this->transformedImages);

}

void face_recognition::setNComponent(int n_component)
{
this->n_component = n_component;
cv::Mat tempVectors = eigenVectors.rowRange(0, this->n_component);
multiplyEigen(tempVectors, this->images, this->transformedImages);
}

std::string face_recognition::getPerson(cv::Mat image)
{

cv::Mat reshapedImage = image.reshape(1, 1);
reshapedImage.convertTo(reshapedImage, CV_32F);
cv::Mat tempEigen = eigenVectors.rowRange(0, n_component);
cv::Mat output;
multiplyEigen(tempEigen, reshapedImage, output);
return labels[getNearest(this->transformedImages, output)];


}

void face_recognition:: performPCA(cv::Mat& dataPoints)
{
cv::Mat mean;
cv::reduce(dataPoints, mean, 0, cv::REDUCE_AVG);
Expand All @@ -11,16 +54,31 @@ void face_recognition:: performPCA(cv::Mat& dataPoints, cv::Mat& eigenvalues, cv
cv::Mat covariance;
cv::mulTransposed(dataPoints, covariance, true);
covariance = covariance / (dataPoints.rows - 1);
cv::eigen(covariance, eigenvalues, eigenvectors);
eigenvalues=eigenvalues.rowRange(0, numComponents);
eigenvectors = eigenvectors.rowRange(0, numComponents);
convertedData = dataPoints * eigenvectors.t();
cv::eigen(covariance, this->eigenValues, this->eigenVectors);



}

void face_recognition:: multiplyEigen(cv::Mat& eigenvectors, cv::Mat& image, cv::Mat& result)
void face_recognition:: multiplyEigen(cv::Mat& eigenvectors, cv::Mat& images, cv::Mat& result)
{
cv::Mat reshapedImage = image.reshape(1, 1);
cv::Mat floatImage;
reshapedImage.convertTo(floatImage, CV_32F);
result = floatImage * eigenvectors.t();

result = images * eigenvectors.t();
}

int face_recognition::getNearest(cv::Mat images, cv::Mat image) {
cv::Mat distances= cv::Mat(images.rows, 1, CV_32F);

for (int row = 0; row < images.rows; row++) {
double distance = 0;
for (int col = 0; col < images.cols; col++) {
distance += (images.at<float>(row, col) - image.at<float>(col)) * (images.at<float>(row, col) - image.at<float>(col));
}
distances.at<float>(row) = distance;
}

double minValue;
cv::Point minLocation;
cv::minMaxLoc(distances, &minValue, nullptr, &minLocation, nullptr);
return minLocation.y;
}
14 changes: 12 additions & 2 deletions CV/face_recognition.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ class face_recognition
{
public:
face_recognition();
void setModel(std::string filePath, cv::Mat images, std::vector<std::string> labels);
void train(cv::Mat images, std::vector<std::string> labels);
void setNComponent(int n_component);
std::string getPerson(cv::Mat image);

private:
void performPCA(cv::Mat& dataPoints, cv::Mat& eigenvalues, cv::Mat& eigenvectors, cv:: Mat& convertedData,int numComponents);
void multiplyEigen(cv::Mat& eigenvectors, cv::Mat& image, cv::Mat& result);
void performPCA(cv::Mat& dataPoints);
void multiplyEigen(cv::Mat& eigenvectors,cv::Mat& images, cv::Mat& result);
int getNearest(cv::Mat images, cv::Mat image);
cv::Mat eigenValues, eigenVectors, transformedImages, images;
std::vector<std::string> labels;
int n_component = 100;



};
Expand Down
2 changes: 1 addition & 1 deletion CV/faces_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void faces_detection::detect_faces(cv::Mat& inputImage, cv::Mat& outputImage, bo
{
// Load the cascade classifier
cv::CascadeClassifier face_cascade;
face_cascade.load("C://Users/kamel/OneDrive/Documents/GitKraken/CV-Toolbox/models/haarcascade_frontalface_default.xml");
face_cascade.load("D:\\Projects\\CV\\Cv-Toolbox\\models\\haarcascade_frontalface_default.xml");

cv::Mat gray;
cvtColor(inputImage, gray, cv::COLOR_BGR2GRAY);
Expand Down
1 change: 1 addition & 0 deletions CV/faces_detection.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>
#include <filesystem>
#include <opencv2/opencv.hpp>
#include <opencv2/objdetect.hpp>

class faces_detection
{
Expand Down
2 changes: 0 additions & 2 deletions pages/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class MainWindow : public QMainWindow
~MainWindow();


private slots:
void on_pushButton_11_clicked();

private:
Ui::MainWindow *ui;
Expand Down
1 change: 0 additions & 1 deletion pages/page11.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class page11 : public QWidget
private slots:
void on_inputImageLabel_clicked();

void on_outputImageLabel_clicked();

void on_applyButton_clicked();

Expand Down
1 change: 1 addition & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<file>resources/handpointing.png</file>
<file>resources/textcursor.png</file>
<file>style/Cstartpage.qss</file>
<file>models/haarcascade_frontalface_default.xml</file>
</qresource>
<qresource prefix="/icons">
<file>resources/icons8-upload-32.png</file>
Expand Down

0 comments on commit 5142371

Please sign in to comment.