-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
67 lines (50 loc) · 1.58 KB
/
main.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
#include <opencv2/opencv.hpp>
int main() {
// Open the video file
cv::VideoCapture cap;
cap.open("</path/to/video>");
// Check if the video file was successfully opened
if (!cap.isOpened()) {
std::cerr << "Error: Failed to open video file" << std::endl;
return -1;
}
// Create a window to display the video frames
cv::namedWindow("Video", cv::WINDOW_NORMAL);
// Initialize the face detector
cv::CascadeClassifier face_detector;
bool is_success = face_detector.load("</path/to/haarcascade_frontalface_alt.xml>");
if (!is_success)
{
return 0;
}
cv::Mat frame;
std::vector<cv::Rect> faces;
// Loop through the frames of the video
while (true) {
// Read a frame from the video
cap.read(frame);
// Check if the frame was successfully read
if (frame.empty()) {
break;
}
// Detect faces in the input image
faces.clear();
face_detector.detectMultiScale(frame, faces);
// Draw rectangles around the detected faces
for (size_t i = 0; i < faces.size(); i++) {
rectangle(frame, faces[i], cv::Scalar(0, 255, 0), 2);
}
// Display the frame in the window
cv::imshow("Video", frame);
// Wait for a key press
int key = cv::waitKey(30);
// Exit the loop if the 'q' key is pressed
if (key == 'q') {
break;
}
}
// Release the video file and destroy the window
cap.release();
cv::destroyAllWindows();
return 0;
}