Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import numpy as np
|
|
4 |
import torch
|
5 |
from transformers import AutoImageProcessor, SiglipForImageClassification
|
6 |
from PIL import Image
|
7 |
-
import
|
8 |
|
9 |
# Load model and processor
|
10 |
model_name = "prithivMLmods/deepfake-detector-model-v1"
|
@@ -12,82 +12,82 @@ processor = AutoImageProcessor.from_pretrained(model_name)
|
|
12 |
model = SiglipForImageClassification.from_pretrained(model_name)
|
13 |
model.eval()
|
14 |
|
15 |
-
#
|
16 |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
frame_preds = []
|
21 |
real_count = 0
|
22 |
fake_count = 0
|
23 |
-
|
24 |
-
max_frames =
|
25 |
-
frame_skip =
|
26 |
|
27 |
while True:
|
28 |
ret, frame = cap.read()
|
29 |
-
if not ret or
|
30 |
break
|
31 |
|
32 |
-
if
|
33 |
-
|
34 |
continue
|
35 |
|
36 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
37 |
faces = face_detector.detectMultiScale(gray, 1.1, 4)
|
38 |
|
39 |
-
|
|
|
40 |
face = frame[y:y+h, x:x+w]
|
41 |
if face.size == 0:
|
42 |
continue
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
|
|
48 |
with torch.no_grad():
|
49 |
logits = model(**inputs).logits
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
if fake_prob > 0.6:
|
55 |
fake_count += 1
|
56 |
else:
|
57 |
real_count += 1
|
58 |
|
59 |
-
|
60 |
|
61 |
cap.release()
|
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 |
-
description="This model detects faces in video frames and classifies each as REAL or FAKE using a fine-tuned vision transformer."
|
91 |
-
)
|
92 |
-
|
93 |
-
demo.launch()
|
|
|
4 |
import torch
|
5 |
from transformers import AutoImageProcessor, SiglipForImageClassification
|
6 |
from PIL import Image
|
7 |
+
import os
|
8 |
|
9 |
# Load model and processor
|
10 |
model_name = "prithivMLmods/deepfake-detector-model-v1"
|
|
|
12 |
model = SiglipForImageClassification.from_pretrained(model_name)
|
13 |
model.eval()
|
14 |
|
15 |
+
# Haar face detector
|
16 |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
17 |
|
18 |
+
# Inference function
|
19 |
+
def detect_deepfake(video):
|
20 |
+
if video is None:
|
21 |
+
return "β Please upload a valid MP4 video."
|
22 |
+
|
23 |
+
cap = cv2.VideoCapture(video)
|
24 |
frame_preds = []
|
25 |
real_count = 0
|
26 |
fake_count = 0
|
27 |
+
frame_index = 0
|
28 |
+
max_frames = 20
|
29 |
+
frame_skip = 10
|
30 |
|
31 |
while True:
|
32 |
ret, frame = cap.read()
|
33 |
+
if not ret or frame_index >= max_frames:
|
34 |
break
|
35 |
|
36 |
+
if frame_index % frame_skip != 0:
|
37 |
+
frame_index += 1
|
38 |
continue
|
39 |
|
40 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
41 |
faces = face_detector.detectMultiScale(gray, 1.1, 4)
|
42 |
|
43 |
+
if len(faces) > 0:
|
44 |
+
x, y, w, h = faces[0] # Use only first detected face
|
45 |
face = frame[y:y+h, x:x+w]
|
46 |
if face.size == 0:
|
47 |
continue
|
48 |
|
49 |
+
face = cv2.resize(face, (224, 224))
|
50 |
+
face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
|
51 |
+
img = Image.fromarray(face_rgb)
|
52 |
|
53 |
+
inputs = processor(images=img, return_tensors="pt")
|
54 |
with torch.no_grad():
|
55 |
logits = model(**inputs).logits
|
56 |
+
prob = torch.softmax(logits, dim=-1)[0][1].item()
|
57 |
+
frame_preds.append(prob)
|
58 |
+
if prob > 0.6:
|
|
|
|
|
59 |
fake_count += 1
|
60 |
else:
|
61 |
real_count += 1
|
62 |
|
63 |
+
frame_index += 1
|
64 |
|
65 |
cap.release()
|
66 |
|
67 |
+
if not frame_preds:
|
68 |
+
return "β No faces detected. Try a different video."
|
69 |
+
|
70 |
+
avg_conf = np.mean(frame_preds)
|
71 |
+
verdict = "FAKE" if fake_count > real_count else "REAL"
|
72 |
+
|
73 |
+
return f"""
|
74 |
+
β
**Result: {verdict}**
|
75 |
+
π’ Real Frames: {real_count}
|
76 |
+
π΄ Fake Frames: {fake_count}
|
77 |
+
π Avg Confidence: {avg_conf:.2f}
|
78 |
+
"""
|
79 |
+
|
80 |
+
# Gradio app using Blocks (more stable)
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
gr.Markdown("## π Fast Deepfake Video Detector")
|
83 |
+
gr.Markdown("Upload a short .mp4 video (under 50MB). Model will analyze and classify it as REAL or FAKE based on face analysis.")
|
84 |
+
|
85 |
+
with gr.Row():
|
86 |
+
video_input = gr.Video(label="π€ Upload your video")
|
87 |
+
result_output = gr.Markdown(label="π§ Deepfake Detection Result")
|
88 |
+
|
89 |
+
analyze_btn = gr.Button("Analyze Video")
|
90 |
+
|
91 |
+
analyze_btn.click(fn=detect_deepfake, inputs=video_input, outputs=result_output)
|
92 |
+
|
93 |
+
demo.queue(concurrency_count=1).launch()
|
|
|
|
|
|
|
|