Spaces:
Sleeping
Sleeping
import os | |
import numpy as np | |
import cv2 | |
from mtcnn import MTCNN | |
from PIL import Image | |
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 tensorflow.keras.preprocessing.image import img_to_array | |
from huggingface_hub import hf_hub_download | |
# 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") | |
model_xcp = load_model(xcp_path) | |
model_eff = load_model(eff_path) | |
# Face detector | |
detector = MTCNN() | |
# Prediction function | |
def predict_image(image_path): | |
img = cv2.imread(image_path) | |
if img is None: | |
return {"error": "Image could not be loaded"} | |
results = [] | |
faces = detector.detect_faces(img) | |
# === Single or no face === | |
if len(faces) <= 1: | |
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
img_xcp = xcp_pre(np.expand_dims(cv2.resize(img_rgb, (299, 299)), axis=0)) | |
img_eff = eff_pre(np.expand_dims(cv2.resize(img_rgb, (224, 224)), axis=0)) | |
xcp_pred = model_xcp.predict(img_xcp)[0][0] | |
eff_pred = model_eff.predict(img_eff)[0][0] | |
final_score = (xcp_pred + eff_pred) / 2 | |
label = "REAL" if final_score > 0.5 else "FAKE" | |
results.append({"face_id": "whole image", "label": label, "score": round(float(final_score), 3)}) | |
else: | |
for idx, face in enumerate(faces): | |
x, y, w, h = face['box'] | |
x, y = max(0, x), max(0, y) | |
cropped = img[y:y+h, x:x+w] | |
if cropped.shape[0] < 60 or cropped.shape[1] < 60: | |
continue | |
face_rgb = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB) | |
img_xcp = xcp_pre(np.expand_dims(cv2.resize(face_rgb, (299, 299)), axis=0)) | |
img_eff = eff_pre(np.expand_dims(cv2.resize(face_rgb, (224, 224)), axis=0)) | |
xcp_pred = model_xcp.predict(img_xcp)[0][0] | |
eff_pred = model_eff.predict(img_eff)[0][0] | |
final_score = (xcp_pred + eff_pred) / 2 | |
label = "REAL" if final_score > 0.5 else "FAKE" | |
results.append({"face_id": f"face_{idx+1}", "label": label, "score": round(float(final_score), 3)}) | |
return results | |