-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_detector.cpp
92 lines (78 loc) · 3.19 KB
/
face_detector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <opencv2/opencv.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
#include <filesystem>
#include <string>
namespace fs = std::filesystem;
void detectAndSaveFaces(const std::string& imagePath, const std::string& outputDir, cv::CascadeClassifier& faceCascade) {
cv::Mat img = cv::imread(imagePath);
if (img.empty()) {
std::cerr << "Could not read the image: " << imagePath << std::endl;
return;
}
std::vector<cv::Rect> faces;
cv::Mat imgGray;
cv::cvtColor(img, imgGray, cv::COLOR_BGR2GRAY);
cv::equalizeHist(imgGray, imgGray);
faceCascade.detectMultiScale(imgGray, faces);
if (faces.empty()) {
std::cout << "No faces found in " << imagePath << std::endl;
return;
}
std::string frameDir = outputDir + "/" + fs::path(imagePath).stem().string();
fs::create_directories(frameDir);
for (size_t i = 0; i < faces.size(); ++i) {
cv::Mat face = img(faces[i]);
std::string facePath = frameDir + "/face_" + std::to_string(i+1) + ".jpg";
if (!cv::imwrite(facePath, face)) {
std::cerr << "Error saving face image: " << facePath << std::endl;
}
}
}
int main(int argc, char** argv) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <input_directory> <output_directory>" << std::endl;
return 1;
}
std::string inputDir = argv[1];
std::string outputDir = argv[2];
std::cout << "Input directory: " << inputDir << std::endl;
std::cout << "Output directory: " << outputDir << std::endl;
cv::CascadeClassifier faceCascade;
std::string cascadePath = "/home/kali/project/haarcascade_frontalface_alt.xml";
std::cout << " XML PATH: " << cascadePath << std::endl;
if (!faceCascade.load(cascadePath)) {
std::cerr << "Could not load face cascade." << std::endl;
return -1;
}
// Check if the input directory exists
if (!fs::exists(inputDir) || !fs::is_directory(inputDir)) {
std::cerr << "Input directory does not exist or is not a directory: " << inputDir << std::endl;
return -1;
}
// Check if the output directory exists
if (!fs::exists(outputDir) || !fs::is_directory(outputDir)) {
std::cerr << "Output directory does not exist or is not a directory: " << outputDir << std::endl;
return -1;
}
try {
std::cout << "Reading files from directory: " << inputDir << std::endl;
// Iterate over all files in the input directory
for (const auto& entry : fs::directory_iterator(inputDir)) {
if (entry.is_regular_file() && entry.path().extension() == ".jpg") {
std::cout << "Processing file: " << entry.path() << std::endl;
detectAndSaveFaces(entry.path().string(), outputDir, faceCascade);
}
}
} catch (const fs::filesystem_error& ex) {
std::cerr << "Filesystem error: " << ex.what() << std::endl;
return -1;
} catch (const cv::Exception& ex) {
std::cerr << "OpenCV exception: " << ex.what() << std::endl;
return -1;
} catch (const std::exception& ex) {
std::cerr << "Standard exception: " << ex.what() << std::endl;
return -1;
}
return 0;
}