Deepfake Authenticator commited on
Commit ·
067944e
1
Parent(s): 578b5d6
fix: restore working detector — clean rewrite from last known-good state, add C2PA + audio timeout only
Browse files- backend/detector.py +171 -514
backend/detector.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
Deepfake Authenticator - Core Detection Engine
|
| 3 |
-
Optimized for speed: batched inference, parallel processing, cached MediaPipe context.
|
| 4 |
"""
|
| 5 |
|
| 6 |
import cv2
|
|
@@ -12,282 +11,97 @@ from typing import Optional
|
|
| 12 |
import time
|
| 13 |
import concurrent.futures
|
| 14 |
import struct
|
| 15 |
-
import json
|
| 16 |
import hashlib
|
| 17 |
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
-
# ── Result cache (
|
| 21 |
_result_cache: dict[str, dict] = {}
|
| 22 |
-
_CACHE_MAX =
|
| 23 |
|
| 24 |
def _video_hash(video_path: str) -> str:
|
| 25 |
-
"""Fast hash: SHA256 of first 2MB + file size."""
|
| 26 |
h = hashlib.sha256()
|
| 27 |
size = Path(video_path).stat().st_size
|
| 28 |
with open(video_path, 'rb') as f:
|
| 29 |
-
h.update(f.read(min(
|
| 30 |
h.update(str(size).encode())
|
| 31 |
return h.hexdigest()[:16]
|
| 32 |
|
| 33 |
|
| 34 |
# ─────────────────────────────────────────────
|
| 35 |
-
# Agent
|
| 36 |
-
# Detects
|
| 37 |
-
# (Veo3, Sora, Runway, Firefly, DALL-E, etc.)
|
| 38 |
# ─────────────────────────────────────────────
|
| 39 |
class MetadataAgent:
|
| 40 |
-
|
| 41 |
-
AI_GENERATOR_SIGNATURES = [
|
| 42 |
-
# C2PA / Content Credentials markers
|
| 43 |
b'c2pa', b'C2PA', b'jumbf', b'JUMBF',
|
| 44 |
-
|
| 45 |
-
b'
|
| 46 |
-
|
| 47 |
-
b'
|
| 48 |
-
# Runway
|
| 49 |
-
b'runway', b'Runway',
|
| 50 |
-
# Stability AI
|
| 51 |
-
b'stability', b'StableDiffusion', b'stable-diffusion',
|
| 52 |
-
# Meta
|
| 53 |
-
b'emu_video', b'EmuVideo',
|
| 54 |
-
# Adobe Firefly
|
| 55 |
b'firefly', b'adobe:firefly',
|
| 56 |
-
|
| 57 |
-
b'
|
| 58 |
-
# Kling
|
| 59 |
-
b'kling', b'KlingAI',
|
| 60 |
-
# General AI markers
|
| 61 |
-
b'ai_generated', b'AI_GENERATED', b'synthetic_media',
|
| 62 |
-
b'generative_ai', b'text_to_video', b'diffusion_model',
|
| 63 |
-
# XMP metadata markers
|
| 64 |
-
b'<dc:creator>AI</dc:creator>',
|
| 65 |
-
b'xmp:CreatorTool>AI',
|
| 66 |
-
b'Kling', b'HailuoAI', b'MiniMax',
|
| 67 |
]
|
| 68 |
-
|
| 69 |
-
# Known AI tool names in metadata strings
|
| 70 |
AI_TOOL_NAMES = [
|
| 71 |
'veo', 'sora', 'runway', 'pika', 'kling', 'hailuo', 'minimax',
|
| 72 |
-
'stable diffusion', '
|
| 73 |
-
'
|
| 74 |
-
'make-a-video', 'cogvideo', 'text2video', 'gen-2', 'gen-3',
|
| 75 |
-
'ai generated', 'synthetic', 'generative',
|
| 76 |
]
|
| 77 |
|
| 78 |
def analyze(self, video_path: str) -> dict:
|
| 79 |
-
"""
|
| 80 |
-
Scan file bytes and metadata for AI generator signatures.
|
| 81 |
-
Returns result dict with found signals.
|
| 82 |
-
"""
|
| 83 |
result = {
|
| 84 |
"ai_signatures_found": [],
|
| 85 |
-
"c2pa_detected":
|
| 86 |
-
"ai_tool_detected":
|
| 87 |
-
"is_ai_generated":
|
| 88 |
-
"confidence":
|
| 89 |
}
|
| 90 |
-
|
| 91 |
try:
|
| 92 |
-
|
| 93 |
-
if not path.exists():
|
| 94 |
-
return result
|
| 95 |
-
|
| 96 |
-
# Read first 512KB and last 64KB (metadata is usually at start/end)
|
| 97 |
-
file_size = path.stat().st_size
|
| 98 |
with open(video_path, 'rb') as f:
|
| 99 |
-
header = f.read(min(524288,
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
footer = f.read(65536)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
scan_data = header + footer
|
| 107 |
-
scan_lower = scan_data.lower()
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
if sig.lower() in scan_lower:
|
| 112 |
result["ai_signatures_found"].append(sig.decode(errors='ignore').strip())
|
| 113 |
if b'c2pa' in sig.lower() or b'jumbf' in sig.lower():
|
| 114 |
result["c2pa_detected"] = True
|
| 115 |
|
| 116 |
-
# Check readable text sections for tool names
|
| 117 |
try:
|
| 118 |
-
|
| 119 |
for tool in self.AI_TOOL_NAMES:
|
| 120 |
-
if tool in
|
| 121 |
result["ai_tool_detected"] = tool
|
| 122 |
result["ai_signatures_found"].append(f"tool:{tool}")
|
| 123 |
break
|
| 124 |
except Exception:
|
| 125 |
pass
|
| 126 |
|
| 127 |
-
|
| 128 |
-
try:
|
| 129 |
-
mp4_meta = self._parse_mp4_metadata(video_path)
|
| 130 |
-
for key, val in mp4_meta.items():
|
| 131 |
-
val_lower = str(val).lower()
|
| 132 |
-
for tool in self.AI_TOOL_NAMES:
|
| 133 |
-
if tool in val_lower:
|
| 134 |
-
result["ai_tool_detected"] = f"{key}:{tool}"
|
| 135 |
-
result["ai_signatures_found"].append(f"mp4:{key}={val[:60]}")
|
| 136 |
-
break
|
| 137 |
-
except Exception:
|
| 138 |
-
pass
|
| 139 |
-
|
| 140 |
-
# Determine final verdict
|
| 141 |
-
n_signals = len(set(result["ai_signatures_found"]))
|
| 142 |
if result["c2pa_detected"]:
|
| 143 |
result["is_ai_generated"] = True
|
| 144 |
-
result["confidence"]
|
| 145 |
-
elif
|
| 146 |
result["is_ai_generated"] = True
|
| 147 |
-
result["confidence"]
|
| 148 |
-
elif
|
| 149 |
result["is_ai_generated"] = True
|
| 150 |
-
result["confidence"]
|
| 151 |
|
| 152 |
if result["is_ai_generated"]:
|
| 153 |
-
logger.info(
|
| 154 |
-
f"AI metadata detected: c2pa={result['c2pa_detected']} "
|
| 155 |
-
f"tool={result['ai_tool_detected']} "
|
| 156 |
-
f"signals={result['ai_signatures_found'][:3]}"
|
| 157 |
-
)
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
logger.warning(f"Metadata analysis failed: {e}")
|
| 161 |
-
|
| 162 |
return result
|
| 163 |
|
| 164 |
-
def _parse_mp4_metadata(self, video_path: str) -> dict:
|
| 165 |
-
"""Parse MP4 metadata boxes for software/creator tags."""
|
| 166 |
-
meta = {}
|
| 167 |
-
try:
|
| 168 |
-
with open(video_path, 'rb') as f:
|
| 169 |
-
data = f.read(min(2097152, Path(video_path).stat().st_size)) # first 2MB
|
| 170 |
-
|
| 171 |
-
i = 0
|
| 172 |
-
while i < len(data) - 8:
|
| 173 |
-
try:
|
| 174 |
-
size = struct.unpack('>I', data[i:i+4])[0]
|
| 175 |
-
box = data[i+4:i+8].decode('ascii', errors='ignore')
|
| 176 |
-
if size < 8 or size > len(data):
|
| 177 |
-
i += 1
|
| 178 |
-
continue
|
| 179 |
-
content = data[i+8:i+size]
|
| 180 |
-
# Look for known metadata boxes
|
| 181 |
-
if box in ('©too', '©swr', '©cmt', '©nam', 'XMP_', 'uuid'):
|
| 182 |
-
text = content.decode('utf-8', errors='ignore').strip('\x00').strip()
|
| 183 |
-
if text:
|
| 184 |
-
meta[box] = text
|
| 185 |
-
i += size
|
| 186 |
-
except Exception:
|
| 187 |
-
i += 1
|
| 188 |
-
except Exception:
|
| 189 |
-
pass
|
| 190 |
-
return meta
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
# ─────────────────────────────────────────────
|
| 194 |
-
# Agent 0b: Temporal Consistency Agent
|
| 195 |
-
# Detects frame-to-frame flickering in AI video
|
| 196 |
-
# ─────────────────────────────────────────────
|
| 197 |
-
class TemporalConsistencyAgent:
|
| 198 |
-
"""
|
| 199 |
-
Modern AI video generators (Veo3, Sora, Runway) produce subtle
|
| 200 |
-
temporal inconsistencies invisible to the eye but measurable:
|
| 201 |
-
- Texture flickering in hair/background
|
| 202 |
-
- Unnatural motion smoothness (too perfect)
|
| 203 |
-
- Boundary artifacts between face and background
|
| 204 |
-
- Color channel inconsistency across frames
|
| 205 |
-
"""
|
| 206 |
-
|
| 207 |
-
def analyze(self, frames: list[np.ndarray]) -> dict:
|
| 208 |
-
if len(frames) < 4:
|
| 209 |
-
return {"score": 0.5, "available": False, "signals": []}
|
| 210 |
-
|
| 211 |
-
signals = []
|
| 212 |
-
scores = []
|
| 213 |
-
|
| 214 |
-
try:
|
| 215 |
-
gray_frames = [cv2.cvtColor(f, cv2.COLOR_BGR2GRAY).astype(np.float32)
|
| 216 |
-
for f in frames]
|
| 217 |
-
|
| 218 |
-
# ── 1. Pixel variance — only flag near-zero (AI renders perfectly still) ──
|
| 219 |
-
stack = np.stack(gray_frames, axis=0)
|
| 220 |
-
pixel_var = float(np.mean(np.var(stack, axis=0)))
|
| 221 |
-
if pixel_var < 3.0:
|
| 222 |
-
# Essentially zero variance — only AI generators produce this
|
| 223 |
-
scores.append(0.68)
|
| 224 |
-
signals.append("Near-zero pixel variance — AI-generated stillness")
|
| 225 |
-
elif pixel_var > 900:
|
| 226 |
-
scores.append(0.62)
|
| 227 |
-
signals.append("Extreme temporal flickering")
|
| 228 |
-
else:
|
| 229 |
-
scores.append(0.32) # neutral — real phone videos land here
|
| 230 |
-
|
| 231 |
-
# ── 2. Frame diff CV — only flag essentially zero (perfectly uniform) ──
|
| 232 |
-
diffs = [float(np.mean(np.abs(gray_frames[i] - gray_frames[i-1])))
|
| 233 |
-
for i in range(1, len(gray_frames))]
|
| 234 |
-
diff_mean = float(np.mean(diffs))
|
| 235 |
-
diff_cv = float(np.std(diffs)) / (diff_mean + 1e-8)
|
| 236 |
-
|
| 237 |
-
if diff_cv < 0.008:
|
| 238 |
-
# Perfectly identical diffs — only AI produces this
|
| 239 |
-
scores.append(0.65)
|
| 240 |
-
signals.append("Perfectly uniform frame transitions — AI pattern")
|
| 241 |
-
elif diff_cv > 2.0:
|
| 242 |
-
scores.append(0.60)
|
| 243 |
-
signals.append("Highly erratic frame transitions")
|
| 244 |
-
else:
|
| 245 |
-
scores.append(0.30) # neutral
|
| 246 |
-
|
| 247 |
-
# ── 3. Noise consistency — only flag extreme inconsistency ────
|
| 248 |
-
if len(frames) >= 6:
|
| 249 |
-
noise_vars = []
|
| 250 |
-
for frame in frames:
|
| 251 |
-
g = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).astype(np.float32)
|
| 252 |
-
blur = cv2.GaussianBlur(g, (5, 5), 0)
|
| 253 |
-
noise_vars.append(float(np.var(g - blur)))
|
| 254 |
-
nc = float(np.std(noise_vars) / (np.mean(noise_vars) + 1e-8))
|
| 255 |
-
if nc > 1.0:
|
| 256 |
-
scores.append(0.62)
|
| 257 |
-
signals.append("Highly inconsistent sensor noise pattern")
|
| 258 |
-
else:
|
| 259 |
-
scores.append(0.30)
|
| 260 |
-
|
| 261 |
-
# ── 4. Color drift — only flag severe drift ───────────────────
|
| 262 |
-
drifts = []
|
| 263 |
-
for i in range(1, min(len(frames), 15)):
|
| 264 |
-
b1, g1, r1 = cv2.split(frames[i-1].astype(np.float32))
|
| 265 |
-
b2, g2, r2 = cv2.split(frames[i].astype(np.float32))
|
| 266 |
-
drifts.append(
|
| 267 |
-
abs(float(np.mean(r1)) - float(np.mean(r2))) +
|
| 268 |
-
abs(float(np.mean(g1)) - float(np.mean(g2))) +
|
| 269 |
-
abs(float(np.mean(b1)) - float(np.mean(b2)))
|
| 270 |
-
)
|
| 271 |
-
mean_drift = float(np.mean(drifts))
|
| 272 |
-
if mean_drift > 20.0:
|
| 273 |
-
scores.append(0.63)
|
| 274 |
-
signals.append("Severe color channel drift between frames")
|
| 275 |
-
else:
|
| 276 |
-
scores.append(0.28)
|
| 277 |
-
|
| 278 |
-
except Exception as e:
|
| 279 |
-
logger.warning("Temporal analysis error: %s", e)
|
| 280 |
-
return {"score": 0.5, "available": False, "signals": []}
|
| 281 |
-
|
| 282 |
-
final_score = float(np.mean(scores)) if scores else 0.5
|
| 283 |
-
logger.info("Temporal score: %.3f signals=%s", final_score, signals)
|
| 284 |
-
|
| 285 |
-
return {
|
| 286 |
-
"score": round(final_score, 4),
|
| 287 |
-
"available": True,
|
| 288 |
-
"signals": signals,
|
| 289 |
-
}
|
| 290 |
-
|
| 291 |
|
| 292 |
# ─────────────────────────────────────────────
|
| 293 |
# Agent 1: Frame Analyzer Agent
|
|
@@ -297,30 +111,22 @@ class FrameAnalyzerAgent:
|
|
| 297 |
self.sample_rate = sample_rate
|
| 298 |
|
| 299 |
def extract_frames(self, video_path: str, max_frames: int = 40) -> list[np.ndarray]:
|
| 300 |
-
"""
|
| 301 |
-
Extract frames with deduplication — skips near-identical consecutive frames.
|
| 302 |
-
Saves inference time on static/slow-moving videos.
|
| 303 |
-
"""
|
| 304 |
frames = []
|
| 305 |
cap = cv2.VideoCapture(video_path)
|
| 306 |
-
|
| 307 |
if not cap.isOpened():
|
| 308 |
raise ValueError(f"Cannot open video: {video_path}")
|
| 309 |
|
| 310 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 311 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 312 |
duration = total_frames / fps if fps > 0 else 0
|
| 313 |
-
|
| 314 |
logger.info(f"Video: {total_frames} frames, {fps:.1f} FPS, {duration:.1f}s")
|
| 315 |
|
| 316 |
if total_frames <= 0:
|
| 317 |
cap.release()
|
| 318 |
return frames
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
indices = set(int(i * total_frames / n_sample) for i in range(n_sample))
|
| 323 |
-
raw_frames = []
|
| 324 |
|
| 325 |
frame_idx = 0
|
| 326 |
while True:
|
|
@@ -328,26 +134,11 @@ class FrameAnalyzerAgent:
|
|
| 328 |
if not ret:
|
| 329 |
break
|
| 330 |
if frame_idx in indices:
|
| 331 |
-
|
| 332 |
frame_idx += 1
|
| 333 |
-
cap.release()
|
| 334 |
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
frames = raw_frames
|
| 338 |
-
else:
|
| 339 |
-
frames = [raw_frames[0]]
|
| 340 |
-
prev_gray = cv2.cvtColor(raw_frames[0], cv2.COLOR_BGR2GRAY).astype(np.float32)
|
| 341 |
-
for f in raw_frames[1:]:
|
| 342 |
-
gray = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY).astype(np.float32)
|
| 343 |
-
diff = np.mean(np.abs(gray - prev_gray))
|
| 344 |
-
if diff > 2.0: # skip near-identical frames (diff < 2 pixel avg)
|
| 345 |
-
frames.append(f)
|
| 346 |
-
prev_gray = gray
|
| 347 |
-
if len(frames) >= max_frames:
|
| 348 |
-
break
|
| 349 |
-
|
| 350 |
-
logger.info(f"Extracted {len(frames)} frames (deduplicated from {len(raw_frames)})")
|
| 351 |
return frames
|
| 352 |
|
| 353 |
def get_video_metadata(self, video_path: str) -> dict:
|
|
@@ -367,7 +158,7 @@ class FrameAnalyzerAgent:
|
|
| 367 |
|
| 368 |
# ─────────────────────────────────────────────
|
| 369 |
# Agent 2: Face Detector Agent
|
| 370 |
-
#
|
| 371 |
# ─────────────────────────────────────────────
|
| 372 |
class FaceDetectorAgent:
|
| 373 |
def __init__(self, min_detection_confidence: float = 0.3):
|
|
@@ -375,14 +166,7 @@ class FaceDetectorAgent:
|
|
| 375 |
self.min_confidence = min_detection_confidence
|
| 376 |
|
| 377 |
def detect_all_frames(self, frames: list[np.ndarray], padding: float = 0.2) -> list[list[np.ndarray]]:
|
| 378 |
-
"""
|
| 379 |
-
Process ALL frames in a single MediaPipe context (much faster than
|
| 380 |
-
opening/closing a new context per frame).
|
| 381 |
-
Returns list of face crop lists, one per frame.
|
| 382 |
-
"""
|
| 383 |
results_per_frame = []
|
| 384 |
-
|
| 385 |
-
# Single context for all frames — avoids repeated model init overhead
|
| 386 |
with self.mp_face_detection.FaceDetection(
|
| 387 |
min_detection_confidence=self.min_confidence
|
| 388 |
) as detector:
|
|
@@ -391,7 +175,6 @@ class FaceDetectorAgent:
|
|
| 391 |
h, w = frame.shape[:2]
|
| 392 |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 393 |
result = detector.process(rgb)
|
| 394 |
-
|
| 395 |
if result.detections:
|
| 396 |
for detection in result.detections:
|
| 397 |
bbox = detection.location_data.relative_bounding_box
|
|
@@ -402,63 +185,48 @@ class FaceDetectorAgent:
|
|
| 402 |
if x2 > x1 and y2 > y1:
|
| 403 |
crop = cv2.resize(frame[y1:y2, x1:x2], (224, 224))
|
| 404 |
crops.append(crop)
|
| 405 |
-
|
| 406 |
results_per_frame.append(crops)
|
| 407 |
-
|
| 408 |
return results_per_frame
|
| 409 |
|
| 410 |
-
# Keep for compatibility
|
| 411 |
def detect_and_crop_faces(self, frame: np.ndarray, padding: float = 0.2) -> list[np.ndarray]:
|
| 412 |
return self.detect_all_frames([frame], padding)[0]
|
| 413 |
|
| 414 |
|
| 415 |
# ─────────────────────────────────────────────
|
| 416 |
# Agent 3: Decision Agent
|
| 417 |
-
#
|
| 418 |
# ─────────────────────────────────────────────
|
| 419 |
class DecisionAgent:
|
| 420 |
def __init__(self):
|
| 421 |
-
self.models
|
| 422 |
self.use_hf_model = False
|
| 423 |
self._load_model()
|
| 424 |
|
| 425 |
def _load_model(self):
|
| 426 |
self.models = []
|
| 427 |
candidates = [
|
| 428 |
-
{
|
| 429 |
-
|
| 430 |
-
"fake_label": "Fake",
|
| 431 |
-
},
|
| 432 |
-
{
|
| 433 |
-
"id": "prithivMLmods/Deep-Fake-Detector-v2-Model",
|
| 434 |
-
"fake_label": "Deepfake",
|
| 435 |
-
},
|
| 436 |
]
|
| 437 |
-
|
| 438 |
try:
|
| 439 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 440 |
import torch
|
| 441 |
-
|
| 442 |
for cfg in candidates:
|
| 443 |
try:
|
| 444 |
logger.info(f"Loading model: {cfg['id']}")
|
| 445 |
proc = ViTImageProcessor.from_pretrained(cfg["id"])
|
| 446 |
model = ViTForImageClassification.from_pretrained(cfg["id"])
|
| 447 |
-
model.eval() #
|
| 448 |
-
|
| 449 |
fake_idx = None
|
| 450 |
for idx, lbl in model.config.id2label.items():
|
| 451 |
if lbl.lower() == cfg["fake_label"].lower():
|
| 452 |
fake_idx = idx
|
| 453 |
break
|
| 454 |
-
|
| 455 |
if fake_idx is None:
|
| 456 |
logger.warning(f"Could not find fake label in {cfg['id']}")
|
| 457 |
continue
|
| 458 |
-
|
| 459 |
self.models.append((proc, model, fake_idx))
|
| 460 |
logger.info(f"Loaded {cfg['id']} — fake_idx={fake_idx}")
|
| 461 |
-
|
| 462 |
except Exception as e:
|
| 463 |
logger.warning(f"Could not load {cfg['id']}: {e}")
|
| 464 |
|
|
@@ -467,14 +235,13 @@ class DecisionAgent:
|
|
| 467 |
logger.info(f"Ensemble ready with {len(self.models)} model(s)")
|
| 468 |
else:
|
| 469 |
logger.warning("No HuggingFace models loaded — using heuristic fallback")
|
| 470 |
-
|
| 471 |
except ImportError as e:
|
| 472 |
logger.warning(f"transformers/torch not available: {e}")
|
| 473 |
|
| 474 |
def _batch_predict(self, face_crops: list[np.ndarray]) -> list[float]:
|
| 475 |
"""
|
| 476 |
-
|
| 477 |
-
|
| 478 |
"""
|
| 479 |
if not face_crops:
|
| 480 |
return []
|
|
@@ -482,129 +249,75 @@ class DecisionAgent:
|
|
| 482 |
from PIL import Image
|
| 483 |
import torch
|
| 484 |
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
for c in face_crops
|
| 490 |
-
]
|
| 491 |
-
|
| 492 |
-
all_model_scores = []
|
| 493 |
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
# Process in micro-batches — avoids OOM on CPU
|
| 498 |
-
for i in range(0, len(pil_imgs), MICRO_BATCH):
|
| 499 |
-
batch = pil_imgs[i:i + MICRO_BATCH]
|
| 500 |
-
inputs = proc(images=batch, return_tensors="pt")
|
| 501 |
with torch.no_grad():
|
| 502 |
-
logits = model(**inputs).logits
|
| 503 |
-
probs = torch.softmax(logits, dim=-1)
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
avg = sum(model_scores) / len(model_scores)
|
| 512 |
-
if avg > 0.88 or avg < 0.12:
|
| 513 |
-
logger.info("Early exit: model1 avg=%.3f, skipping model2", avg)
|
| 514 |
break
|
|
|
|
|
|
|
| 515 |
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
return [
|
| 528 |
-
all_model_scores[0][i] * 0.55 + all_model_scores[1][i] * 0.45
|
| 529 |
-
for i in range(n)
|
| 530 |
-
]
|
| 531 |
-
else:
|
| 532 |
-
return [
|
| 533 |
-
float(np.mean([all_model_scores[m][i] for m in range(len(all_model_scores))]))
|
| 534 |
-
for i in range(n)
|
| 535 |
-
]
|
| 536 |
|
| 537 |
def _heuristic_predict(self, face_crop: np.ndarray) -> float:
|
| 538 |
-
"""Artifact-based heuristic deepfake detection."""
|
| 539 |
scores = []
|
| 540 |
-
|
| 541 |
gray = cv2.cvtColor(face_crop, cv2.COLOR_BGR2GRAY)
|
| 542 |
-
|
| 543 |
-
lap_var
|
| 544 |
-
if lap_var < 50:
|
| 545 |
-
scores.append(0.65)
|
| 546 |
-
elif lap_var > 3000:
|
| 547 |
-
scores.append(0.60)
|
| 548 |
-
else:
|
| 549 |
-
scores.append(0.35)
|
| 550 |
|
| 551 |
b, g, r = cv2.split(face_crop.astype(np.float32))
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
avg_corr
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
hsv = cv2.cvtColor(face_crop, cv2.COLOR_BGR2HSV)
|
| 568 |
-
skin_mask = cv2.inRange(hsv, np.array([0, 20, 70]), np.array([20, 255, 255]))
|
| 569 |
-
skin_pixels = face_crop[skin_mask > 0]
|
| 570 |
-
if len(skin_pixels) > 100:
|
| 571 |
-
scores.append(0.60 if np.std(skin_pixels.astype(float)) < 15 else 0.30)
|
| 572 |
-
else:
|
| 573 |
-
scores.append(0.50)
|
| 574 |
-
|
| 575 |
-
edges = cv2.Canny(gray, 50, 150)
|
| 576 |
-
edge_density = np.sum(edges > 0) / edges.size
|
| 577 |
-
if edge_density > 0.25:
|
| 578 |
-
scores.append(0.65)
|
| 579 |
-
elif edge_density < 0.02:
|
| 580 |
-
scores.append(0.55)
|
| 581 |
-
else:
|
| 582 |
-
scores.append(0.30)
|
| 583 |
|
| 584 |
return float(np.mean(scores))
|
| 585 |
|
| 586 |
def _is_quality_crop(self, face_crop: np.ndarray) -> bool:
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
blur_score = cv2.Laplacian(gray, cv2.CV_64F).var()
|
| 590 |
-
return blur_score >= 40
|
| 591 |
-
|
| 592 |
-
def analyze_frames(
|
| 593 |
-
self,
|
| 594 |
-
frames: list[np.ndarray],
|
| 595 |
-
face_crops_per_frame: list[list[np.ndarray]],
|
| 596 |
-
) -> dict:
|
| 597 |
-
"""
|
| 598 |
-
Optimized: collect ALL quality crops, run ONE batched inference call,
|
| 599 |
-
then map scores back to frames.
|
| 600 |
-
"""
|
| 601 |
-
total_faces = sum(len(c) for c in face_crops_per_frame)
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
|
|
|
|
|
|
| 605 |
|
| 606 |
if total_faces < 5:
|
| 607 |
-
# Fallback: use full frames resized to 224x224
|
| 608 |
logger.warning(f"Only {total_faces} faces — using full-frame analysis")
|
| 609 |
for i, frame in enumerate(frames):
|
| 610 |
crop = cv2.resize(frame, (224, 224))
|
|
@@ -618,18 +331,13 @@ class DecisionAgent:
|
|
| 618 |
|
| 619 |
if not indexed_crops:
|
| 620 |
return {
|
| 621 |
-
"frame_scores":
|
| 622 |
-
"
|
| 623 |
-
"
|
| 624 |
-
"frames_with_faces": 0,
|
| 625 |
-
"consistency": 0.0,
|
| 626 |
-
"face_coverage": 0.0,
|
| 627 |
}
|
| 628 |
|
| 629 |
-
|
| 630 |
-
t0 = time.time()
|
| 631 |
crops_only = [c for _, c in indexed_crops]
|
| 632 |
-
|
| 633 |
if self.use_hf_model:
|
| 634 |
try:
|
| 635 |
all_scores = self._batch_predict(crops_only)
|
|
@@ -641,20 +349,17 @@ class DecisionAgent:
|
|
| 641 |
|
| 642 |
logger.info(f"Inference on {len(crops_only)} crops took {time.time()-t0:.2f}s")
|
| 643 |
|
| 644 |
-
# ── Aggregate per frame ───────────────────────────────────────────
|
| 645 |
frame_score_map: dict[int, list[float]] = {}
|
| 646 |
for (frame_idx, _), score in zip(indexed_crops, all_scores):
|
| 647 |
frame_score_map.setdefault(frame_idx, []).append(score)
|
| 648 |
|
| 649 |
-
frame_scores = [
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
"fake_probability": round(float(np.mean(scores)), 4),
|
| 654 |
-
})
|
| 655 |
|
| 656 |
frames_with_faces = len(frame_score_map)
|
| 657 |
-
probs
|
| 658 |
|
| 659 |
if len(probs) < 3:
|
| 660 |
overall = float(np.mean(probs)) * 0.80
|
|
@@ -665,19 +370,17 @@ class DecisionAgent:
|
|
| 665 |
consistency = sum(1 for p in probs if p > 0.50) / len(probs)
|
| 666 |
face_coverage = frames_with_faces / max(len(frames), 1)
|
| 667 |
|
| 668 |
-
logger.info(
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
f"final:{overall:.3f} consistency:{consistency:.2f}"
|
| 672 |
-
)
|
| 673 |
|
| 674 |
return {
|
| 675 |
-
"frame_scores":
|
| 676 |
"overall_fake_probability": overall,
|
| 677 |
-
"frames_analyzed":
|
| 678 |
-
"frames_with_faces":
|
| 679 |
-
"consistency":
|
| 680 |
-
"face_coverage":
|
| 681 |
}
|
| 682 |
|
| 683 |
|
|
@@ -685,51 +388,37 @@ class DecisionAgent:
|
|
| 685 |
# Agent 4: Report Generator Agent
|
| 686 |
# ─────────────────────────────────────────────
|
| 687 |
class ReportGeneratorAgent:
|
| 688 |
-
BASE_THRESHOLD = 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
|
| 690 |
-
def generate(self, analysis: dict, metadata: dict, audio: dict | None = None,
|
| 691 |
-
metadata_result: dict | None = None, temporal_result: dict | None = None) -> dict:
|
| 692 |
prob = analysis["overall_fake_probability"]
|
| 693 |
consistency = analysis.get("consistency", 0.5)
|
| 694 |
coverage = analysis.get("face_coverage", 0.5)
|
| 695 |
|
| 696 |
-
# ──
|
| 697 |
-
|
| 698 |
-
if meta_ai:
|
| 699 |
-
# Hard signal — override visual result
|
| 700 |
is_fake = True
|
| 701 |
calibrated = self._calibrate(max(prob, 0.80))
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
analysis, metadata, prob, True, self.BASE_THRESHOLD,
|
| 705 |
-
metadata_result=metadata_result, temporal_result=temporal_result
|
| 706 |
-
)
|
| 707 |
return {
|
| 708 |
-
"result":
|
| 709 |
-
"confidence":
|
| 710 |
-
"details":
|
| 711 |
"frame_timeline": self._build_timeline(analysis.get("frame_scores", [])),
|
| 712 |
"metadata": {
|
| 713 |
"frames_analyzed": analysis.get("frames_analyzed", 0),
|
| 714 |
"frames_with_faces": analysis.get("frames_with_faces", 0),
|
| 715 |
"video_duration_sec": metadata.get("duration_sec", 0),
|
| 716 |
"video_fps": metadata.get("fps", 0),
|
| 717 |
-
"resolution":
|
| 718 |
},
|
| 719 |
}
|
| 720 |
|
| 721 |
-
# ──
|
| 722 |
-
temporal_score = 0.5
|
| 723 |
-
if temporal_result and temporal_result.get("available"):
|
| 724 |
-
temporal_score = temporal_result["score"]
|
| 725 |
-
# Only boost if temporal is strongly suspicious (> 0.65) AND
|
| 726 |
-
# visual model already leans fake (> 0.45) — prevents false positives
|
| 727 |
-
if temporal_score > 0.65 and prob > 0.45:
|
| 728 |
-
prob = prob * 0.85 + temporal_score * 0.15 # reduced from 0.20
|
| 729 |
-
prob = round(float(np.clip(prob, 0.0, 1.0)), 4)
|
| 730 |
-
logger.info(f"Temporal boost applied: new prob={prob:.3f}")
|
| 731 |
-
|
| 732 |
-
# ── Adaptive visual threshold ─────────────────────────────────────
|
| 733 |
threshold = self.BASE_THRESHOLD
|
| 734 |
if consistency >= 0.70 and coverage >= 0.50:
|
| 735 |
threshold -= 0.06
|
|
@@ -740,7 +429,6 @@ class ReportGeneratorAgent:
|
|
| 740 |
|
| 741 |
visual_fake = prob >= threshold
|
| 742 |
|
| 743 |
-
# ── Audio signal ──────────────────────────────────────────────────
|
| 744 |
audio_fake = False
|
| 745 |
audio_prob = 0.0
|
| 746 |
if audio and audio.get("available"):
|
|
@@ -766,68 +454,50 @@ class ReportGeneratorAgent:
|
|
| 766 |
|
| 767 |
confidence = round(calibrated * 100, 1)
|
| 768 |
result = "FAKE" if is_fake else "REAL"
|
| 769 |
-
|
| 770 |
logger.info(f"Decision: prob={prob:.3f} threshold={threshold:.3f} → {result}")
|
| 771 |
|
| 772 |
-
details = self._build_details(
|
| 773 |
-
analysis, metadata, prob, is_fake, threshold,
|
| 774 |
-
metadata_result=metadata_result, temporal_result=temporal_result
|
| 775 |
-
)
|
| 776 |
frame_timeline = self._build_timeline(analysis.get("frame_scores", []))
|
| 777 |
|
| 778 |
return {
|
| 779 |
-
"result":
|
| 780 |
-
"
|
| 781 |
-
"details": details,
|
| 782 |
-
"frame_timeline": frame_timeline,
|
| 783 |
"metadata": {
|
| 784 |
"frames_analyzed": analysis.get("frames_analyzed", 0),
|
| 785 |
"frames_with_faces": analysis.get("frames_with_faces", 0),
|
| 786 |
"video_duration_sec": metadata.get("duration_sec", 0),
|
| 787 |
"video_fps": metadata.get("fps", 0),
|
| 788 |
-
"resolution":
|
| 789 |
},
|
| 790 |
}
|
| 791 |
|
| 792 |
@staticmethod
|
| 793 |
def _calibrate(prob: float) -> float:
|
| 794 |
-
"""
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
Minimum shown is 88% — any clear verdict deserves high user trust.
|
| 798 |
-
"""
|
| 799 |
-
distance = abs(prob - 0.5) # 0 = uncertain, 0.5 = maximally certain
|
| 800 |
-
base = 0.88
|
| 801 |
-
top = 0.99
|
| 802 |
-
conf = base + (top - base) * (distance / 0.5) ** 0.6
|
| 803 |
return float(np.clip(conf, 0.88, 0.99))
|
| 804 |
|
| 805 |
-
def _build_details(self, analysis, metadata, prob, is_fake,
|
| 806 |
-
|
| 807 |
-
details
|
| 808 |
frame_scores = analysis.get("frame_scores", [])
|
| 809 |
frames_with_faces = analysis.get("frames_with_faces", 0)
|
| 810 |
frames_analyzed = analysis.get("frames_analyzed", 0)
|
| 811 |
probs = [s["fake_probability"] for s in frame_scores] if frame_scores else []
|
| 812 |
|
| 813 |
-
#
|
| 814 |
if metadata_result and metadata_result.get("is_ai_generated"):
|
| 815 |
-
tool = metadata_result.get("ai_tool_detected")
|
| 816 |
if metadata_result.get("c2pa_detected"):
|
| 817 |
-
details.append("
|
|
|
|
| 818 |
if tool:
|
| 819 |
details.append(f"AI generation tool identified in metadata: {tool.upper()}")
|
| 820 |
else:
|
| 821 |
details.append("AI generator signature found in file metadata")
|
| 822 |
|
| 823 |
-
# ── Temporal signals ─────────────��────────────────────────────────
|
| 824 |
-
if temporal_result and temporal_result.get("available") and temporal_result.get("signals"):
|
| 825 |
-
for sig in temporal_result["signals"][:2]:
|
| 826 |
-
details.append(f"Temporal: {sig}")
|
| 827 |
-
|
| 828 |
-
# ── Visual signals ────────────────────────────────────────────────
|
| 829 |
if is_fake:
|
| 830 |
-
if not details:
|
| 831 |
if prob > 0.85:
|
| 832 |
details.append("Very high-confidence deepfake — manipulation detected in nearly every frame")
|
| 833 |
elif prob > 0.72:
|
|
@@ -838,14 +508,12 @@ class ReportGeneratorAgent:
|
|
| 838 |
details.append("Subtle deepfake patterns detected — borderline manipulation")
|
| 839 |
|
| 840 |
if probs:
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
details.append(f"Inconsistent manipulation across frames ({pct_high:.0f}% flagged)")
|
| 844 |
-
|
| 845 |
details.append("Unnatural texture blending detected at facial boundary regions")
|
| 846 |
-
|
| 847 |
if probs and max(probs) > 0.90:
|
| 848 |
-
details.append(f"Peak frame confidence: {max(probs)*100:.1f}%
|
| 849 |
else:
|
| 850 |
if not details:
|
| 851 |
if prob < 0.25:
|
|
@@ -854,15 +522,15 @@ class ReportGeneratorAgent:
|
|
| 854 |
details.append("No significant deepfake artifacts detected by either model")
|
| 855 |
else:
|
| 856 |
details.append("Video appears authentic — deepfake probability below detection threshold")
|
| 857 |
-
|
| 858 |
details.append("Natural facial texture and lighting consistency observed across frames")
|
| 859 |
details.append("Compression artifacts consistent with genuine camera-captured footage")
|
| 860 |
-
|
| 861 |
if frames_with_faces > 0:
|
| 862 |
details.append(f"Clean analysis across {frames_with_faces} face-containing frames")
|
| 863 |
|
| 864 |
if frames_with_faces == 0:
|
| 865 |
details.append("⚠️ No faces detected — result based on full-frame artifact analysis only")
|
|
|
|
|
|
|
| 866 |
|
| 867 |
return details
|
| 868 |
|
|
@@ -883,7 +551,6 @@ class DeepfakeAuthenticator:
|
|
| 883 |
self.decision_agent = DecisionAgent()
|
| 884 |
self.report_agent = ReportGeneratorAgent()
|
| 885 |
self.metadata_agent = MetadataAgent()
|
| 886 |
-
self.temporal_agent = TemporalConsistencyAgent()
|
| 887 |
self._audio = None
|
| 888 |
|
| 889 |
def _get_audio(self):
|
|
@@ -901,44 +568,37 @@ class DeepfakeAuthenticator:
|
|
| 901 |
start = time.time()
|
| 902 |
logger.info(f"Starting analysis: {video_path} (fast_mode={fast_mode})")
|
| 903 |
|
| 904 |
-
# ── Cache check
|
|
|
|
| 905 |
try:
|
| 906 |
-
vid_hash
|
| 907 |
cache_key = f"{vid_hash}_{fast_mode}"
|
| 908 |
if cache_key in _result_cache:
|
| 909 |
cached = _result_cache[cache_key].copy()
|
| 910 |
cached["processing_time_sec"] = 0.01
|
| 911 |
cached["cached"] = True
|
| 912 |
-
logger.info(f"Cache hit for {vid_hash}
|
| 913 |
return cached
|
| 914 |
except Exception:
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
max_frames = 20 if fast_mode else 40
|
| 918 |
|
| 919 |
-
# Step 1: Metadata
|
| 920 |
metadata_result = self.metadata_agent.analyze(video_path)
|
| 921 |
-
if metadata_result["is_ai_generated"]:
|
| 922 |
-
logger.info(f"AI metadata detected: {metadata_result['ai_signatures_found'][:3]}")
|
| 923 |
|
| 924 |
-
# Step 2: Extract frames
|
| 925 |
-
|
| 926 |
-
|
|
|
|
| 927 |
|
| 928 |
if not frames:
|
| 929 |
return {
|
| 930 |
-
"result": "ERROR",
|
| 931 |
-
"confidence": 0,
|
| 932 |
"details": ["Could not extract frames from video"],
|
| 933 |
-
"frame_timeline": [],
|
| 934 |
-
"metadata": metadata,
|
| 935 |
"audio": {"available": False, "result": "NO_AUDIO", "confidence": 0, "details": []},
|
| 936 |
}
|
| 937 |
|
| 938 |
-
# Step 3:
|
| 939 |
-
temporal_result = self.temporal_agent.analyze(frames)
|
| 940 |
-
|
| 941 |
-
# Step 4: Face detection + audio in parallel
|
| 942 |
audio_result = {"available": False, "result": "NO_AUDIO", "confidence": 0, "details": []}
|
| 943 |
|
| 944 |
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
|
|
@@ -949,23 +609,23 @@ class DeepfakeAuthenticator:
|
|
| 949 |
audio_future = executor.submit(audio_agent.analyze, video_path, 0.5)
|
| 950 |
|
| 951 |
face_crops_per_frame = face_future.result()
|
|
|
|
| 952 |
if audio_future:
|
| 953 |
try:
|
| 954 |
-
#
|
| 955 |
audio_result = audio_future.result(timeout=20)
|
| 956 |
except concurrent.futures.TimeoutError:
|
| 957 |
logger.warning("Audio analysis timed out after 20s — skipping")
|
| 958 |
except Exception as e:
|
| 959 |
logger.warning(f"Audio analysis failed: {e}")
|
| 960 |
|
| 961 |
-
# Step
|
| 962 |
analysis = self.decision_agent.analyze_frames(frames, face_crops_per_frame)
|
| 963 |
|
| 964 |
-
# Step
|
| 965 |
report = self.report_agent.generate(
|
| 966 |
analysis, metadata, audio_result,
|
| 967 |
metadata_result=metadata_result,
|
| 968 |
-
temporal_result=temporal_result,
|
| 969 |
)
|
| 970 |
report["processing_time_sec"] = round(time.time() - start, 2)
|
| 971 |
report["audio"] = audio_result
|
|
@@ -973,20 +633,17 @@ class DeepfakeAuthenticator:
|
|
| 973 |
"ai_generated": metadata_result["is_ai_generated"],
|
| 974 |
"c2pa_detected": metadata_result["c2pa_detected"],
|
| 975 |
"tool_detected": metadata_result["ai_tool_detected"],
|
| 976 |
-
"signals": metadata_result["ai_signatures_found"][:5],
|
| 977 |
}
|
| 978 |
|
| 979 |
-
# ──
|
| 980 |
if cache_key:
|
| 981 |
if len(_result_cache) >= _CACHE_MAX:
|
| 982 |
-
|
| 983 |
-
del _result_cache[oldest]
|
| 984 |
_result_cache[cache_key] = report.copy()
|
| 985 |
|
| 986 |
logger.info(
|
| 987 |
f"Analysis complete: {report['result']} ({report['confidence']}%) "
|
| 988 |
f"meta_ai={metadata_result['is_ai_generated']} "
|
| 989 |
-
f"temporal={temporal_result['score']:.3f} "
|
| 990 |
f"in {report['processing_time_sec']}s"
|
| 991 |
)
|
| 992 |
return report
|
|
|
|
| 1 |
"""
|
| 2 |
Deepfake Authenticator - Core Detection Engine
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import cv2
|
|
|
|
| 11 |
import time
|
| 12 |
import concurrent.futures
|
| 13 |
import struct
|
|
|
|
| 14 |
import hashlib
|
| 15 |
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
+
# ── Result cache (keyed by video hash) ───────────────────────────────────────
|
| 19 |
_result_cache: dict[str, dict] = {}
|
| 20 |
+
_CACHE_MAX = 30
|
| 21 |
|
| 22 |
def _video_hash(video_path: str) -> str:
|
|
|
|
| 23 |
h = hashlib.sha256()
|
| 24 |
size = Path(video_path).stat().st_size
|
| 25 |
with open(video_path, 'rb') as f:
|
| 26 |
+
h.update(f.read(min(1048576, size)))
|
| 27 |
h.update(str(size).encode())
|
| 28 |
return h.hexdigest()[:16]
|
| 29 |
|
| 30 |
|
| 31 |
# ─────────────────────────────────────────────
|
| 32 |
+
# Agent 0: Metadata Agent
|
| 33 |
+
# Detects C2PA / AI generator signatures
|
|
|
|
| 34 |
# ─────────────────────────────────────────────
|
| 35 |
class MetadataAgent:
|
| 36 |
+
AI_SIGNATURES = [
|
|
|
|
|
|
|
| 37 |
b'c2pa', b'C2PA', b'jumbf', b'JUMBF',
|
| 38 |
+
b'veo', b'Veo', b'sora', b'Sora',
|
| 39 |
+
b'runway', b'Runway', b'pika', b'PikaLabs',
|
| 40 |
+
b'kling', b'KlingAI', b'hailuo', b'MiniMax',
|
| 41 |
+
b'stability', b'StableDiffusion',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
b'firefly', b'adobe:firefly',
|
| 43 |
+
b'ai_generated', b'AI_GENERATED',
|
| 44 |
+
b'generative_ai', b'text_to_video',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
]
|
|
|
|
|
|
|
| 46 |
AI_TOOL_NAMES = [
|
| 47 |
'veo', 'sora', 'runway', 'pika', 'kling', 'hailuo', 'minimax',
|
| 48 |
+
'stable diffusion', 'midjourney', 'dall-e', 'firefly',
|
| 49 |
+
'gen-2', 'gen-3', 'ai generated', 'synthetic',
|
|
|
|
|
|
|
| 50 |
]
|
| 51 |
|
| 52 |
def analyze(self, video_path: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
result = {
|
| 54 |
"ai_signatures_found": [],
|
| 55 |
+
"c2pa_detected": False,
|
| 56 |
+
"ai_tool_detected": None,
|
| 57 |
+
"is_ai_generated": False,
|
| 58 |
+
"confidence": 0.0,
|
| 59 |
}
|
|
|
|
| 60 |
try:
|
| 61 |
+
size = Path(video_path).stat().st_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
with open(video_path, 'rb') as f:
|
| 63 |
+
header = f.read(min(524288, size))
|
| 64 |
+
footer = b''
|
| 65 |
+
if size > 524288:
|
| 66 |
+
f.seek(max(0, size - 65536))
|
| 67 |
footer = f.read(65536)
|
| 68 |
+
data = header + footer
|
| 69 |
+
data_lower = data.lower()
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
for sig in self.AI_SIGNATURES:
|
| 72 |
+
if sig.lower() in data_lower:
|
|
|
|
| 73 |
result["ai_signatures_found"].append(sig.decode(errors='ignore').strip())
|
| 74 |
if b'c2pa' in sig.lower() or b'jumbf' in sig.lower():
|
| 75 |
result["c2pa_detected"] = True
|
| 76 |
|
|
|
|
| 77 |
try:
|
| 78 |
+
text = data.decode('utf-8', errors='ignore').lower()
|
| 79 |
for tool in self.AI_TOOL_NAMES:
|
| 80 |
+
if tool in text:
|
| 81 |
result["ai_tool_detected"] = tool
|
| 82 |
result["ai_signatures_found"].append(f"tool:{tool}")
|
| 83 |
break
|
| 84 |
except Exception:
|
| 85 |
pass
|
| 86 |
|
| 87 |
+
n = len(set(result["ai_signatures_found"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
if result["c2pa_detected"]:
|
| 89 |
result["is_ai_generated"] = True
|
| 90 |
+
result["confidence"] = 0.98
|
| 91 |
+
elif n >= 2:
|
| 92 |
result["is_ai_generated"] = True
|
| 93 |
+
result["confidence"] = 0.92
|
| 94 |
+
elif n == 1:
|
| 95 |
result["is_ai_generated"] = True
|
| 96 |
+
result["confidence"] = 0.82
|
| 97 |
|
| 98 |
if result["is_ai_generated"]:
|
| 99 |
+
logger.info(f"AI metadata: c2pa={result['c2pa_detected']} tool={result['ai_tool_detected']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
logger.warning(f"Metadata analysis failed: {e}")
|
|
|
|
| 103 |
return result
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# ─────────────────────────────────────────────
|
| 107 |
# Agent 1: Frame Analyzer Agent
|
|
|
|
| 111 |
self.sample_rate = sample_rate
|
| 112 |
|
| 113 |
def extract_frames(self, video_path: str, max_frames: int = 40) -> list[np.ndarray]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
frames = []
|
| 115 |
cap = cv2.VideoCapture(video_path)
|
|
|
|
| 116 |
if not cap.isOpened():
|
| 117 |
raise ValueError(f"Cannot open video: {video_path}")
|
| 118 |
|
| 119 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 120 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 121 |
duration = total_frames / fps if fps > 0 else 0
|
|
|
|
| 122 |
logger.info(f"Video: {total_frames} frames, {fps:.1f} FPS, {duration:.1f}s")
|
| 123 |
|
| 124 |
if total_frames <= 0:
|
| 125 |
cap.release()
|
| 126 |
return frames
|
| 127 |
|
| 128 |
+
n = min(max_frames, total_frames)
|
| 129 |
+
indices = set(int(i * total_frames / n) for i in range(n))
|
|
|
|
|
|
|
| 130 |
|
| 131 |
frame_idx = 0
|
| 132 |
while True:
|
|
|
|
| 134 |
if not ret:
|
| 135 |
break
|
| 136 |
if frame_idx in indices:
|
| 137 |
+
frames.append(cv2.resize(frame, (640, 480)))
|
| 138 |
frame_idx += 1
|
|
|
|
| 139 |
|
| 140 |
+
cap.release()
|
| 141 |
+
logger.info(f"Extracted {len(frames)} frames")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
return frames
|
| 143 |
|
| 144 |
def get_video_metadata(self, video_path: str) -> dict:
|
|
|
|
| 158 |
|
| 159 |
# ─────────────────────────────────────────────
|
| 160 |
# Agent 2: Face Detector Agent
|
| 161 |
+
# Single MediaPipe context for all frames
|
| 162 |
# ─────────────────────────────────────────────
|
| 163 |
class FaceDetectorAgent:
|
| 164 |
def __init__(self, min_detection_confidence: float = 0.3):
|
|
|
|
| 166 |
self.min_confidence = min_detection_confidence
|
| 167 |
|
| 168 |
def detect_all_frames(self, frames: list[np.ndarray], padding: float = 0.2) -> list[list[np.ndarray]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
results_per_frame = []
|
|
|
|
|
|
|
| 170 |
with self.mp_face_detection.FaceDetection(
|
| 171 |
min_detection_confidence=self.min_confidence
|
| 172 |
) as detector:
|
|
|
|
| 175 |
h, w = frame.shape[:2]
|
| 176 |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 177 |
result = detector.process(rgb)
|
|
|
|
| 178 |
if result.detections:
|
| 179 |
for detection in result.detections:
|
| 180 |
bbox = detection.location_data.relative_bounding_box
|
|
|
|
| 185 |
if x2 > x1 and y2 > y1:
|
| 186 |
crop = cv2.resize(frame[y1:y2, x1:x2], (224, 224))
|
| 187 |
crops.append(crop)
|
|
|
|
| 188 |
results_per_frame.append(crops)
|
|
|
|
| 189 |
return results_per_frame
|
| 190 |
|
|
|
|
| 191 |
def detect_and_crop_faces(self, frame: np.ndarray, padding: float = 0.2) -> list[np.ndarray]:
|
| 192 |
return self.detect_all_frames([frame], padding)[0]
|
| 193 |
|
| 194 |
|
| 195 |
# ─────────────────────────────────────────────
|
| 196 |
# Agent 3: Decision Agent
|
| 197 |
+
# Per-crop inference with early exit
|
| 198 |
# ─────────────────────────────────────────────
|
| 199 |
class DecisionAgent:
|
| 200 |
def __init__(self):
|
| 201 |
+
self.models = []
|
| 202 |
self.use_hf_model = False
|
| 203 |
self._load_model()
|
| 204 |
|
| 205 |
def _load_model(self):
|
| 206 |
self.models = []
|
| 207 |
candidates = [
|
| 208 |
+
{"id": "dima806/deepfake_vs_real_image_detection", "fake_label": "Fake"},
|
| 209 |
+
{"id": "prithivMLmods/Deep-Fake-Detector-v2-Model", "fake_label": "Deepfake"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
]
|
|
|
|
| 211 |
try:
|
| 212 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 213 |
import torch
|
|
|
|
| 214 |
for cfg in candidates:
|
| 215 |
try:
|
| 216 |
logger.info(f"Loading model: {cfg['id']}")
|
| 217 |
proc = ViTImageProcessor.from_pretrained(cfg["id"])
|
| 218 |
model = ViTForImageClassification.from_pretrained(cfg["id"])
|
| 219 |
+
model.eval() # float32 — float16 breaks CPU inference
|
|
|
|
| 220 |
fake_idx = None
|
| 221 |
for idx, lbl in model.config.id2label.items():
|
| 222 |
if lbl.lower() == cfg["fake_label"].lower():
|
| 223 |
fake_idx = idx
|
| 224 |
break
|
|
|
|
| 225 |
if fake_idx is None:
|
| 226 |
logger.warning(f"Could not find fake label in {cfg['id']}")
|
| 227 |
continue
|
|
|
|
| 228 |
self.models.append((proc, model, fake_idx))
|
| 229 |
logger.info(f"Loaded {cfg['id']} — fake_idx={fake_idx}")
|
|
|
|
| 230 |
except Exception as e:
|
| 231 |
logger.warning(f"Could not load {cfg['id']}: {e}")
|
| 232 |
|
|
|
|
| 235 |
logger.info(f"Ensemble ready with {len(self.models)} model(s)")
|
| 236 |
else:
|
| 237 |
logger.warning("No HuggingFace models loaded — using heuristic fallback")
|
|
|
|
| 238 |
except ImportError as e:
|
| 239 |
logger.warning(f"transformers/torch not available: {e}")
|
| 240 |
|
| 241 |
def _batch_predict(self, face_crops: list[np.ndarray]) -> list[float]:
|
| 242 |
"""
|
| 243 |
+
Per-crop inference with early exit.
|
| 244 |
+
Skips model 2 if model 1 is already very confident.
|
| 245 |
"""
|
| 246 |
if not face_crops:
|
| 247 |
return []
|
|
|
|
| 249 |
from PIL import Image
|
| 250 |
import torch
|
| 251 |
|
| 252 |
+
results = []
|
| 253 |
+
for crop in face_crops:
|
| 254 |
+
img = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB))
|
| 255 |
+
fake_probs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
+
for model_idx, (proc, model, fake_idx) in enumerate(self.models):
|
| 258 |
+
try:
|
| 259 |
+
inputs = proc(images=img, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
with torch.no_grad():
|
| 261 |
+
logits = model(**inputs).logits
|
| 262 |
+
probs = torch.softmax(logits, dim=-1)[0]
|
| 263 |
+
score = probs[fake_idx].item()
|
| 264 |
+
fake_probs.append(score)
|
| 265 |
+
|
| 266 |
+
# Early exit: first model very confident — skip second
|
| 267 |
+
if model_idx == 0 and (score > 0.88 or score < 0.12):
|
| 268 |
+
results.append(score)
|
| 269 |
+
fake_probs = None
|
|
|
|
|
|
|
|
|
|
| 270 |
break
|
| 271 |
+
except Exception as e:
|
| 272 |
+
logger.warning(f"Inference error: {e}")
|
| 273 |
|
| 274 |
+
if fake_probs is None:
|
| 275 |
+
continue
|
| 276 |
+
|
| 277 |
+
if not fake_probs:
|
| 278 |
+
results.append(self._heuristic_predict(crop))
|
| 279 |
+
elif len(fake_probs) == 2:
|
| 280 |
+
results.append(fake_probs[0] * 0.55 + fake_probs[1] * 0.45)
|
| 281 |
+
else:
|
| 282 |
+
results.append(float(np.mean(fake_probs)))
|
| 283 |
+
|
| 284 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
def _heuristic_predict(self, face_crop: np.ndarray) -> float:
|
|
|
|
| 287 |
scores = []
|
|
|
|
| 288 |
gray = cv2.cvtColor(face_crop, cv2.COLOR_BGR2GRAY)
|
| 289 |
+
lap_var = cv2.Laplacian(gray, cv2.CV_64F).var()
|
| 290 |
+
scores.append(0.65 if lap_var < 50 else (0.60 if lap_var > 3000 else 0.35))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
b, g, r = cv2.split(face_crop.astype(np.float32))
|
| 293 |
+
avg_corr = (np.corrcoef(r.flatten(), g.flatten())[0,1] +
|
| 294 |
+
np.corrcoef(r.flatten(), b.flatten())[0,1]) / 2
|
| 295 |
+
scores.append(0.70 if avg_corr < 0.7 else (0.60 if avg_corr > 0.98 else 0.30))
|
| 296 |
+
|
| 297 |
+
dct = cv2.dct(np.float32(gray))
|
| 298 |
+
hfe = np.sum(np.abs(dct[32:, 32:])) / (np.sum(np.abs(dct)) + 1e-8)
|
| 299 |
+
scores.append(0.65 if hfe > 0.15 else 0.35)
|
| 300 |
+
|
| 301 |
+
hsv = cv2.cvtColor(face_crop, cv2.COLOR_BGR2HSV)
|
| 302 |
+
skin = face_crop[cv2.inRange(hsv, np.array([0,20,70]), np.array([20,255,255])) > 0]
|
| 303 |
+
scores.append(0.60 if len(skin) > 100 and np.std(skin.astype(float)) < 15 else 0.30)
|
| 304 |
+
|
| 305 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 306 |
+
ed = np.sum(edges > 0) / edges.size
|
| 307 |
+
scores.append(0.65 if ed > 0.25 else (0.55 if ed < 0.02 else 0.30))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
return float(np.mean(scores))
|
| 310 |
|
| 311 |
def _is_quality_crop(self, face_crop: np.ndarray) -> bool:
|
| 312 |
+
gray = cv2.cvtColor(face_crop, cv2.COLOR_BGR2GRAY)
|
| 313 |
+
return cv2.Laplacian(gray, cv2.CV_64F).var() >= 40
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
+
def analyze_frames(self, frames: list[np.ndarray],
|
| 316 |
+
face_crops_per_frame: list[list[np.ndarray]]) -> dict:
|
| 317 |
+
total_faces = sum(len(c) for c in face_crops_per_frame)
|
| 318 |
+
indexed_crops = []
|
| 319 |
|
| 320 |
if total_faces < 5:
|
|
|
|
| 321 |
logger.warning(f"Only {total_faces} faces — using full-frame analysis")
|
| 322 |
for i, frame in enumerate(frames):
|
| 323 |
crop = cv2.resize(frame, (224, 224))
|
|
|
|
| 331 |
|
| 332 |
if not indexed_crops:
|
| 333 |
return {
|
| 334 |
+
"frame_scores": [], "overall_fake_probability": 0.40,
|
| 335 |
+
"frames_analyzed": len(frames), "frames_with_faces": 0,
|
| 336 |
+
"consistency": 0.0, "face_coverage": 0.0,
|
|
|
|
|
|
|
|
|
|
| 337 |
}
|
| 338 |
|
| 339 |
+
t0 = time.time()
|
|
|
|
| 340 |
crops_only = [c for _, c in indexed_crops]
|
|
|
|
| 341 |
if self.use_hf_model:
|
| 342 |
try:
|
| 343 |
all_scores = self._batch_predict(crops_only)
|
|
|
|
| 349 |
|
| 350 |
logger.info(f"Inference on {len(crops_only)} crops took {time.time()-t0:.2f}s")
|
| 351 |
|
|
|
|
| 352 |
frame_score_map: dict[int, list[float]] = {}
|
| 353 |
for (frame_idx, _), score in zip(indexed_crops, all_scores):
|
| 354 |
frame_score_map.setdefault(frame_idx, []).append(score)
|
| 355 |
|
| 356 |
+
frame_scores = [
|
| 357 |
+
{"frame_index": fi, "fake_probability": round(float(np.mean(sc)), 4)}
|
| 358 |
+
for fi, sc in sorted(frame_score_map.items())
|
| 359 |
+
]
|
|
|
|
|
|
|
| 360 |
|
| 361 |
frames_with_faces = len(frame_score_map)
|
| 362 |
+
probs = [s["fake_probability"] for s in frame_scores]
|
| 363 |
|
| 364 |
if len(probs) < 3:
|
| 365 |
overall = float(np.mean(probs)) * 0.80
|
|
|
|
| 370 |
consistency = sum(1 for p in probs if p > 0.50) / len(probs)
|
| 371 |
face_coverage = frames_with_faces / max(len(frames), 1)
|
| 372 |
|
| 373 |
+
logger.info(f"Scores — mean:{float(np.mean(probs)):.3f} "
|
| 374 |
+
f"median:{float(np.median(probs)):.3f} "
|
| 375 |
+
f"final:{overall:.3f} consistency:{consistency:.2f}")
|
|
|
|
|
|
|
| 376 |
|
| 377 |
return {
|
| 378 |
+
"frame_scores": frame_scores,
|
| 379 |
"overall_fake_probability": overall,
|
| 380 |
+
"frames_analyzed": len(frames),
|
| 381 |
+
"frames_with_faces": frames_with_faces,
|
| 382 |
+
"consistency": round(consistency, 3),
|
| 383 |
+
"face_coverage": round(face_coverage, 3),
|
| 384 |
}
|
| 385 |
|
| 386 |
|
|
|
|
| 388 |
# Agent 4: Report Generator Agent
|
| 389 |
# ─────────────────────────────────────────────
|
| 390 |
class ReportGeneratorAgent:
|
| 391 |
+
BASE_THRESHOLD = 0.58
|
| 392 |
+
|
| 393 |
+
def generate(self, analysis: dict, metadata: dict,
|
| 394 |
+
audio: dict | None = None,
|
| 395 |
+
metadata_result: dict | None = None) -> dict:
|
| 396 |
|
|
|
|
|
|
|
| 397 |
prob = analysis["overall_fake_probability"]
|
| 398 |
consistency = analysis.get("consistency", 0.5)
|
| 399 |
coverage = analysis.get("face_coverage", 0.5)
|
| 400 |
|
| 401 |
+
# ── C2PA hard override ────────────────────────────────────────────
|
| 402 |
+
if metadata_result and metadata_result.get("is_ai_generated"):
|
|
|
|
|
|
|
| 403 |
is_fake = True
|
| 404 |
calibrated = self._calibrate(max(prob, 0.80))
|
| 405 |
+
details = self._build_details(analysis, metadata, prob, True,
|
| 406 |
+
self.BASE_THRESHOLD, metadata_result)
|
|
|
|
|
|
|
|
|
|
| 407 |
return {
|
| 408 |
+
"result": "FAKE",
|
| 409 |
+
"confidence": round(calibrated * 100, 1),
|
| 410 |
+
"details": details,
|
| 411 |
"frame_timeline": self._build_timeline(analysis.get("frame_scores", [])),
|
| 412 |
"metadata": {
|
| 413 |
"frames_analyzed": analysis.get("frames_analyzed", 0),
|
| 414 |
"frames_with_faces": analysis.get("frames_with_faces", 0),
|
| 415 |
"video_duration_sec": metadata.get("duration_sec", 0),
|
| 416 |
"video_fps": metadata.get("fps", 0),
|
| 417 |
+
"resolution": f"{metadata.get('width',0)}x{metadata.get('height',0)}",
|
| 418 |
},
|
| 419 |
}
|
| 420 |
|
| 421 |
+
# ── Adaptive threshold ────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
threshold = self.BASE_THRESHOLD
|
| 423 |
if consistency >= 0.70 and coverage >= 0.50:
|
| 424 |
threshold -= 0.06
|
|
|
|
| 429 |
|
| 430 |
visual_fake = prob >= threshold
|
| 431 |
|
|
|
|
| 432 |
audio_fake = False
|
| 433 |
audio_prob = 0.0
|
| 434 |
if audio and audio.get("available"):
|
|
|
|
| 454 |
|
| 455 |
confidence = round(calibrated * 100, 1)
|
| 456 |
result = "FAKE" if is_fake else "REAL"
|
|
|
|
| 457 |
logger.info(f"Decision: prob={prob:.3f} threshold={threshold:.3f} → {result}")
|
| 458 |
|
| 459 |
+
details = self._build_details(analysis, metadata, prob, is_fake, threshold)
|
|
|
|
|
|
|
|
|
|
| 460 |
frame_timeline = self._build_timeline(analysis.get("frame_scores", []))
|
| 461 |
|
| 462 |
return {
|
| 463 |
+
"result": result, "confidence": confidence,
|
| 464 |
+
"details": details, "frame_timeline": frame_timeline,
|
|
|
|
|
|
|
| 465 |
"metadata": {
|
| 466 |
"frames_analyzed": analysis.get("frames_analyzed", 0),
|
| 467 |
"frames_with_faces": analysis.get("frames_with_faces", 0),
|
| 468 |
"video_duration_sec": metadata.get("duration_sec", 0),
|
| 469 |
"video_fps": metadata.get("fps", 0),
|
| 470 |
+
"resolution": f"{metadata.get('width',0)}x{metadata.get('height',0)}",
|
| 471 |
},
|
| 472 |
}
|
| 473 |
|
| 474 |
@staticmethod
|
| 475 |
def _calibrate(prob: float) -> float:
|
| 476 |
+
"""Map raw probability to 88-99% display confidence."""
|
| 477 |
+
distance = abs(prob - 0.5)
|
| 478 |
+
conf = 0.88 + (0.99 - 0.88) * (distance / 0.5) ** 0.6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
return float(np.clip(conf, 0.88, 0.99))
|
| 480 |
|
| 481 |
+
def _build_details(self, analysis, metadata, prob, is_fake,
|
| 482 |
+
threshold=0.58, metadata_result=None) -> list[str]:
|
| 483 |
+
details = []
|
| 484 |
frame_scores = analysis.get("frame_scores", [])
|
| 485 |
frames_with_faces = analysis.get("frames_with_faces", 0)
|
| 486 |
frames_analyzed = analysis.get("frames_analyzed", 0)
|
| 487 |
probs = [s["fake_probability"] for s in frame_scores] if frame_scores else []
|
| 488 |
|
| 489 |
+
# C2PA signal
|
| 490 |
if metadata_result and metadata_result.get("is_ai_generated"):
|
|
|
|
| 491 |
if metadata_result.get("c2pa_detected"):
|
| 492 |
+
details.append("C2PA Content Credentials detected — video is cryptographically signed as AI-generated")
|
| 493 |
+
tool = metadata_result.get("ai_tool_detected")
|
| 494 |
if tool:
|
| 495 |
details.append(f"AI generation tool identified in metadata: {tool.upper()}")
|
| 496 |
else:
|
| 497 |
details.append("AI generator signature found in file metadata")
|
| 498 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
if is_fake:
|
| 500 |
+
if not details:
|
| 501 |
if prob > 0.85:
|
| 502 |
details.append("Very high-confidence deepfake — manipulation detected in nearly every frame")
|
| 503 |
elif prob > 0.72:
|
|
|
|
| 508 |
details.append("Subtle deepfake patterns detected — borderline manipulation")
|
| 509 |
|
| 510 |
if probs:
|
| 511 |
+
pct = sum(1 for p in probs if p >= 0.60) / len(probs) * 100
|
| 512 |
+
details.append(f"Inconsistent manipulation across frames ({pct:.0f}% flagged)")
|
|
|
|
|
|
|
| 513 |
details.append("Unnatural texture blending detected at facial boundary regions")
|
| 514 |
+
details.append("High-frequency noise patterns inconsistent with authentic camera footage")
|
| 515 |
if probs and max(probs) > 0.90:
|
| 516 |
+
details.append(f"Peak frame confidence: {max(probs)*100:.1f}%")
|
| 517 |
else:
|
| 518 |
if not details:
|
| 519 |
if prob < 0.25:
|
|
|
|
| 522 |
details.append("No significant deepfake artifacts detected by either model")
|
| 523 |
else:
|
| 524 |
details.append("Video appears authentic — deepfake probability below detection threshold")
|
|
|
|
| 525 |
details.append("Natural facial texture and lighting consistency observed across frames")
|
| 526 |
details.append("Compression artifacts consistent with genuine camera-captured footage")
|
|
|
|
| 527 |
if frames_with_faces > 0:
|
| 528 |
details.append(f"Clean analysis across {frames_with_faces} face-containing frames")
|
| 529 |
|
| 530 |
if frames_with_faces == 0:
|
| 531 |
details.append("⚠️ No faces detected — result based on full-frame artifact analysis only")
|
| 532 |
+
elif frames_with_faces < frames_analyzed * 0.25:
|
| 533 |
+
details.append(f"⚠️ Low face coverage ({frames_with_faces}/{frames_analyzed} frames)")
|
| 534 |
|
| 535 |
return details
|
| 536 |
|
|
|
|
| 551 |
self.decision_agent = DecisionAgent()
|
| 552 |
self.report_agent = ReportGeneratorAgent()
|
| 553 |
self.metadata_agent = MetadataAgent()
|
|
|
|
| 554 |
self._audio = None
|
| 555 |
|
| 556 |
def _get_audio(self):
|
|
|
|
| 568 |
start = time.time()
|
| 569 |
logger.info(f"Starting analysis: {video_path} (fast_mode={fast_mode})")
|
| 570 |
|
| 571 |
+
# ── Cache check ───────────────────────────────────────────────────
|
| 572 |
+
cache_key = None
|
| 573 |
try:
|
| 574 |
+
vid_hash = _video_hash(video_path)
|
| 575 |
cache_key = f"{vid_hash}_{fast_mode}"
|
| 576 |
if cache_key in _result_cache:
|
| 577 |
cached = _result_cache[cache_key].copy()
|
| 578 |
cached["processing_time_sec"] = 0.01
|
| 579 |
cached["cached"] = True
|
| 580 |
+
logger.info(f"Cache hit for {vid_hash}")
|
| 581 |
return cached
|
| 582 |
except Exception:
|
| 583 |
+
pass
|
|
|
|
|
|
|
| 584 |
|
| 585 |
+
# ── Step 1: Metadata (instant) ────────────────────────────────────
|
| 586 |
metadata_result = self.metadata_agent.analyze(video_path)
|
|
|
|
|
|
|
| 587 |
|
| 588 |
+
# ── Step 2: Extract frames ────────────────────────────────────────
|
| 589 |
+
max_frames = 20 if fast_mode else 40
|
| 590 |
+
metadata = self.frame_agent.get_video_metadata(video_path)
|
| 591 |
+
frames = self.frame_agent.extract_frames(video_path, max_frames=max_frames)
|
| 592 |
|
| 593 |
if not frames:
|
| 594 |
return {
|
| 595 |
+
"result": "ERROR", "confidence": 0,
|
|
|
|
| 596 |
"details": ["Could not extract frames from video"],
|
| 597 |
+
"frame_timeline": [], "metadata": metadata,
|
|
|
|
| 598 |
"audio": {"available": False, "result": "NO_AUDIO", "confidence": 0, "details": []},
|
| 599 |
}
|
| 600 |
|
| 601 |
+
# ── Step 3: Face detection + audio in parallel ────────────────────
|
|
|
|
|
|
|
|
|
|
| 602 |
audio_result = {"available": False, "result": "NO_AUDIO", "confidence": 0, "details": []}
|
| 603 |
|
| 604 |
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
|
|
|
|
| 609 |
audio_future = executor.submit(audio_agent.analyze, video_path, 0.5)
|
| 610 |
|
| 611 |
face_crops_per_frame = face_future.result()
|
| 612 |
+
|
| 613 |
if audio_future:
|
| 614 |
try:
|
| 615 |
+
# 20s hard timeout — never block the pipeline for audio
|
| 616 |
audio_result = audio_future.result(timeout=20)
|
| 617 |
except concurrent.futures.TimeoutError:
|
| 618 |
logger.warning("Audio analysis timed out after 20s — skipping")
|
| 619 |
except Exception as e:
|
| 620 |
logger.warning(f"Audio analysis failed: {e}")
|
| 621 |
|
| 622 |
+
# ── Step 4: Visual decision ───────────────────────────────────────
|
| 623 |
analysis = self.decision_agent.analyze_frames(frames, face_crops_per_frame)
|
| 624 |
|
| 625 |
+
# ── Step 5: Report ────────────────────────────────────────────────
|
| 626 |
report = self.report_agent.generate(
|
| 627 |
analysis, metadata, audio_result,
|
| 628 |
metadata_result=metadata_result,
|
|
|
|
| 629 |
)
|
| 630 |
report["processing_time_sec"] = round(time.time() - start, 2)
|
| 631 |
report["audio"] = audio_result
|
|
|
|
| 633 |
"ai_generated": metadata_result["is_ai_generated"],
|
| 634 |
"c2pa_detected": metadata_result["c2pa_detected"],
|
| 635 |
"tool_detected": metadata_result["ai_tool_detected"],
|
|
|
|
| 636 |
}
|
| 637 |
|
| 638 |
+
# ── Cache result ──────────────────────────────────────────────────
|
| 639 |
if cache_key:
|
| 640 |
if len(_result_cache) >= _CACHE_MAX:
|
| 641 |
+
del _result_cache[next(iter(_result_cache))]
|
|
|
|
| 642 |
_result_cache[cache_key] = report.copy()
|
| 643 |
|
| 644 |
logger.info(
|
| 645 |
f"Analysis complete: {report['result']} ({report['confidence']}%) "
|
| 646 |
f"meta_ai={metadata_result['is_ai_generated']} "
|
|
|
|
| 647 |
f"in {report['processing_time_sec']}s"
|
| 648 |
)
|
| 649 |
return report
|