Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ from tensorflow.keras.models import load_model
|
|
| 7 |
from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
| 8 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 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")
|
|
@@ -15,52 +14,32 @@ eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", fi
|
|
| 15 |
xcp_model = load_model(xcp_path)
|
| 16 |
eff_model = load_model(eff_path)
|
| 17 |
|
| 18 |
-
# Face detector
|
| 19 |
-
detector = MTCNN()
|
| 20 |
-
|
| 21 |
-
def extract_face(image):
|
| 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 |
-
return image[y:y+h, x:x+w]
|
| 28 |
-
|
| 29 |
def predict(image):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
# Xception
|
| 35 |
-
xcp_img = cv2.resize(face, (299, 299))
|
| 36 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
| 37 |
-
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 38 |
-
|
| 39 |
-
# EfficientNet
|
| 40 |
-
eff_img = cv2.resize(face, (224, 224))
|
| 41 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
|
|
|
|
|
|
| 42 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 43 |
|
| 44 |
-
# Ensemble
|
| 45 |
avg_pred = (xcp_pred + eff_pred) / 2
|
| 46 |
label = "Real" if avg_pred > 0.5 else "Fake"
|
| 47 |
|
| 48 |
-
# Log probabilities
|
| 49 |
-
print(f"Xception: {xcp_pred:.4f}, EfficientNetB4: {eff_pred:.4f}, Ensemble Avg: {avg_pred:.4f}")
|
| 50 |
-
|
| 51 |
-
# Return label with confidence
|
| 52 |
result = f"{label} (Avg: {avg_pred:.3f}, XCP: {xcp_pred:.3f}, EFF: {eff_pred:.3f})"
|
| 53 |
-
return result,
|
| 54 |
|
| 55 |
interface = gr.Interface(
|
| 56 |
fn=predict,
|
| 57 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 58 |
outputs=[
|
| 59 |
gr.Label(label="Prediction"),
|
| 60 |
-
gr.Image(type="numpy", label="
|
| 61 |
],
|
| 62 |
title="Deepfake Image Detector (Ensemble: Xception + EfficientNetB4)",
|
| 63 |
-
description="Upload
|
| 64 |
)
|
| 65 |
|
| 66 |
interface.launch()
|
|
|
|
| 7 |
from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
| 8 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 9 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 10 |
|
| 11 |
# Download and load models
|
| 12 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
|
|
|
|
| 14 |
xcp_model = load_model(xcp_path)
|
| 15 |
eff_model = load_model(eff_path)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def predict(image):
|
| 18 |
+
# Use the full image directly (no face extraction)
|
| 19 |
+
xcp_img = cv2.resize(image, (299, 299))
|
| 20 |
+
eff_img = cv2.resize(image, (224, 224))
|
| 21 |
|
|
|
|
|
|
|
| 22 |
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
| 24 |
+
|
| 25 |
+
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 26 |
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 27 |
|
|
|
|
| 28 |
avg_pred = (xcp_pred + eff_pred) / 2
|
| 29 |
label = "Real" if avg_pred > 0.5 else "Fake"
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
result = f"{label} (Avg: {avg_pred:.3f}, XCP: {xcp_pred:.3f}, EFF: {eff_pred:.3f})"
|
| 32 |
+
return result, image
|
| 33 |
|
| 34 |
interface = gr.Interface(
|
| 35 |
fn=predict,
|
| 36 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 37 |
outputs=[
|
| 38 |
gr.Label(label="Prediction"),
|
| 39 |
+
gr.Image(type="numpy", label="Input Image")
|
| 40 |
],
|
| 41 |
title="Deepfake Image Detector (Ensemble: Xception + EfficientNetB4)",
|
| 42 |
+
description="Upload a full image. The model classifies it as real or fake using an ensemble of Xception and EfficientNetB4."
|
| 43 |
)
|
| 44 |
|
| 45 |
interface.launch()
|