here’s the **error **im getting when trying to execute the python code
[ERROR:[email protected]] global persistence.cpp:519 cv::FileStorage::Impl::open Can't open file: 'models/haarcascade_frontalface_default.xml' in read mode
Traceback (most recent call last):
File "C:UsersRichard.JoyDesktopFinal-antispoofing_modelsanti.py", line 14, in <module>
model = model_from_json(loaded_model_json)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersRichard.JoyDesktopFinal-antispoofing_modelsantispoof_envLibsite-packageskerassrcmodelsmodel.py", line 575, in model_from_json
return serialization_lib.deserialize_keras_object(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersRichard.JoyDesktopFinal-antispoofing_modelsantispoof_envLibsite-packageskerassrcsavingserialization_lib.py", line 694, in deserialize_keras_object
cls = _retrieve_class_or_fn(
^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersRichard.JoyDesktopFinal-antispoofing_modelsantispoof_envLibsite-packageskerassrcsavingserialization_lib.py", line 812, in _retrieve_class_or_fn
raise TypeError(
TypeError: Could not locate class 'Functional'. Make sure custom classes are decorated with `@keras.saving.register_keras_serializable()`. Full object config: {'class_name': 'Functional', 'config': {'name': 'model', 'trainable': True,...(**contents of the json file**........'keras_version': '2.15.0', 'backend': 'tensorflow')
here are the library versions
keras 3.3.3
opencv-python 4.9.0.80
tensorflow 2.16.1
python 3.12.3
here’s the **code **im trying to execute
import cv2
import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array
import os
import numpy as np
root_dir = os.getcwd()
# Load Face Detection Model
face_cascade = cv2.CascadeClassifier("models/haarcascade_frontalface_default.xml")
# Load Anti-Spoofing Model graph
json_file = open('C:/Users/Richard.Joy/Desktop/Final-antispoofing_models/Antispoofing_model_mobilenet.json','r')
loaded_model_json = json_file.read()
json_file.close()
model = tf.keras.models.model_from_json(loaded_model_json)
# load antispoofing model weights
model.load_weights('C:/Users/Richard.Joy/Desktop/Final-antispoofing_models/finalyearproject_antispoofing_model_99-0.978947.h5')
print("Model loaded from disk")
video = cv2.VideoCapture(0)
while True:
try:
ret,frame = video.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
face = frame[y-5:y+h+5,x-5:x+w+5]
resized_face = cv2.resize(face,(160,160))
resized_face = resized_face.astype("float") / 255.0
resized_face = np.expand_dims(resized_face, axis=0)
# pass the face ROI through the trained liveness detector
# model to determine if the face is "real" or "fake"
preds = model.predict(resized_face)[0]
print(preds)
if preds> 0.5:
label = 'poof'
cv2.putText(frame, label, (x,y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
cv2.rectangle(frame, (x, y), (x+w,y+h),
(0, 0, 255), 2)
else:
label = 'eal'
cv2.putText(frame, label, (x,y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
cv2.rectangle(frame, (x, y), (x+w,y+h),
(0, 255, 0), 2)
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
except Exception as e:
pass
video.release()
cv2.destroyAllWindows()
Please let me know what im suppose to do (i tried to downgrade the libraries but was getting error in that too)
i tried to downgrade the libraries but was getting error in that too
Yassir Hiroli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.