Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,10 +9,9 @@ from tensorflow.keras.applications.efficientnet import preprocess_input as eff_p
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
from mtcnn import MTCNN
|
| 11 |
|
| 12 |
-
#
|
| 13 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
|
| 14 |
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
|
| 15 |
-
|
| 16 |
xcp_model = load_model(xcp_path)
|
| 17 |
eff_model = load_model(eff_path)
|
| 18 |
|
|
@@ -23,37 +22,43 @@ def extract_face(image):
|
|
| 23 |
faces = detector.detect_faces(image)
|
| 24 |
if not faces:
|
| 25 |
return None
|
| 26 |
-
x, y, w, h = faces[0][
|
| 27 |
x, y = max(0, x), max(0, y)
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
def predict(image):
|
| 31 |
face = extract_face(image)
|
| 32 |
if face is None:
|
| 33 |
-
return "No face detected"
|
| 34 |
|
| 35 |
-
# Xception
|
| 36 |
xcp_img = cv2.resize(face, (299, 299))
|
| 37 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
| 38 |
-
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0)[0]
|
| 39 |
|
| 40 |
-
# EfficientNet
|
| 41 |
eff_img = cv2.resize(face, (224, 224))
|
| 42 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
| 43 |
-
eff_pred = eff_model.predict(eff_tensor, verbose=0)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
avg_pred = (xcp_pred + eff_pred) / 2.0
|
| 47 |
-
label = "Fake" if avg_pred > 0.5 else "Real"
|
| 48 |
-
return label
|
| 49 |
|
| 50 |
-
# Gradio interface
|
| 51 |
interface = gr.Interface(
|
| 52 |
fn=predict,
|
| 53 |
-
inputs=gr.Image(type="numpy", label="Upload
|
| 54 |
-
outputs=
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
interface.launch()
|
|
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
from mtcnn import MTCNN
|
| 11 |
|
| 12 |
+
# Download and load models
|
| 13 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
|
| 14 |
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
|
|
|
|
| 15 |
xcp_model = load_model(xcp_path)
|
| 16 |
eff_model = load_model(eff_path)
|
| 17 |
|
|
|
|
| 22 |
faces = detector.detect_faces(image)
|
| 23 |
if not faces:
|
| 24 |
return None
|
| 25 |
+
x, y, w, h = faces[0]['box']
|
| 26 |
x, y = max(0, x), max(0, y)
|
| 27 |
+
face = image[y:y+h, x:x+w]
|
| 28 |
+
return face
|
| 29 |
|
| 30 |
def predict(image):
|
| 31 |
face = extract_face(image)
|
| 32 |
if face is None:
|
| 33 |
+
return "No face detected", None
|
| 34 |
|
| 35 |
+
# Prepare for Xception
|
| 36 |
xcp_img = cv2.resize(face, (299, 299))
|
| 37 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
| 38 |
+
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 39 |
|
| 40 |
+
# Prepare for EfficientNet
|
| 41 |
eff_img = cv2.resize(face, (224, 224))
|
| 42 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
| 43 |
+
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 44 |
+
|
| 45 |
+
# Ensemble average
|
| 46 |
+
avg_pred = (xcp_pred + eff_pred) / 2
|
| 47 |
+
|
| 48 |
+
# ✅ Important fix: if label "real" = 1, fake = 0, prediction > 0.5 = real
|
| 49 |
+
label = "Real" if avg_pred > 0.5 else "Fake"
|
| 50 |
|
| 51 |
+
return label, face
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
interface = gr.Interface(
|
| 54 |
fn=predict,
|
| 55 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Label(label="Prediction"),
|
| 58 |
+
gr.Image(type="numpy", label="Detected Face")
|
| 59 |
+
],
|
| 60 |
+
title="Deepfake Image Detector (Ensemble: Xception + EfficientNetB4)",
|
| 61 |
+
description="Upload an image. The model detects the face, classifies it as real or fake using an ensemble of Xception and EfficientNetB4."
|
| 62 |
)
|
| 63 |
|
| 64 |
interface.launch()
|