face detecting ai in python

Here's a basic code block for detecting faces using OpenCV in Python:

main.py
import cv2

# Load the pre-trained cascade classifier
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Load the image
image = cv2.imread('image.jpg')

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

# Detect faces in the image
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)

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

# Display the image with faces detected
cv2.imshow('Faces Detected', image)
cv2.waitKey(0)
595 chars
22 lines

This code uses the Haar Cascade classifier for face detection, which is included in the OpenCV library. It detects faces in the given image and draws green rectangles around them.

gistlibby LogSnag