File size: 2,141 Bytes
835e1c4
1f10db7
7428897
835e1c4
1f10db7
835e1c4
 
 
295d2fc
835e1c4
 
3dad336
835e1c4
 
 
 
 
 
 
 
 
 
 
3dad336
 
 
 
 
835e1c4
3dad336
 
835e1c4
3dad336
 
835e1c4
3dad336
 
835e1c4
3dad336
 
 
835e1c4
3dad336
 
 
 
 
 
 
 
 
 
835e1c4
 
 
 
3dad336
835e1c4
6c4efde
 
835e1c4
3dad336
835e1c4
 
 
1f10db7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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()