Spaces:
Sleeping
Sleeping
Prathmesh Patil
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from keras.preprocessing import image
|
| 3 |
+
from keras.applications.vgg16 import preprocess_input
|
| 4 |
+
import numpy as np
|
| 5 |
+
from keras.models import load_model
|
| 6 |
+
import cv2 as cv
|
| 7 |
+
|
| 8 |
+
# Load the trained model
|
| 9 |
+
model = load_model('fake_real_face_classification_model.keras')
|
| 10 |
+
|
| 11 |
+
# Load the pre-trained face detection model with error handling
|
| 12 |
+
face_cascade = cv.CascadeClassifier('img_for_deepfake_detection\\hass_face.xml')
|
| 13 |
+
|
| 14 |
+
# Define a function to preprocess the input image
|
| 15 |
+
def preprocess_image(image_path):
|
| 16 |
+
img = cv.imread(image_path)
|
| 17 |
+
img = cv.resize(img, (224, 224))
|
| 18 |
+
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
|
| 19 |
+
img_array = np.expand_dims(img, axis=0)
|
| 20 |
+
img_array = preprocess_input(img_array)
|
| 21 |
+
return img_array
|
| 22 |
+
|
| 23 |
+
# Define a function to classify the input image
|
| 24 |
+
def classify_image(image_path):
|
| 25 |
+
# Preprocess the image
|
| 26 |
+
img_array = preprocess_image(image_path)
|
| 27 |
+
|
| 28 |
+
# Convert the image to grayscale
|
| 29 |
+
gray_image = cv.cvtColor(cv.imread(image_path), cv.COLOR_BGR2GRAY)
|
| 30 |
+
|
| 31 |
+
# Detect faces in the image
|
| 32 |
+
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 33 |
+
|
| 34 |
+
# Check if any faces were detected
|
| 35 |
+
if len(faces) == 0:
|
| 36 |
+
return "No faces detected in the input image."
|
| 37 |
+
else:
|
| 38 |
+
# Make predictions
|
| 39 |
+
prediction = model.predict(img_array)
|
| 40 |
+
|
| 41 |
+
# Return the prediction
|
| 42 |
+
if prediction[0][0] > 0.5:
|
| 43 |
+
return "The image is classified as real."
|
| 44 |
+
else:
|
| 45 |
+
return "The image is classified as fake."
|
| 46 |
+
|
| 47 |
+
# Create the Gradio interface
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=classify_image,
|
| 50 |
+
inputs=gr.Image(type="file", label="Upload Image"),
|
| 51 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 52 |
+
title="DeepFake Image Detection",
|
| 53 |
+
description="Upload an image and the model will classify it as real or fake.",
|
| 54 |
+
theme="default",
|
| 55 |
+
layout="vertical",
|
| 56 |
+
css="""
|
| 57 |
+
.gradio-container {
|
| 58 |
+
font-family: 'Roboto', sans-serif;
|
| 59 |
+
}
|
| 60 |
+
.gradio-input, .gradio-output {
|
| 61 |
+
border: 1px solid #ccc;
|
| 62 |
+
border-radius: 4px;
|
| 63 |
+
padding: 10px;
|
| 64 |
+
font-size: 16px;
|
| 65 |
+
}
|
| 66 |
+
.gradio-button {
|
| 67 |
+
background-color: #4CAF50;
|
| 68 |
+
color: white;
|
| 69 |
+
border: none;
|
| 70 |
+
border-radius: 4px;
|
| 71 |
+
padding: 10px 20px;
|
| 72 |
+
font-size: 16px;
|
| 73 |
+
cursor: pointer;
|
| 74 |
+
}
|
| 75 |
+
"""
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Launch the Gradio app
|
| 79 |
+
demo.launch()
|