File size: 2,815 Bytes
086e367
f6ee5ca
 
 
086e367
 
 
61cca59
086e367
 
 
f6ee5ca
086e367
61cca59
f6ee5ca
 
61cca59
a540a43
 
 
 
 
f6ee5ca
ce23e3a
 
a540a43
 
 
086e367
 
 
a540a43
086e367
f6ee5ca
a540a43
 
c09345c
 
f6ee5ca
ce23e3a
f6ee5ca
a540a43
61cca59
f6ee5ca
 
086e367
f6ee5ca
a540a43
 
 
f6ee5ca
a540a43
f6ee5ca
 
a540a43
 
61cca59
a540a43
ce23e3a
 
 
f6ee5ca
a540a43
086e367
 
 
a540a43
 
 
 
 
 
 
61cca59
a540a43
 
 
 
 
61cca59
a540a43
 
61cca59
 
a540a43
 
61cca59
a540a43
61cca59
a540a43
 
 
61cca59
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

# βœ… 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()

# βœ… Haar cascade face detector
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

# βœ… Inference logic
def detect_deepfake(video):
    if video is None:
        return "❌ Please upload a valid MP4 video."

    cap = cv2.VideoCapture(video)
    frame_preds = []
    real_count = 0
    fake_count = 0
    frame_index = 0
    max_frames = 20
    frame_skip = 10

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

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

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

        if len(faces) > 0:
            x, y, w, h = faces[0]  # Only the first face for speed
            face = frame[y:y+h, x:x+w]
            if face.size == 0:
                continue

            face = cv2.resize(face, (224, 224))
            face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
            img = Image.fromarray(face_rgb)

            inputs = processor(images=img, return_tensors="pt")
            with torch.no_grad():
                logits = model(**inputs).logits
                prob = torch.softmax(logits, dim=-1)[0][1].item()
                frame_preds.append(prob)

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

        frame_index += 1

    cap.release()

    if not frame_preds:
        return "❌ No faces detected. Try a different video."

    avg_conf = np.mean(frame_preds)
    verdict = "FAKE" if fake_count > real_count else "REAL"

    return f"""
    βœ… **Final Result: {verdict}**
    🟒 Real Frames: {real_count}
    πŸ”΄ Fake Frames: {fake_count}
    πŸ“Š Avg Confidence: {avg_conf:.2f}
    """

# βœ… Gradio app using Blocks (queue-safe)
with gr.Blocks() as demo:
    gr.Markdown("## 🎭 Fast Deepfake Video Detector")
    gr.Markdown("Upload a short `.mp4` video (under 50MB). The model analyzes faces and detects if the video is REAL or FAKE.")

    with gr.Row():
        video_input = gr.Video(label="πŸ“€ Upload your video")
        result_output = gr.Markdown(label="🧠 Detection Result")

    analyze_btn = gr.Button("πŸ” Analyze Video")

    analyze_btn.click(fn=detect_deepfake, inputs=video_input, outputs=result_output)

demo.queue().launch()