Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,29 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
import gradio as gr
|
4 |
-
|
|
|
5 |
from tensorflow.keras.models import load_model
|
6 |
-
import
|
7 |
|
8 |
-
|
|
|
9 |
|
|
|
10 |
def predict_image(img):
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
prediction = np.argmax(model.predict(x), axis=1)
|
20 |
-
|
21 |
-
if prediction == 0:
|
22 |
-
return 'Real Image'
|
23 |
else:
|
24 |
-
return
|
25 |
-
|
26 |
-
# Define the Gradio Interface with the desired title and description
|
27 |
|
|
|
28 |
description_html = """
|
29 |
<p>Upload a face image to check if it's real or morphed with deepfake</p>
|
30 |
"""
|
@@ -33,10 +32,11 @@ custom_css = """
|
|
33 |
div {background-color: whitesmoke;}
|
34 |
"""
|
35 |
|
|
|
36 |
gr.Interface(
|
37 |
fn=predict_image,
|
38 |
-
inputs=
|
39 |
-
outputs=
|
40 |
title="Deepfake Image Detection",
|
41 |
description=description_html,
|
42 |
allow_flagging='never'
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.utils import img_to_array
|
6 |
|
7 |
+
# Load the pre-trained model
|
8 |
+
model = load_model('deepfake_detection_mobilenet_model.h5')
|
9 |
|
10 |
+
# Define the function for prediction
|
11 |
def predict_image(img):
|
12 |
+
# Resize and preprocess the input image
|
13 |
+
x = cv2.resize(img, (224, 224))
|
14 |
+
x = img_to_array(x) / 255.0 # Normalize the image
|
15 |
+
x = np.expand_dims(x, axis=0) # Add batch dimension
|
16 |
|
17 |
+
# Predict with the model
|
18 |
+
prediction = (model.predict(x) > 0.5).astype("int32")[0][0]
|
19 |
|
20 |
+
# Return result based on the prediction
|
21 |
+
if prediction == 1:
|
22 |
+
return "Fake Image"
|
|
|
|
|
|
|
|
|
23 |
else:
|
24 |
+
return "Real Image"
|
|
|
|
|
25 |
|
26 |
+
# Define the Gradio Interface
|
27 |
description_html = """
|
28 |
<p>Upload a face image to check if it's real or morphed with deepfake</p>
|
29 |
"""
|
|
|
32 |
div {background-color: whitesmoke;}
|
33 |
"""
|
34 |
|
35 |
+
# Create the Gradio app interface
|
36 |
gr.Interface(
|
37 |
fn=predict_image,
|
38 |
+
inputs=gr.Image(type="numpy", label="Upload Face Image"),
|
39 |
+
outputs=gr.Textbox(label="Prediction"),
|
40 |
title="Deepfake Image Detection",
|
41 |
description=description_html,
|
42 |
allow_flagging='never'
|