27 lines
774 B
Python
27 lines
774 B
Python
# Programmin for Face detection
|
|
# Haar Cascade
|
|
import cv2
|
|
|
|
# Charger le classificateur Haar Cascade
|
|
face_cascade = cv2.CascadeClassifier("Haar_Cascade.xml")
|
|
|
|
# Charger l'image dans OpenCV
|
|
# Convertir l'image en niveaux de gris
|
|
img = cv2.imread("Image.jpg")
|
|
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
# Just to check if there is a photo
|
|
if gray_img.shape[0] == 0 or gray_img.shape[1] == 0:
|
|
print("Error: input image is empty")
|
|
|
|
# Détection des visages dans l'image
|
|
faces = face_cascade.detectMultiScale(
|
|
gray_img, scaleFactor=1.1, minNeighbors=5)
|
|
|
|
# Dessiner un rectangle autour de chaque visage détecté
|
|
for x, y, w, h in faces:
|
|
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
|
|
|
|
# Afficher l'image
|
|
cv2.imshow("Faces", img)
|
|
cv2.waitKey(0)
|