Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,18 +7,27 @@ 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 |
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
|
17 |
detector = MTCNN()
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def predict(image):
|
24 |
faces = detector.detect_faces(image)
|
@@ -27,32 +36,33 @@ def predict(image):
|
|
27 |
|
28 |
output_image = image.copy()
|
29 |
results = []
|
30 |
-
|
31 |
|
32 |
for idx, face in enumerate(faces):
|
33 |
-
conf = face.get("confidence", 0)
|
34 |
x, y, w, h = face['box']
|
35 |
-
|
36 |
-
# Filter out unclear faces
|
37 |
-
if w < MIN_FACE_SIZE or h < MIN_FACE_SIZE or conf < MIN_CONFIDENCE:
|
38 |
continue
|
39 |
|
40 |
-
img_h, img_w = image.shape[:2]
|
41 |
margin = 0.2
|
|
|
42 |
x = max(0, int(x - w * margin))
|
43 |
y = max(0, int(y - h * margin))
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
face_img = image[y:y2, x:x2]
|
48 |
|
49 |
-
|
|
|
|
|
|
|
|
|
50 |
face_xcp = cv2.resize(face_img, (299, 299))
|
51 |
face_eff = cv2.resize(face_img, (224, 224))
|
52 |
xcp_tensor = xcp_pre(face_xcp.astype(np.float32))[np.newaxis, ...]
|
53 |
eff_tensor = eff_pre(face_eff.astype(np.float32))[np.newaxis, ...]
|
54 |
|
55 |
-
# Predict
|
56 |
pred_xcp = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
57 |
pred_eff = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
58 |
avg = (pred_xcp + pred_eff) / 2
|
@@ -60,20 +70,19 @@ def predict(image):
|
|
60 |
label = "Real" if avg > 0.41 else "Fake"
|
61 |
color = (0, 255, 0) if label == "Real" else (0, 0, 255)
|
62 |
|
63 |
-
|
64 |
cv2.rectangle(output_image, (x, y), (x2, y2), color, 2)
|
65 |
cv2.putText(output_image, f"{label} ({avg:.2f})", (x, y - 10),
|
66 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
67 |
|
68 |
-
results.append(f"Face {
|
69 |
-
valid_faces += 1
|
70 |
|
71 |
-
if
|
72 |
-
return "No clear or confident face detected",
|
73 |
|
74 |
return "\n".join(results), output_image
|
75 |
|
76 |
-
# Interface
|
77 |
interface = gr.Interface(
|
78 |
fn=predict,
|
79 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
@@ -82,8 +91,9 @@ interface = gr.Interface(
|
|
82 |
gr.Image(type="numpy", label="Annotated Image"),
|
83 |
],
|
84 |
title="Deepfake Detector (Multi-Face Ensemble)",
|
85 |
-
description="Detects all
|
86 |
)
|
87 |
|
88 |
interface.launch()
|
89 |
|
|
|
|
7 |
from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
+
# Load models
|
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 |
+
# Filters
|
20 |
+
MIN_FACE_AREA = 6400 # 80x80 minimum face area
|
21 |
+
MIN_SHARPNESS = 20 # blur threshold
|
22 |
+
MIN_BRIGHTNESS = 30 # dark crop threshold
|
23 |
+
|
24 |
+
def is_blurry(image):
|
25 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
26 |
+
return cv2.Laplacian(gray, cv2.CV_64F).var() < MIN_SHARPNESS
|
27 |
+
|
28 |
+
def is_dark(image):
|
29 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
30 |
+
return np.mean(gray) < MIN_BRIGHTNESS
|
31 |
|
32 |
def predict(image):
|
33 |
faces = detector.detect_faces(image)
|
|
|
36 |
|
37 |
output_image = image.copy()
|
38 |
results = []
|
39 |
+
face_count = 0
|
40 |
|
41 |
for idx, face in enumerate(faces):
|
|
|
42 |
x, y, w, h = face['box']
|
43 |
+
if w * h < MIN_FACE_AREA:
|
|
|
|
|
44 |
continue
|
45 |
|
|
|
46 |
margin = 0.2
|
47 |
+
img_h, img_w = image.shape[:2]
|
48 |
x = max(0, int(x - w * margin))
|
49 |
y = max(0, int(y - h * margin))
|
50 |
+
w = int(w * (1 + 2 * margin))
|
51 |
+
h = int(h * (1 + 2 * margin))
|
52 |
+
x2 = min(img_w, x + w)
|
53 |
+
y2 = min(img_h, y + h)
|
54 |
face_img = image[y:y2, x:x2]
|
55 |
|
56 |
+
if face_img.shape[0] < 40 or face_img.shape[1] < 40:
|
57 |
+
continue
|
58 |
+
if is_blurry(face_img) or is_dark(face_img):
|
59 |
+
continue
|
60 |
+
|
61 |
face_xcp = cv2.resize(face_img, (299, 299))
|
62 |
face_eff = cv2.resize(face_img, (224, 224))
|
63 |
xcp_tensor = xcp_pre(face_xcp.astype(np.float32))[np.newaxis, ...]
|
64 |
eff_tensor = eff_pre(face_eff.astype(np.float32))[np.newaxis, ...]
|
65 |
|
|
|
66 |
pred_xcp = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0]
|
67 |
pred_eff = eff_model.predict(eff_tensor, verbose=0).flatten()[0]
|
68 |
avg = (pred_xcp + pred_eff) / 2
|
|
|
70 |
label = "Real" if avg > 0.41 else "Fake"
|
71 |
color = (0, 255, 0) if label == "Real" else (0, 0, 255)
|
72 |
|
73 |
+
face_count += 1
|
74 |
cv2.rectangle(output_image, (x, y), (x2, y2), color, 2)
|
75 |
cv2.putText(output_image, f"{label} ({avg:.2f})", (x, y - 10),
|
76 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
77 |
|
78 |
+
results.append(f"Face {face_count}: {label} (Avg: {avg:.3f}, XCP: {pred_xcp:.3f}, EFF: {pred_eff:.3f})")
|
|
|
79 |
|
80 |
+
if not results:
|
81 |
+
return "No clear or confident face detected", output_image
|
82 |
|
83 |
return "\n".join(results), output_image
|
84 |
|
85 |
+
# Gradio Interface
|
86 |
interface = gr.Interface(
|
87 |
fn=predict,
|
88 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
|
|
91 |
gr.Image(type="numpy", label="Annotated Image"),
|
92 |
],
|
93 |
title="Deepfake Detector (Multi-Face Ensemble)",
|
94 |
+
description="Detects all confident faces and classifies each one as real or fake using Xception and EfficientNetB4 ensemble."
|
95 |
)
|
96 |
|
97 |
interface.launch()
|
98 |
|
99 |
+
|