SignalProjectFaceDetection/ProgrammingForFaceDetection.py

45 lines
1.2 KiB
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")
average_rgb = []
# 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)
b, g, r = cv2.split(img)
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)
cv2.waitKey(0)
# Calculate average on each channel
avg_b = np.mean(b) / 255
avg_g = np.mean(g) / 255
avg_r = np.mean(r) / 255
# Add to list
average_rgb.append([avg_b, avg_g, avg_r])
# frame_matrices.append(face)
print(average_rgb)