Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,7 +4,7 @@ import torch
|
|
| 4 |
import shutil
|
| 5 |
from fastapi import FastAPI, UploadFile, File
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
-
from transformers import
|
| 8 |
from facenet_pytorch import MTCNN
|
| 9 |
from PIL import Image
|
| 10 |
|
|
@@ -23,32 +23,31 @@ app.add_middleware(
|
|
| 23 |
print("Loading MTCNN Face Detector...")
|
| 24 |
mtcnn = MTCNN(keep_all=False, select_largest=True, post_process=False)
|
| 25 |
|
| 26 |
-
print("Loading Hugging Face Deepfake Detector...")
|
| 27 |
-
model_name = "
|
| 28 |
-
processor =
|
| 29 |
-
model =
|
| 30 |
|
| 31 |
# Ensure temp directory exists
|
| 32 |
os.makedirs("temp", exist_ok=True)
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
"""Extracts a
|
| 36 |
cap = cv2.VideoCapture(video_path)
|
| 37 |
frames_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 38 |
|
| 39 |
if frames_count == 0:
|
| 40 |
return []
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
faces = []
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
|
| 50 |
ret, frame = cap.read()
|
| 51 |
-
|
| 52 |
if not ret:
|
| 53 |
break
|
| 54 |
|
|
@@ -58,85 +57,83 @@ def extract_faces_from_video(video_path, max_frames=8):
|
|
| 58 |
|
| 59 |
# Detect and crop face
|
| 60 |
boxes, _ = mtcnn.detect(pil_img)
|
|
|
|
|
|
|
| 61 |
if boxes is not None and len(boxes) > 0:
|
| 62 |
box = boxes[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
if x2 > x1 and y2 > y1:
|
| 77 |
-
face_crop = pil_img.crop((x1, y1, x2, y2))
|
| 78 |
-
faces.append(face_crop)
|
| 79 |
-
|
| 80 |
-
current_frame += step
|
| 81 |
-
|
| 82 |
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
return faces
|
| 84 |
|
| 85 |
@app.post("/api/analyze")
|
| 86 |
async def analyze_video(file: UploadFile = File(...)):
|
| 87 |
print(f"Received file: {file.filename}")
|
| 88 |
|
| 89 |
-
# Save the uploaded file temporarily
|
| 90 |
temp_video_path = os.path.join("temp", file.filename)
|
| 91 |
with open(temp_video_path, "wb") as buffer:
|
| 92 |
shutil.copyfileobj(file.file, buffer)
|
| 93 |
|
| 94 |
try:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
faces =
|
| 98 |
|
| 99 |
if not faces:
|
| 100 |
return {
|
| 101 |
"isFake": False,
|
| 102 |
"confidence": 0,
|
| 103 |
-
"explanation": "Could not detect
|
| 104 |
"details": []
|
| 105 |
}
|
| 106 |
|
| 107 |
-
print(f"Extracted {len(faces)}
|
| 108 |
|
| 109 |
-
# Prepare for model
|
| 110 |
-
inputs = processor(
|
| 111 |
|
| 112 |
# Run inference
|
| 113 |
with torch.no_grad():
|
| 114 |
outputs = model(**inputs)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 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 |
-
#
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
top_k_probs, _ = torch.topk(fake_probs, k)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
is_fake = avg_fake_prob > 0.75
|
| 132 |
-
|
| 133 |
-
# Calculate confidence score based on the chosen class
|
| 134 |
-
if is_fake:
|
| 135 |
-
confidence = round(avg_fake_prob * 100, 2)
|
| 136 |
-
else:
|
| 137 |
-
confidence = round((1.0 - avg_fake_prob) * 100, 2)
|
| 138 |
|
| 139 |
-
explanation = "Our
|
| 140 |
|
| 141 |
print(f"Result: isFake={is_fake}, confidence={confidence}%")
|
| 142 |
|
|
@@ -145,9 +142,8 @@ async def analyze_video(file: UploadFile = File(...)):
|
|
| 145 |
"confidence": confidence,
|
| 146 |
"explanation": explanation,
|
| 147 |
"details": [
|
| 148 |
-
{"title": "
|
| 149 |
-
{"title": "
|
| 150 |
-
{"title": "Temporal Pooling", "desc": f"Applied Top-{k} analysis to identify and flag the highest-risk manipulated frames."}
|
| 151 |
]
|
| 152 |
}
|
| 153 |
|
|
@@ -156,10 +152,9 @@ async def analyze_video(file: UploadFile = File(...)):
|
|
| 156 |
return {"error": str(e)}
|
| 157 |
|
| 158 |
finally:
|
| 159 |
-
# Clean up temp file
|
| 160 |
if os.path.exists(temp_video_path):
|
| 161 |
os.remove(temp_video_path)
|
| 162 |
|
| 163 |
@app.get("/")
|
| 164 |
def health_check():
|
| 165 |
-
return {"status": "Backend is running!"}
|
|
|
|
| 4 |
import shutil
|
| 5 |
from fastapi import FastAPI, UploadFile, File
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from transformers import VideoMAEImageProcessor, VideoMAEForVideoClassification
|
| 8 |
from facenet_pytorch import MTCNN
|
| 9 |
from PIL import Image
|
| 10 |
|
|
|
|
| 23 |
print("Loading MTCNN Face Detector...")
|
| 24 |
mtcnn = MTCNN(keep_all=False, select_largest=True, post_process=False)
|
| 25 |
|
| 26 |
+
print("Loading Hugging Face Temporal Deepfake Detector (VideoMAE)...")
|
| 27 |
+
model_name = "Ammar2k/videomae-base-finetuned-deepfake-subset"
|
| 28 |
+
processor = VideoMAEImageProcessor.from_pretrained(model_name)
|
| 29 |
+
model = VideoMAEForVideoClassification.from_pretrained(model_name)
|
| 30 |
|
| 31 |
# Ensure temp directory exists
|
| 32 |
os.makedirs("temp", exist_ok=True)
|
| 33 |
|
| 34 |
+
def extract_faces_sequence(video_path, sequence_length=16):
|
| 35 |
+
"""Extracts a sequence of continuous frames and tracks the face temporally."""
|
| 36 |
cap = cv2.VideoCapture(video_path)
|
| 37 |
frames_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 38 |
|
| 39 |
if frames_count == 0:
|
| 40 |
return []
|
| 41 |
|
| 42 |
+
# Try to get frames from the middle of the video
|
| 43 |
+
start_frame = max(0, (frames_count // 2) - (sequence_length // 2))
|
| 44 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
|
| 45 |
|
| 46 |
faces = []
|
| 47 |
+
last_box = None
|
| 48 |
|
| 49 |
+
for _ in range(sequence_length):
|
|
|
|
| 50 |
ret, frame = cap.read()
|
|
|
|
| 51 |
if not ret:
|
| 52 |
break
|
| 53 |
|
|
|
|
| 57 |
|
| 58 |
# Detect and crop face
|
| 59 |
boxes, _ = mtcnn.detect(pil_img)
|
| 60 |
+
|
| 61 |
+
# Bounding box smoothing: fallback to last known box if detection fails on a frame
|
| 62 |
if boxes is not None and len(boxes) > 0:
|
| 63 |
box = boxes[0]
|
| 64 |
+
last_box = box
|
| 65 |
+
elif last_box is not None:
|
| 66 |
+
box = last_box
|
| 67 |
+
else:
|
| 68 |
+
continue # Skip if no face found yet
|
| 69 |
|
| 70 |
+
# Add 30% padding around the face for better context
|
| 71 |
+
w = box[2] - box[0]
|
| 72 |
+
h = box[3] - box[1]
|
| 73 |
+
pad_w = int(w * 0.3)
|
| 74 |
+
pad_h = int(h * 0.3)
|
| 75 |
+
|
| 76 |
+
x1 = max(0, int(box[0]) - pad_w)
|
| 77 |
+
y1 = max(0, int(box[1]) - pad_h)
|
| 78 |
+
x2 = min(pil_img.width, int(box[2]) + pad_w)
|
| 79 |
+
y2 = min(pil_img.height, int(box[3]) + pad_h)
|
| 80 |
+
|
| 81 |
+
if x2 > x1 and y2 > y1:
|
| 82 |
+
face_crop = pil_img.crop((x1, y1, x2, y2))
|
| 83 |
+
faces.append(face_crop)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
cap.release()
|
| 86 |
+
|
| 87 |
+
# VideoMAE requires exactly `sequence_length` frames. Pad by duplicating last frame if short.
|
| 88 |
+
if len(faces) == 0:
|
| 89 |
+
return []
|
| 90 |
+
|
| 91 |
+
while len(faces) < sequence_length:
|
| 92 |
+
faces.append(faces[-1])
|
| 93 |
+
|
| 94 |
return faces
|
| 95 |
|
| 96 |
@app.post("/api/analyze")
|
| 97 |
async def analyze_video(file: UploadFile = File(...)):
|
| 98 |
print(f"Received file: {file.filename}")
|
| 99 |
|
|
|
|
| 100 |
temp_video_path = os.path.join("temp", file.filename)
|
| 101 |
with open(temp_video_path, "wb") as buffer:
|
| 102 |
shutil.copyfileobj(file.file, buffer)
|
| 103 |
|
| 104 |
try:
|
| 105 |
+
print("Extracting facial sequence from video...")
|
| 106 |
+
# Extract 16 consecutive frames
|
| 107 |
+
faces = extract_faces_sequence(temp_video_path, sequence_length=16)
|
| 108 |
|
| 109 |
if not faces:
|
| 110 |
return {
|
| 111 |
"isFake": False,
|
| 112 |
"confidence": 0,
|
| 113 |
+
"explanation": "Could not detect a clear face in the video sequence.",
|
| 114 |
"details": []
|
| 115 |
}
|
| 116 |
|
| 117 |
+
print(f"Extracted {len(faces)} frame sequence. Running temporal inference...")
|
| 118 |
|
| 119 |
+
# Prepare for VideoMAE 3D model
|
| 120 |
+
inputs = processor(list(faces), return_tensors="pt")
|
| 121 |
|
| 122 |
# Run inference
|
| 123 |
with torch.no_grad():
|
| 124 |
outputs = model(**inputs)
|
| 125 |
|
| 126 |
+
# Video classification models return logits for the whole sequence
|
| 127 |
+
probabilities = torch.nn.functional.softmax(outputs.logits[0], dim=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
# Get highest probability label
|
| 130 |
+
predicted_class_idx = probabilities.argmax(-1).item()
|
| 131 |
+
label = model.config.id2label[predicted_class_idx].lower()
|
|
|
|
| 132 |
|
| 133 |
+
is_fake = 'fake' in label
|
| 134 |
+
confidence = round(probabilities[predicted_class_idx].item() * 100, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
explanation = "Our 3D Temporal AI analyzed facial motion and detected unnatural movement, micro-expressions, or spatial inconsistencies typical of deepfakes." if is_fake else "Our 3D Temporal AI analyzed the facial movement and found natural micro-expressions and consistent temporal flow."
|
| 137 |
|
| 138 |
print(f"Result: isFake={is_fake}, confidence={confidence}%")
|
| 139 |
|
|
|
|
| 142 |
"confidence": confidence,
|
| 143 |
"explanation": explanation,
|
| 144 |
"details": [
|
| 145 |
+
{"title": "Temporal Analysis", "desc": "Analyzed a continuous 16-frame clip using a VideoMAE 3D Transformer."},
|
| 146 |
+
{"title": "Motion Tracking", "desc": "Tracked facial landmarks across time to detect jitter, blending artifacts, and lip-sync inconsistencies."}
|
|
|
|
| 147 |
]
|
| 148 |
}
|
| 149 |
|
|
|
|
| 152 |
return {"error": str(e)}
|
| 153 |
|
| 154 |
finally:
|
|
|
|
| 155 |
if os.path.exists(temp_video_path):
|
| 156 |
os.remove(temp_video_path)
|
| 157 |
|
| 158 |
@app.get("/")
|
| 159 |
def health_check():
|
| 160 |
+
return {"status": "3D Temporal Backend is running!"}
|