snipplets.dev/snipplets/code/Python/opencv/face-detect.py

25 lines
550 B
Python
Executable File

#!/usr/bin/env python3
import cv2
img = cv2.imread('face.png')
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_classifier = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)
face = face_classifier.detectMultiScale(
gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40)
)
for x, y, w, h in face:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('Face Detector', img_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()