Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,84 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Load
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
description="This model detects all faces in an image and classifies each one as real or fake using Xception and EfficientNetB4 ensemble."
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
interface.launch()
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from mtcnn import MTCNN
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
| 7 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
| 8 |
+
|
| 9 |
+
# Load models
|
| 10 |
+
xcp_model = load_model("xception_model.h5")
|
| 11 |
+
eff_model = load_model("efficientnet_model.h5")
|
| 12 |
+
|
| 13 |
+
# Load face detector
|
| 14 |
+
detector = MTCNN()
|
| 15 |
+
|
| 16 |
+
def expand_box(x, y, w, h, scale=1.5, img_shape=None):
|
| 17 |
+
"""Expand face bounding box with margin."""
|
| 18 |
+
cx, cy = x + w // 2, y + h // 2
|
| 19 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
| 20 |
+
x1 = max(0, cx - new_w // 2)
|
| 21 |
+
y1 = max(0, cy - new_h // 2)
|
| 22 |
+
x2 = min(img_shape[1], cx + new_w // 2)
|
| 23 |
+
y2 = min(img_shape[0], cy + new_h // 2)
|
| 24 |
+
return x1, y1, x2, y2
|
| 25 |
+
|
| 26 |
+
def predict(image):
|
| 27 |
+
faces = detector.detect_faces(image)
|
| 28 |
+
if not faces:
|
| 29 |
+
return "No faces detected", image
|
| 30 |
+
|
| 31 |
+
results = []
|
| 32 |
+
annotated = image.copy()
|
| 33 |
+
|
| 34 |
+
for i, face in enumerate(faces):
|
| 35 |
+
x, y, w, h = face['box']
|
| 36 |
+
x, y, w, h = max(0, x), max(0, y), w, h
|
| 37 |
+
x1, y1, x2, y2 = expand_box(x, y, w, h, scale=1.6, img_shape=image.shape)
|
| 38 |
+
|
| 39 |
+
face_crop = image[y1:y2, x1:x2]
|
| 40 |
+
|
| 41 |
+
# Preprocess for each model
|
| 42 |
+
xcp_img = cv2.resize(face_crop, (299, 299))
|
| 43 |
+
eff_img = cv2.resize(face_crop, (224, 224))
|
| 44 |
+
|
| 45 |
+
xcp_tensor = xcp_pre(xcp_img.astype(np.float32))[np.newaxis, ...]
|
| 46 |
+
eff_tensor = eff_pre(eff_img.astype(np.float32))[np.newaxis, ...]
|
| 47 |
+
|
| 48 |
+
xcp_pred = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
| 49 |
+
eff_pred = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
| 50 |
+
avg_pred = (xcp_pred + eff_pred) / 2
|
| 51 |
+
label = "Real" if avg_pred > 0.5 else "Fake"
|
| 52 |
+
|
| 53 |
+
results.append(
|
| 54 |
+
f"Face {i+1}: {label} (Avg: {avg_pred:.3f}, XCP: {xcp_pred:.3f}, EFF: {eff_pred:.3f})"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Draw
|
| 58 |
+
color = (0, 255, 0) if label == "Real" else (255, 0, 0)
|
| 59 |
+
cv2.rectangle(annotated, (x1, y1), (x2, y2), color, 2)
|
| 60 |
+
cv2.putText(
|
| 61 |
+
annotated,
|
| 62 |
+
f"{label} ({avg_pred:.2f})",
|
| 63 |
+
(x1, y1 - 10),
|
| 64 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 65 |
+
0.6,
|
| 66 |
+
color,
|
| 67 |
+
2,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
return "\n".join(results), annotated
|
| 71 |
+
|
| 72 |
+
# Gradio Interface
|
| 73 |
+
interface = gr.Interface(
|
| 74 |
+
fn=predict,
|
| 75 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 76 |
+
outputs=[
|
| 77 |
+
gr.Textbox(label="Predictions"),
|
| 78 |
+
gr.Image(type="numpy", label="Annotated Image"),
|
| 79 |
+
],
|
| 80 |
+
title="Deepfake Detector (Multi-Face Ensemble)",
|
| 81 |
+
description="Detects all faces in an image and classifies each one as real or fake using Xception and EfficientNetB4 ensemble.",
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|