Cpp/face_detection.cpp
2026-05-17 02:56:32 +03:00

53 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
std::cout << "Не удалось открыть камеру\n";
return -1;
}
cv::CascadeClassifier face_cascade;
std::string path = "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml";
if (!face_cascade.load(path)) {
std::cout << "Ошибка загрузки каскада\n";
return -1;
}
cv::Mat frame, gray;
while (true) {
cap >> frame;
if (frame.empty()) break;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
std::vector<cv::Rect> faces;
face_cascade.detectMultiScale(
gray,
faces,
1.1,
5,
0,
cv::Size(30, 30)
);
for (const auto &f : faces) {
cv::rectangle(frame, f, cv::Scalar(0, 255, 0), 2);
}
cv::imshow("Face Detection", frame);
if (cv::waitKey(1) == 27) break;
}
cap.release();
cv::destroyAllWindows();
return 0;
}