abo/cat_face_detector.py

28 lines
787 B
Python
Raw Normal View History

2025-07-16 15:34:54 +08:00
import cv2
# Load the Haar cascade classifier for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Load the image
img_path = 'path/to/your/cat/image.jpg' # Replace with the path to your cat image
img = cv2.imread(img_path)
if img is None:
print(f"Error: Could not load image at {img_path}")
exit()
# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the image with detected faces
cv2.imshow('Cat Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()