import cv2
import numpy as np
from tensorflow.python.keras.models import load_model
# Load the cascade
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
print("Face recognition model loaded")
# Load the pre-trained models for age and emotion detection
emotion_model = load_model("best_model.h5")
print("Emotion recognition model loaded")
age_model = load_model("age_detection_model.h5")
print("Age recognition model loaded")
# Define the labels for age and emotion classification
age_labels = [
"(0-2)",
"(4-6)",
"(8-12)",
"(15-20)",
"(25-32)",
"(38-43)",
"(48-53)",
"(60-100)",
]
emotion_labels = ["Angry", "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise"]
# To capture the video frame
cap = cv2.VideoCapture(0)
# Loop to detect and draw rectangle around face until key is pressed
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
# Draw rectangle around the faces and estimate age and emotion
for (x, y, w, h) in faces:
roi_gray = gray[y : y + h, x : x + w]
roi_color = img[y : y + h, x : x + w]
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
# Use the emotion model to predict the emotion
resized_img = cv2.resize(roi_gray, (48, 48))
resized_img = resized_img.astype("float") / 255.0
resized_img = np.reshape(resized_img, (1, 48, 48, 1))
prediction = emotion_model.predict(resized_img)
label = emotion_labels[prediction.argmax()]
cv2.putText(
img,
"Emotion: {}".format(label),
(x, y - 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 0, 255),
2,
)
# Use the age model to predict the age
resized_img = cv2.resize(roi_color, (224, 224))
resized_img = resized_img.astype("float") / 255.0
prediction = age_model.predict(resized_img.reshape(1, 224, 224, 3))
age = age_labels[int(prediction[0])]
cv2.putText(
img,
"Age: {}".format(age),
(x, y - 50),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 0, 255),
2,
)
# Display the output
cv2.imshow("Face Detection", img)
# Check for ESC key pressed to exit
if cv2.waitKey(1) == 27:
break
# Release the VideoCapture object
cap.release()
# Destroy all windows
cv2.destroyAllWindows()
C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\python.exe C:\Users\Ромэо\PycharmProjects\Scripts\huamn_recognize.py
Face recognition model loaded
Traceback (most recent call last):
File "C:\Users\Ромэо\PycharmProjects\Scripts\huamn_recognize.py", line 10, in <module>
emotion_model = load_model('best_model.h5')
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\saving\save.py", line 200, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects,
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 180, in load_model_from_hdf5
model = model_config_lib.model_from_config(model_config,
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\saving\model_config.py", line 52, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 118, in deserialize
return generic_utils.deserialize_keras_object(
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 674, in deserialize_keras_object
deserialized_obj = cls.from_config(
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 667, in from_config
input_tensors, output_tensors, created_layers = reconstruct_from_config(
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1278, in reconstruct_from_config
process_layer(layer_data)
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1260, in process_layer
layer = deserialize_layer(layer_data, custom_objects=custom_objects)
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 118, in deserialize
return generic_utils.deserialize_keras_object(
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 659, in deserialize_keras_object
(cls, cls_config) = class_and_config_for_serialized_keras_object(
File "C:\Users\Ромэо\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 556, in class_and_config_for_serialized_keras_object
raise ValueError(
ValueError: Unknown layer: BatchNormalization. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
Process finished with exit code 1
custom_objects
при загрузке модели.