Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,74 +7,74 @@ from tensorflow.keras.applications.xception import preprocess_input as xcp_pre
|
|
7 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
-
|
11 |
-
# Load models
|
12 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5")
|
13 |
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="efficientnet_model.h5")
|
14 |
xcp_model = load_model(xcp_path)
|
15 |
eff_model = load_model(eff_path)
|
16 |
|
17 |
-
|
18 |
-
# Load face detector
|
19 |
detector = MTCNN()
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
x1 = max(0, cx - new_w // 2)
|
26 |
-
y1 = max(0, cy - new_h // 2)
|
27 |
-
x2 = min(img_shape[1], cx + new_w // 2)
|
28 |
-
y2 = min(img_shape[0], cy + new_h // 2)
|
29 |
-
return x1, y1, x2, y2
|
30 |
|
31 |
def predict(image):
|
32 |
faces = detector.detect_faces(image)
|
33 |
if not faces:
|
34 |
return "No face detected", image
|
35 |
|
36 |
-
|
37 |
results = []
|
38 |
|
39 |
for idx, face in enumerate(faces):
|
|
|
|
|
|
|
40 |
x, y, w, h = face['box']
|
|
|
|
|
41 |
|
42 |
-
#
|
43 |
margin = 0.2
|
44 |
img_h, img_w = image.shape[:2]
|
45 |
x = max(0, int(x - w * margin))
|
46 |
y = max(0, int(y - h * margin))
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
# Resize
|
55 |
-
|
56 |
-
|
57 |
-
xcp_tensor = xcp_pre(
|
58 |
-
eff_tensor = eff_pre(
|
59 |
-
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
label = "Real" if avg > 0.41 else "Fake"
|
66 |
color = (0, 255, 0) if label == "Real" else (0, 0, 255)
|
67 |
|
68 |
# Annotate image
|
69 |
-
cv2.rectangle(
|
70 |
-
cv2.putText(
|
71 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
72 |
|
73 |
-
results.append(f"Face {idx+1}: {label} (Avg: {
|
74 |
|
75 |
-
|
|
|
76 |
|
77 |
-
|
|
|
|
|
78 |
interface = gr.Interface(
|
79 |
fn=predict,
|
80 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
@@ -83,7 +83,8 @@ interface = gr.Interface(
|
|
83 |
gr.Image(type="numpy", label="Annotated Image"),
|
84 |
],
|
85 |
title="Deepfake Detector (Multi-Face Ensemble)",
|
86 |
-
description="Detects
|
87 |
)
|
88 |
|
89 |
interface.launch()
|
|
|
|
7 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
+
# Download models from Hugging Face
|
|
|
11 |
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5")
|
12 |
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="efficientnet_model.h5")
|
13 |
xcp_model = load_model(xcp_path)
|
14 |
eff_model = load_model(eff_path)
|
15 |
|
16 |
+
# Load MTCNN detector
|
|
|
17 |
detector = MTCNN()
|
18 |
|
19 |
+
# Parameters
|
20 |
+
MIN_FACE_SIZE = 100 # Skip small distant faces
|
21 |
+
MIN_CONFIDENCE = 0.97 # MTCNN face confidence
|
22 |
+
AVG_THRESHOLD = 0.41 # Decision boundary
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def predict(image):
|
25 |
faces = detector.detect_faces(image)
|
26 |
if not faces:
|
27 |
return "No face detected", image
|
28 |
|
29 |
+
output = image.copy()
|
30 |
results = []
|
31 |
|
32 |
for idx, face in enumerate(faces):
|
33 |
+
if face['confidence'] < MIN_CONFIDENCE:
|
34 |
+
continue
|
35 |
+
|
36 |
x, y, w, h = face['box']
|
37 |
+
if w < MIN_FACE_SIZE or h < MIN_FACE_SIZE:
|
38 |
+
continue
|
39 |
|
40 |
+
# Expand face bounding box by margin
|
41 |
margin = 0.2
|
42 |
img_h, img_w = image.shape[:2]
|
43 |
x = max(0, int(x - w * margin))
|
44 |
y = max(0, int(y - h * margin))
|
45 |
+
x2 = min(img_w, int(x + w * (1 + 2 * margin)))
|
46 |
+
y2 = min(img_h, int(y + h * (1 + 2 * margin)))
|
47 |
+
|
48 |
+
face_crop = image[y:y2, x:x2]
|
49 |
+
if face_crop.size == 0:
|
50 |
+
continue
|
51 |
+
|
52 |
+
# Resize and preprocess for both models
|
53 |
+
xcp_input = cv2.resize(face_crop, (299, 299)).astype(np.float32)
|
54 |
+
eff_input = cv2.resize(face_crop, (224, 224)).astype(np.float32)
|
55 |
+
xcp_tensor = xcp_pre(xcp_input)[np.newaxis, ...]
|
56 |
+
eff_tensor = eff_pre(eff_input)[np.newaxis, ...]
|
57 |
+
|
58 |
+
# Get predictions
|
59 |
+
xcp_score = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
60 |
+
eff_score = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
61 |
+
avg_score = (xcp_score + eff_score) / 2
|
62 |
+
label = "Real" if avg_score > AVG_THRESHOLD else "Fake"
|
|
|
63 |
color = (0, 255, 0) if label == "Real" else (0, 0, 255)
|
64 |
|
65 |
# Annotate image
|
66 |
+
cv2.rectangle(output, (x, y), (x2, y2), color, 2)
|
67 |
+
cv2.putText(output, f"{label} ({avg_score:.2f})", (x, y - 10),
|
68 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
69 |
|
70 |
+
results.append(f"Face {idx+1}: {label} (Avg: {avg_score:.3f}, XCP: {xcp_score:.3f}, EFF: {eff_score:.3f})")
|
71 |
|
72 |
+
if not results:
|
73 |
+
return "No clear or confident face detected", image
|
74 |
|
75 |
+
return "\n".join(results), output
|
76 |
+
|
77 |
+
# Gradio UI
|
78 |
interface = gr.Interface(
|
79 |
fn=predict,
|
80 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
|
|
83 |
gr.Image(type="numpy", label="Annotated Image"),
|
84 |
],
|
85 |
title="Deepfake Detector (Multi-Face Ensemble)",
|
86 |
+
description="Detects and classifies confident faces as Real or Fake using Xception + EfficientNetB4.",
|
87 |
)
|
88 |
|
89 |
interface.launch()
|
90 |
+
|