TypeError: unhashable type: 'list'

import cv2
import numpy as np
import tensorflow as tf

Constants

IMAGE_SIZE = 224
gender_mapping = [“Male”, “Female”]

Load trained models

age_model = tf.keras.models.load_model(“Age-VGG16.keras”)
gender_model = tf.keras.models.load_model(“Gender-ResNet152.keras”)

Function to preprocess image

def preprocess_image(image):
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE))
image = np.array(image) / 255.0
image = np.expand_dims(image, axis=0)
return image

Function to predict age and gender from image

def predict_age_gender(image):
# Preprocess image
preprocessed_image = preprocess_image(image)

# Predict age
age_prediction = age_model.predict(preprocessed_image)[0][0]

# Predict gender
gender_prediction = gender_model.predict(preprocessed_image)[0][0]
gender_label = gender_mapping[int(round(gender_prediction))]

return int(round(age_prediction)), gender_label

Open webcam

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
if not ret:
print(“Error: Failed to capture image”)
break

# Flip frame horizontally for correct display
frame = cv2.flip(frame, 1)

# Draw rectangle around face
cv2.rectangle(frame, (150, 150), (500, 500), (255, 0, 0), 2)

# Crop face region
face_region = frame[150:500, 150:500]

# Predict age and gender
age, gender = predict_age_gender(face_region)

# Display age and gender on frame
cv2.putText(frame, f"Age: {age}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(frame, f"Gender: {gender}", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

# Display frame
cv2.imshow('Real-time Age and Gender Detection', frame)

# Break loop on 'q' press
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

Release webcam and close OpenCV windows

cap.release()
cv2.destroyAllWindows()

Error:
C:\Users\DELL\Desktop\Prediction>python detect.py
2024-04-28 19:41:08.676850: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
Traceback (most recent call last):
File “C:\Users\DELL\Desktop\Prediction\detect.py”, line 3, in
import tensorflow as tf
File “C:\Users\DELL\Desktop\Prediction\tfod\lib\site-packages\tensorflow_init_.py”, line 45, in
from tensorflow.api.v2 import internal
File "C:\Users\DELL\Desktop\Prediction\tfod\lib\site-packages\tensorflow_api\v2_internal
_init_.py", line 8, in
from tensorflow.api.v2.internal import autograph
File "C:\Users\DELL\Desktop\Prediction\tfod\lib\site-packages\tensorflow_api\v2_internal
\autograph_init_.py", line 8, in
from tensorflow.python.autograph.core.ag_ctx import control_status_ctx # line: 34
File “C:\Users\DELL\Desktop\Prediction\tfod\lib\site-packages\tensorflow\python\autograph\core\ag_ctx.py”, line 21, in
from tensorflow.python.autograph.utils import ag_logging
File “C:\Users\DELL\Desktop\Prediction\tfod\lib\site-packages\tensorflow\python\autograph\utils_init_.py”, line 17, in
from tensorflow.python.autograph.utils.context_managers import control_dependency_on_returns
File “C:\Users\DELL\Desktop\Prediction\tfod\lib\site-packages\tensorflow\python\autograph\utils\context_managers.py”, line 19, in
from tensorflow.python.framework import ops
File “C:\Users\DELL\Desktop\Prediction\tfod\lib\site-packages\tensorflow\python\framework\ops.py”, line 5906, in
) → Optional[Callable[[Any], message.Message]]:
File “C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\typing.py”, line 243, in inner
return func(*args, **kwds)
File “C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\typing.py”, line 316, in getitem
return self._getitem(self, parameters)
File “C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\typing.py”, line 433, in Optional
return Union[arg, type(None)]
File “C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\typing.py”, line 243, in inner
return func(*args, **kwds)
File “C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\typing.py”, line 316, in getitem
return self._getitem(self, parameters)
File “C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\typing.py”, line 421, in Union
parameters = _remove_dups_flatten(parameters)
File “C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\typing.py”, line 215, in _remove_dups_flatten
all_params = set(params)
TypeError: unhashable type: 'list