https://github.com/maximilianfeldthusen/facerecog
https://github.com/maximilianfeldthusen/facerecog
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/maximilianfeldthusen/facerecog
- Owner: maximilianfeldthusen
- License: bsd-3-clause
- Created: 2025-02-27T06:45:59.000Z (8 months ago)
- Default Branch: TFD
- Last Pushed: 2025-02-27T07:09:11.000Z (8 months ago)
- Last Synced: 2025-02-27T09:19:20.362Z (8 months ago)
- Language: C++
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Documentation
### faceRecog
This C++ code is for a simple face recognition application that uses the OpenCV library for image processing and the Caffe deep learning framework for running a trained neural network model. Let's break down the code step by step:
### Includes and Namespaces
```cpp
#include
#include
#include
#includeusing namespace std;
using namespace cv;
using namespace caffe;
```
- `#include `: Includes the standard input-output stream library for console output.
- `#include `: Includes the OpenCV library, which is used for image processing.
- `#include ` and `#include `: Include the Caffe library headers for working with neural networks.
- The `using namespace` statements allow for direct usage of standard library (std), OpenCV (cv), and Caffe (caffe) classes and functions without prefixing them.### FaceRecognition Class
```cpp
class FaceRecognition {
public:
FaceRecognition(const string& modelFile, const string& trainedFile) {
loadModel(modelFile, trainedFile);
}
```
- The `FaceRecognition` class encapsulates the functionality for face recognition.
- Its constructor takes two strings: `modelFile` (the architecture of the neural network) and `trainedFile` (the pre-trained weights).#### Method: recognizeFace
```cpp
void recognizeFace(const Mat& inputImage) {
Mat preprocessedImage = preprocessImage(inputImage);
Blob* inputBlob = net_->input_blobs()[0];if (preprocessedImage.channels() != 3) {
cerr << "Error: Preprocessed image must have 3 channels." << endl;
return;
}if (preprocessedImage.size() != Size(inputBlob->width(), inputBlob->height())) {
cerr << "Error: Preprocessed image dimensions do not match model input size." << endl;
return;
}
```
- `recognizeFace` takes an input image and preprocesses it to be fed into the neural network.
- `preprocessedImage` is obtained by calling `preprocessImage(inputImage)`.
- The function checks if the image has 3 channels (for RGB) and if its dimensions match the expected input size of the neural network.```cpp
Mat resizedImage;
preprocessedImage.convertTo(resizedImage, CV_32F, 1.0 / 255);
inputBlob->Reshape(1, 3, inputBlob->height(), inputBlob->width());
net_->Reshape();
inputBlob->set_cpu_data(resizedImage.ptr());
```
- The image is converted to a floating-point type and scaled to the range [0, 1].
- The input blob (data structure used by Caffe to hold input data) is reshaped to accommodate the input image.
- The preprocessed image data is copied into the input blob.```cpp
net_->Forward();
// Further processing of the output can be done here
Blob* outputBlob = net_->output_blobs()[0];
// Process outputBlob as needed
}
```
- The neural network performs a forward pass with `net_->Forward()`, which computes the output based on the input.
- The output blob is obtained, which contains the results of the network's inference (not explicitly processed in this code).#### Method: loadModel
```cpp
private:
shared_ptr> net_;void loadModel(const string& modelFile, const string& trainedFile) {
try {
net_.reset(new Net(modelFile, TEST));
net_->CopyTrainedLayersFrom(trainedFile);
} catch (const std::exception& e) {
cerr << "Error loading model from " << modelFile << " and " << trainedFile << ": " << e.what() << endl;
}
}
```
- `loadModel` initializes the Caffe network using the model and trained files.
- A shared pointer to a `Net` object is used to manage the network's lifecycle.
- It catches any exceptions that occur during model loading, printing an error message if necessary.#### Method: preprocessImage
```cpp
Mat preprocessImage(const Mat& image) {
Mat rgbImage;
cvtColor(image, rgbImage, COLOR_BGR2RGB);
Mat resizedImage;
resize(rgbImage, resizedImage, Size(224, 224));
return resizedImage;
}
};
```
- `preprocessImage` converts the input image from BGR (OpenCV's default) to RGB and resizes it to 224x224 pixels, a common input size for many deep learning models.### Main Function
```cpp
int main(int argc, char** argv) {
if (argc != 3) {
cerr << "Usage: " << argv[0] << " " << endl;
return -1;
}string modelFile = argv[1];
string trainedFile = argv[2];FaceRecognition faceRecognition(modelFile, trainedFile);
Mat inputImage = imread("path_to_image.jpg");if (inputImage.empty()) {
cerr << "Error: Could not read the image." << endl;
return -1;
}faceRecognition.recognizeFace(inputImage);
return 0;
}
```
- The `main` function checks for the correct number of command line arguments (model file and trained file).
- It initializes the `FaceRecognition` object with the provided files.
- It attempts to read an image (the path should be replaced with a valid image path).
- If the image is read successfully, it calls `recognizeFace` to process the image and recognize faces.### Summary
This code serves as a basic framework for face recognition using a pre-trained model in Caffe. It includes necessary preprocessing steps, model loading, and inference, but lacks detailed implementation for processing the output of the model and handling the results of face recognition.