28 lines
862 B
Python
28 lines
862 B
Python
# Programmin for Face detection
|
|
# Haar Cascade
|
|
import cv2
|
|
import numpy as np
|
|
|
|
# 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("PhotoTest.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
|
|
face = face_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=5)
|
|
print(face)
|
|
# Dessiner un rectangle autour de chaque visage détecté
|
|
for x, y, w, h in face:
|
|
img = cv2.rectangle(img, (int(x*1.2), int(y*1.15)),
|
|
(x + int(w*0.7), y + int(h*0.2)), (0, 255, 0), 3)
|
|
|
|
# Afficher l'image
|
|
cv2.imshow("Faces", img)
|
|
cv2.waitKey(0)
|