Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -94,7 +94,7 @@ async def analyze_video(file: UploadFile = File(...)):
|
|
| 94 |
try:
|
| 95 |
# Extract faces
|
| 96 |
print("Extracting faces from video...")
|
| 97 |
-
faces = extract_faces_from_video(temp_video_path, max_frames=
|
| 98 |
|
| 99 |
if not faces:
|
| 100 |
return {
|
|
@@ -118,9 +118,15 @@ async def analyze_video(file: UploadFile = File(...)):
|
|
| 118 |
|
| 119 |
# For dima806/deepfake_vs_real_image_detection, Fake is index 1 and Real is index 0
|
| 120 |
fake_idx = 1
|
|
|
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
is_fake = avg_fake_prob > 0.5
|
| 126 |
|
|
@@ -139,8 +145,9 @@ async def analyze_video(file: UploadFile = File(...)):
|
|
| 139 |
"confidence": confidence,
|
| 140 |
"explanation": explanation,
|
| 141 |
"details": [
|
| 142 |
-
{"title": "Face Detection", "desc": f"
|
| 143 |
-
{"title": "Spatial Analysis", "desc": "Evaluated using a Vision Transformer (ViT) deep learning architecture."}
|
|
|
|
| 144 |
]
|
| 145 |
}
|
| 146 |
|
|
|
|
| 94 |
try:
|
| 95 |
# Extract faces
|
| 96 |
print("Extracting faces from video...")
|
| 97 |
+
faces = extract_faces_from_video(temp_video_path, max_frames=15)
|
| 98 |
|
| 99 |
if not faces:
|
| 100 |
return {
|
|
|
|
| 118 |
|
| 119 |
# For dima806/deepfake_vs_real_image_detection, Fake is index 1 and Real is index 0
|
| 120 |
fake_idx = 1
|
| 121 |
+
fake_probs = probabilities[:, fake_idx]
|
| 122 |
|
| 123 |
+
# Use Top-K Pooling: Analyze the most "fake" scoring frames
|
| 124 |
+
# Deepfakes often have glitches in a few frames. Average the worst 3 frames.
|
| 125 |
+
k = min(3, len(fake_probs))
|
| 126 |
+
top_k_probs, _ = torch.topk(fake_probs, k)
|
| 127 |
+
|
| 128 |
+
# Get average probability for 'Fake' class across the worst frames
|
| 129 |
+
avg_fake_prob = top_k_probs.mean().item()
|
| 130 |
|
| 131 |
is_fake = avg_fake_prob > 0.5
|
| 132 |
|
|
|
|
| 145 |
"confidence": confidence,
|
| 146 |
"explanation": explanation,
|
| 147 |
"details": [
|
| 148 |
+
{"title": "Face Detection", "desc": f"Extracted {len(faces)} key frames and isolated the subject's face using MTCNN."},
|
| 149 |
+
{"title": "Spatial Analysis", "desc": "Evaluated using a Vision Transformer (ViT) deep learning architecture."},
|
| 150 |
+
{"title": "Temporal Pooling", "desc": f"Applied Top-{k} analysis to identify and flag the highest-risk manipulated frames."}
|
| 151 |
]
|
| 152 |
}
|
| 153 |
|