A Python script that uses the OpenCV library to perform real-time face detection from a webcam feed. It captures video from the default camera (index 0) and highlights detected faces with rectangles. Here's an overview of the code:
The code starts by importing two essential libraries: pathlib and cv2 (OpenCV). pathlib is used for working with file paths, and cv2 is OpenCV for computer vision tasks.
It determines the path to the Haar Cascade classifier XML file for face detection. This XML file contains the pre-trained model for detecting faces.
The script creates a cv2.VideoCapture object named camera to capture video from the default camera (camera index 0).
The code sets up a full-screen window for displaying the video feed: It uses the cv2.namedWindow function to create a named window called "Faces." It sets the window properties to make it full-screen using cv2.setWindowProperty.
The main part of the code is a continuous loop that captures video frames, performs face detection, and displays the video feed with rectangles drawn around detected faces.
It reads a frame from the camera using camera.read(). Converts the frame to grayscale using cv2.cvtColor for efficient face detection. Uses the detectMultiScale method of the Cascade Classifier (clf) to find faces in the grayscale frame. It specifies parameters like scaling factor, minimum neighbors, and minimum size for detected objects. It loops through the detected faces, drawing rectangles around them using cv2.rectangle. The processed frame is displayed in the full-screen window named "Faces" using cv2.imshow.
The code continuously checks for user input. If the "q" key is pressed (detected by cv2.waitKey(1)), the loop breaks, and the program exits.
After breaking out of the loop, the script releases the camera capture using camera.release() to free the camera resource. It also closes all OpenCV windows using cv2.destroyAllWindows().
The result of running this code is a full-screen window displaying the live video feed from your camera, with faces highlighted by rectangles. You can exit the program by pressing the "q" key. This code provides a basic implementation of real-time face detection and can serve as a starting point for more advanced face recognition or tracking applications.