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('fake_real_face_classification_model.keras') | |
# Load the pre-trained face detection model with error handling | |
face_cascade = cv.CascadeClassifier('img_for_deepfake_detection\\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_path): | |
# Preprocess the image | |
img_array = preprocess_image(image_path) | |
# Convert the image to grayscale | |
gray_image = cv.cvtColor(cv.imread(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." | |
# Create the Gradio interface | |
demo = gr.Interface( | |
fn=classify_image, | |
inputs=gr.Image(type="file", label="Upload Image"), | |
outputs=gr.Textbox(label="Prediction"), | |
title="DeepFake Image Detection", | |
description="Upload an image and the model will classify it as real or fake.", | |
theme="default", | |
layout="vertical", | |
css=""" | |
.gradio-container { | |
font-family: 'Roboto', sans-serif; | |
} | |
.gradio-input, .gradio-output { | |
border: 1px solid #ccc; | |
border-radius: 4px; | |
padding: 10px; | |
font-size: 16px; | |
} | |
.gradio-button { | |
background-color: #4CAF50; | |
color: white; | |
border: none; | |
border-radius: 4px; | |
padding: 10px 20px; | |
font-size: 16px; | |
cursor: pointer; | |
} | |
""" | |
) | |
# Launch the Gradio app | |
demo.launch() |