import gradio as gr from tensorflow.keras.models import model_from_json import numpy as np import cv2 # Load the face detection model face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Load the anti-spoofing model structure and weights with open('antispoofing_model.json', 'r') as json_file: loaded_model_json = json_file.read() model = model_from_json(loaded_model_json) model.load_weights('antispoofing_model.h5') # Define the prediction function def predict(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: face = image[y:y+h, x:x+w] face = cv2.resize(face, (160, 160)) face = face.astype('float') / 255.0 face = np.expand_dims(face, axis=0) pred = model.predict(face) label = 'Spoof' if pred > 0.5 else 'Real' cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) return image # Create the Gradio interface iface = gr.Interface(fn=predict, inputs="image", outputs="image") # Run the interface iface.launch()