import cv2 import numpy as np import gradio as gr from mtcnn import MTCNN from tensorflow.keras.models import load_model from tensorflow.keras.applications.xception import preprocess_input as xcp_pre from tensorflow.keras.applications.efficientnet import preprocess_input as eff_pre from huggingface_hub import hf_hub_download # Download and load models xcp_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="xception_model.h5") eff_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/deepfake-image-detector_final", filename="efficientnet_model.h5") xcp_model = load_model(xcp_path) eff_model = load_model(eff_path) # Load face detector detector = MTCNN() # Detection thresholds MIN_FACE_SIZE = 60 # Accept faces larger than 60×60 pixels MIN_CONFIDENCE = 0.94 # Accept only confident detections def predict(image): faces = detector.detect_faces(image) if not faces: return "No face detected", image output_image = image.copy() results = [] valid_faces = 0 for idx, face in enumerate(faces): conf = face.get("confidence", 0) x, y, w, h = face['box'] # Filter out unclear faces if w < MIN_FACE_SIZE or h < MIN_FACE_SIZE or conf < MIN_CONFIDENCE: continue img_h, img_w = image.shape[:2] margin = 0.2 x = max(0, int(x - w * margin)) y = max(0, int(y - h * margin)) x2 = min(img_w, x + int(w * (1 + 2 * margin))) y2 = min(img_h, y + int(h * (1 + 2 * margin))) face_img = image[y:y2, x:x2] # Resize and preprocess face_xcp = cv2.resize(face_img, (299, 299)) face_eff = cv2.resize(face_img, (224, 224)) xcp_tensor = xcp_pre(face_xcp.astype(np.float32))[np.newaxis, ...] eff_tensor = eff_pre(face_eff.astype(np.float32))[np.newaxis, ...] # Predict pred_xcp = xcp_model.predict(xcp_tensor, verbose=0).flatten()[0] pred_eff = eff_model.predict(eff_tensor, verbose=0).flatten()[0] avg = (pred_xcp + pred_eff) / 2 label = "Real" if avg > 0.41 else "Fake" color = (0, 255, 0) if label == "Real" else (0, 0, 255) # Draw on image cv2.rectangle(output_image, (x, y), (x2, y2), color, 2) cv2.putText(output_image, f"{label} ({avg:.2f})", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) results.append(f"Face {idx+1}: {label} (Avg: {avg:.3f}, XCP: {pred_xcp:.3f}, EFF: {pred_eff:.3f})") valid_faces += 1 if valid_faces == 0: return "No clear or confident face detected", image return "\n".join(results), output_image # Interface interface = gr.Interface( fn=predict, inputs=gr.Image(type="numpy", label="Upload Image"), outputs=[ gr.Textbox(label="Predictions"), gr.Image(type="numpy", label="Annotated Image"), ], title="Deepfake Detector (Multi-Face Ensemble)", description="Detects all visible faces in an image and classifies each as Real or Fake using Xception and EfficientNetB4 ensemble.", ) interface.launch()