Spaces:
Sleeping
Sleeping
import gradio as gr | |
from keras.preprocessing import image | |
from keras.applications.vgg16 import preprocess_input | |
import numpy as np | |
from keras.models import load_model | |
import cv2 as cv | |
# Load the trained model | |
model = load_model('FaceAuthenticator.keras') | |
# Load the pre-trained face detection model with error handling | |
face_cascade = cv.CascadeClassifier('hass_face.xml') | |
# Define a function to preprocess the input image | |
def preprocess_image(image_path): | |
img = cv.imread(image_path) | |
img = cv.resize(img, (224, 224)) | |
img = cv.cvtColor(img, cv.COLOR_BGR2RGB) | |
img_array = np.expand_dims(img, axis=0) | |
img_array = preprocess_input(img_array) | |
return img_array | |
# Define a function to classify the input image | |
def classify_image(image_data): | |
try: | |
# Save the uploaded image temporarily | |
temp_image_path = "temp_image.jpg" | |
image_data.save(temp_image_path) | |
# Preprocess the image | |
img_array = preprocess_image(temp_image_path) | |
# Convert the image to grayscale | |
gray_image = cv.cvtColor(cv.imread(temp_image_path), cv.COLOR_BGR2GRAY) | |
# Detect faces in the image | |
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) | |
# Check if any faces were detected | |
if len(faces) == 0: | |
return "No faces detected in the input image." | |
else: | |
# Make predictions | |
prediction = model.predict(img_array) | |
# Return the prediction | |
if prediction[0][0] > 0.5: | |
return "The image is classified as real." | |
else: | |
return "The image is classified as fake." | |
except Exception as e: | |
return f"An error occurred: {str(e)}" | |
# Create the Gradio interface | |
demo = gr.Interface( | |
fn=classify_image, | |
inputs=gr.Image(type="pil", label="Upload Image"), | |
outputs=gr.Textbox(label="Prediction"), | |
title="DeepFake Detection for Facial images", | |
description="Upload an Facial image and the model will classify it as real or fake.", | |
theme="default", | |
) | |
# Launch the Gradio app | |
demo.launch() |