File size: 1,549 Bytes
0077a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
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
import gradio as gr
from model.load_model import load_model
from utils.video_utils import extract_frames
from utils.face_utils import extract_faces
from predictor.predict import predict_faces
from utils.gradcam import get_gradcam, get_conv_layers
import numpy as np
from PIL import Image
from tqdm import tqdm

model = load_model()
conv_layer_names = get_conv_layers(model)  # populate dropdown choices

def deepfake_app(video, selected_layer, progress=gr.Progress(track_tqdm=True)):
    frames = extract_frames(video)
    frames = list(frames)

    faces = extract_faces(frames)
    faces = list(progress.tqdm(faces, desc="Detecting faces"))

    if not faces:
        return "No face detected", None

    predictions = predict_faces(model, faces)
    predictions = list(progress.tqdm(predictions, desc="Running predictions"))

    avg_score = np.mean(predictions)
    label = "FAKE" if avg_score > 0.5 else "REAL"

    max_idx = np.argmax(predictions)
    cam_image = get_gradcam(model, faces[max_idx], selected_layer)
    cam_image = Image.fromarray(cam_image)

    return label, cam_image

gr.Interface(
    fn=deepfake_app,
    inputs=[gr.Video(label="Upload a Video"),
            gr.Dropdown(choices=conv_layer_names, label="Grad-CAM Layer", value=conv_layer_names[-1])
        ],
    outputs=["text", "image"],
    title="Deepfake Detection with XceptionNet",
    description="Upload a video, and the model will predict if it contains a deepfake with RAI explainability using GRADCAM."
).launch()