File size: 2,946 Bytes
086e367
f6ee5ca
 
 
086e367
 
f6ee5ca
086e367
c09345c
086e367
 
 
f6ee5ca
086e367
ce23e3a
f6ee5ca
 
 
086e367
f6ee5ca
ce23e3a
 
f6ee5ca
ce23e3a
 
086e367
 
 
f6ee5ca
086e367
f6ee5ca
c09345c
 
 
 
f6ee5ca
ce23e3a
f6ee5ca
 
 
 
086e367
f6ee5ca
ce23e3a
 
 
f6ee5ca
 
 
ce23e3a
 
 
f6ee5ca
ce23e3a
 
 
 
f6ee5ca
 
086e367
 
 
ce23e3a
 
 
f6ee5ca
ce23e3a
 
 
f6ee5ca
ce23e3a
f6ee5ca
ce23e3a
 
c09345c
ce23e3a
f6ee5ca
 
ce23e3a
086e367
c09345c
086e367
f6ee5ca
ce23e3a
c09345c
ce23e3a
 
c09345c
ce23e3a
 
086e367
 
 
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
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
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import cv2
import numpy as np
import torch
from transformers import AutoImageProcessor, SiglipForImageClassification
from PIL import Image
import matplotlib.pyplot as plt

# Load model and processor
model_name = "prithivMLmods/deepfake-detector-model-v1"
processor = AutoImageProcessor.from_pretrained(model_name)
model = SiglipForImageClassification.from_pretrained(model_name)
model.eval()

# Load face detector
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

def analyze_deepfake(video_path):
    cap = cv2.VideoCapture(video_path)
    frame_preds = []
    real_count = 0
    fake_count = 0
    frame_count = 0
    max_frames = 40
    frame_skip = 5

    while True:
        ret, frame = cap.read()
        if not ret or frame_count >= max_frames:
            break

        if frame_count % frame_skip != 0:
            frame_count += 1
            continue

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_detector.detectMultiScale(gray, 1.1, 4)

        for (x, y, w, h) in faces:
            face = frame[y:y+h, x:x+w]
            if face.size == 0:
                continue

            rgb_face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(rgb_face)
            inputs = processor(images=image, return_tensors="pt")

            with torch.no_grad():
                logits = model(**inputs).logits
                probs = torch.softmax(logits, dim=-1)[0]
                fake_prob = probs[1].item()
                frame_preds.append(fake_prob)

                if fake_prob > 0.6:
                    fake_count += 1
                else:
                    real_count += 1

        frame_count += 1

    cap.release()

    # Decision logic
    if real_count + fake_count == 0:
        result = "❌ No faces detected. Try a clearer video."
    else:
        final_verdict = "FAKE" if fake_count > real_count else "REAL"
        confidence = np.mean(frame_preds)
        result = f"🎯 Result: **{final_verdict}** (Avg Confidence = {confidence:.2f}, Real = {real_count}, Fake = {fake_count})"

    # Graph
    fig, ax = plt.subplots(figsize=(6, 4))
    ax.hist(frame_preds, bins=10, color="green" if real_count > fake_count else "red", edgecolor="black")
    ax.set_title("Fake Confidence per Face")
    ax.set_xlabel("Confidence (0 = Real, 1 = Fake)")
    ax.set_ylabel("Count")
    ax.grid(True)

    return result, fig

# Gradio UI
demo = gr.Interface(
    fn=analyze_deepfake,
    inputs=gr.Video(label="πŸ“€ Upload your .mp4 video (under 100MB)"),
    outputs=[
        gr.Markdown(label="πŸ“Š Deepfake Detection Result"),
        gr.Plot(label="πŸ“ˆ Confidence Distribution")
    ],
    title="🎭 Deepfake Video Detector (Accurate & Fast)",
    description="This model detects faces in video frames and classifies each as REAL or FAKE using a fine-tuned vision transformer."
)

demo.launch()