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 |
#include <opencv2/opencv.hpp> #include <iostream> int main() { // Load the pre-trained Haar Cascade for face detection cv::CascadeClassifier faceCascade; if (!faceCascade.load("haarcascade_frontalface_default.xml")) { std::cerr << "Error loading Haar Cascade file!" << std::endl; return -1; } // Open a video capture from the default camera cv::VideoCapture cap(0); if (!cap.isOpened()) { std::cerr << "Error opening video capture!" << std::endl; return -1; } cv::Mat frame; std::vector<cv::Rect> faces; while (true) { // Capture a new frame from the video feed cap >> frame; if (frame.empty()) { std::cerr << "Error capturing frame!" << std::endl; break; } // Convert the frame to grayscale cv::Mat gray; cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY); // Detect faces in the frame faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, cv::Size(30, 30)); // Draw rectangles around detected faces for (const auto& face : faces) { cv::rectangle(frame, face, cv::Scalar(255, 0, 0), 2); } // Display the frame with detected faces cv::imshow("Object Detection", frame); // Exit loop when 'q' is pressed if (cv::waitKey(30) >= 0) { break; } } // Release video capture and close windows cap.release(); cv::destroyAllWindows(); return 0; } |
Explanation:
- Include OpenCV Header:
#include <opencv2/opencv.hpp>
: Includes the OpenCV core functionalities.
- Load Haar Cascade:
cv::CascadeClassifier faceCascade;
: Creates an object to load and use Haar cascade classifiers.faceCascade.load("haarcascade_frontalface_default.xml");
: Loads the pre-trained Haar cascade for face detection. Ensure that thehaarcascade_frontalface_default.xml
file is available in your working directory or provide the correct path.
- Capture Video:
cv::VideoCapture cap(0);
: Opens the default camera (usually the webcam).cap >> frame;
: Captures a frame from the video feed.
- Processing Frames:
- Convert to Grayscale:
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
converts the captured frame to grayscale as the face detection algorithm works on grayscale images. - Detect Faces:
faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, cv::Size(30, 30));
detects faces and stores their locations in thefaces
vector.
- Convert to Grayscale:
- Draw Rectangles:
cv::rectangle(frame, face, cv::Scalar(255, 0, 0), 2);
draws rectangles around detected faces in the frame using a blue color and a thickness of 2.
- Display the Frame:
cv::imshow("Object Detection", frame);
displays the frame with detected faces.
- Exit Loop:
if (cv::waitKey(30) >= 0) { break; }
waits for 30 milliseconds for a key press and exits the loop if any key is pressed.
- Cleanup:
cap.release();
releases the video capture object.cv::destroyAllWindows();
closes all OpenCV windows.
Prerequisites:
- OpenCV Library: Ensure OpenCV is installed and properly configured with your C++ project.
Haar Cascade File: Ensure you have the Haar cascade file (haarcascade_frontalface_default.xml
) available.
Compilation and Execution:
- Compile with OpenCV:
1 |
g++ -o object_detection object_detection.cpp `pkg-config --cflags --libs opencv4` |
- Run the Program:
1 |
./object_detection |