Ex 4
Ex 4
# Load the Haar Cascade XML file for face detection (you can use different XML
files)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
1. Load Haar Cascade Classifier: Load the pre-trained Haar cascade XML file using
cv2.CascadeClassifier().
2. Load Image: Read the input image using cv2.imread().
3. Convert to Grayscale: Convert the image to grayscale using cv2.cvtColor() to
improve detection efficiency.
4. Detect Faces: Use detectMultiScale() to detect faces with parameters like
scaleFactor, minNeighbors, and minSize.
5. Draw Bounding Boxes: Loop through detected faces and draw rectangles using
cv2.rectangle().
6. Display Image: Show the processed image with detected faces using cv2.imshow().
7. Wait & Close: Use cv2.waitKey(0) and cv2.destroyAllWindows().