Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,88 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import cv2
|
4 |
-
import tensorflow as tf
|
5 |
-
from tensorflow.keras.models import load_model, Model
|
6 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
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 PIL import Image
|
10 |
-
import matplotlib.pyplot as plt
|
11 |
-
|
12 |
-
|
13 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import tensorflow as tf
|
5 |
+
from tensorflow.keras.models import load_model, Model
|
6 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
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 PIL import Image
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
|
14 |
+
xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="xception_model.h5")
|
15 |
+
eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector", filename="efficientnet_model.h5")
|
16 |
+
|
17 |
+
xcp_model = load_model(xcp_path)
|
18 |
+
eff_model = load_model(eff_path)
|
19 |
+
|
20 |
+
# Face detection
|
21 |
+
def detect_face_opencv(pil_image):
|
22 |
+
cv_img = np.array(pil_image.convert("RGB"))
|
23 |
+
cv_img = cv_img[:, :, ::-1] # RGB to BGR
|
24 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
25 |
+
gray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
|
26 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4)
|
27 |
+
if len(faces) == 0:
|
28 |
+
return pil_image # fallback
|
29 |
+
(x, y, w, h) = max(faces, key=lambda b: b[2]*b[3]) # largest face
|
30 |
+
return pil_image.crop((x, y, x+w, y+h))
|
31 |
+
|
32 |
+
# Grad-CAM
|
33 |
+
def grad_cam(model, img, size, preprocess_func):
|
34 |
+
img_resized = img.resize(size)
|
35 |
+
x = img_to_array(img_resized)
|
36 |
+
x = np.expand_dims(x, axis=0)
|
37 |
+
x = preprocess_func(x)
|
38 |
+
x_tensor = tf.convert_to_tensor(x)
|
39 |
+
|
40 |
+
grad_model = Model([model.inputs], [model.layers[-3].output, model.output])
|
41 |
+
with tf.GradientTape() as tape:
|
42 |
+
conv_outputs, predictions = grad_model(x_tensor)
|
43 |
+
loss = predictions[:, 0]
|
44 |
+
grads = tape.gradient(loss, conv_outputs)[0]
|
45 |
+
cam = np.mean(grads, axis=-1)
|
46 |
+
cam = np.maximum(cam, 0)
|
47 |
+
cam /= cam.max() if cam.max() != 0 else 1
|
48 |
+
heatmap = cv2.resize(cam.numpy(), (size[0], size[1]))
|
49 |
+
heatmap = np.uint8(255 * heatmap)
|
50 |
+
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
51 |
+
|
52 |
+
img_np = np.array(img_resized)
|
53 |
+
if img_np.shape[-1] == 4:
|
54 |
+
img_np = img_np[:, :, :3]
|
55 |
+
superimposed = cv2.addWeighted(img_np, 0.6, heatmap, 0.4, 0)
|
56 |
+
return Image.fromarray(cv2.cvtColor(superimposed, cv2.COLOR_BGR2RGB))
|
57 |
+
|
58 |
+
# Preprocessing
|
59 |
+
def preprocess(img, size, preprocess_func):
|
60 |
+
img = img.resize(size)
|
61 |
+
arr = img_to_array(img)
|
62 |
+
arr = np.expand_dims(arr, axis=0)
|
63 |
+
return preprocess_func(arr)
|
64 |
+
|
65 |
+
# Prediction logic
|
66 |
+
def predict(image):
|
67 |
+
face = detect_face_opencv(image)
|
68 |
+
|
69 |
+
xcp_input = preprocess(face, (299, 299), xcp_pre)
|
70 |
+
eff_input = preprocess(face, (224, 224), eff_pre)
|
71 |
+
|
72 |
+
xcp_pred = xcp_model.predict(xcp_input)[0][0]
|
73 |
+
eff_pred = eff_model.predict(eff_input)[0][0]
|
74 |
+
ensemble_prob = (xcp_pred + eff_pred) / 2
|
75 |
+
label = "REAL" if ensemble_prob > 0.5 else "FAKE"
|
76 |
+
|
77 |
+
cam_img = grad_cam(xcp_model, face, (299, 299), xcp_pre)
|
78 |
+
|
79 |
+
return f"{label} ({ensemble_prob:.2%} confidence)", cam_img
|
80 |
+
|
81 |
+
# Gradio interface
|
82 |
+
gr.Interface(
|
83 |
+
fn=predict,
|
84 |
+
inputs=gr.Image(type="pil"),
|
85 |
+
outputs=["text", "image"],
|
86 |
+
title="Deepfake Image Detector (with Grad-CAM)",
|
87 |
+
description="Upload an image. We detect the face, classify using an ensemble (Xception + EfficientNetB4), and explain the prediction with Grad-CAM."
|
88 |
+
).launch()
|