user
Create a Python script that utilizes OpenCV library to detect faces in an image, draw bounding boxes around them, and save the output image with detected faces highlighted.
PLAINFORMAT
assistant
import cv2

def detect_faces(image_path):
    # Load the image
    image = cv2.imread(image_path)

    # Convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Load the Haar Cascade classifier for detecting faces
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

    # Detect faces in the image
    faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)

    # Draw bounding boxes around detected faces
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Save the output image with detected faces highlighted
    output_image_path = "output_" + image_path.split("/")[-1]
    cv2.imwrite(output_image_path, image)
    print(f"Output image saved as {output_image_path}")

if __name__ == "__main__":
    import sys

    if len(sys.argv) != 2:
        print("Usage: python detect_faces.py <path_to_image>")
        sys.exit(1)

    image_path = sys.argv[1]
    detect_faces(image_path)