DegenGamer1702 commited on
Commit
a3c7385
·
verified ·
1 Parent(s): 713af0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -7,19 +7,19 @@ import os
7
  # ==================================================
8
  # CONFIGURATION
9
  # ==================================================
10
- MODEL_PATH = "./models/deepfake_detector_multi_input.h5"
11
  IMG_SIZE = (224, 224)
12
- NUM_FRAMES = 20 # frames sampled per video
13
  CASCADE_PATH = cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
14
 
15
  # ==================================================
16
- # MODEL LOADING
17
  # ==================================================
18
  print("[INFO] Loading TensorFlow model...")
19
  model = tf.keras.models.load_model(MODEL_PATH)
20
  print("[INFO] Model loaded successfully!")
21
 
22
- # Enable GPU memory growth
23
  gpus = tf.config.experimental.list_physical_devices('GPU')
24
  if gpus:
25
  for g in gpus:
@@ -29,10 +29,10 @@ if gpus:
29
  face_cascade = cv2.CascadeClassifier(CASCADE_PATH)
30
 
31
  # ==================================================
32
- # FACE EXTRACTION
33
  # ==================================================
34
  def extract_faces(video_path, num_frames=NUM_FRAMES, size=IMG_SIZE):
35
- """Extract faces (or full frames if none detected) from uploaded video."""
36
  cap = cv2.VideoCapture(video_path)
37
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
38
  step = max(1, total_frames // num_frames)
@@ -61,17 +61,25 @@ def extract_faces(video_path, num_frames=NUM_FRAMES, size=IMG_SIZE):
61
  return np.array(frames)
62
 
63
  # ==================================================
64
- # PREDICTION PIPELINE
65
  # ==================================================
66
  def predict(video):
67
  if not video:
68
  return "⚠️ Please upload a video."
 
69
  try:
70
  frames = extract_faces(video)
71
- preds = model.predict(frames, verbose=0)
 
 
 
 
 
72
  score = float(np.mean(preds))
73
  label = "🧠 FAKE" if score > 0.5 else "✅ REAL"
 
74
  return f"**Prediction:** {label}\nConfidence: {score:.2f}"
 
75
  except Exception as e:
76
  return f"❌ Error processing video: {e}"
77
 
@@ -82,11 +90,11 @@ demo = gr.Interface(
82
  fn=predict,
83
  inputs=gr.Video(label="🎥 Upload a short video (≤ 20 s)"),
84
  outputs=gr.Markdown(),
85
- title="Deepfake Detection Demo",
86
  description=(
87
- "Upload a short video clip to test the model. "
88
- "The app extracts faces, runs inference on each frame, "
89
- "and reports the average confidence for REAL vs FAKE."
90
  ),
91
  )
92
 
 
7
  # ==================================================
8
  # CONFIGURATION
9
  # ==================================================
10
+ MODEL_PATH = "./models/deepfake_detector_multi_input_v3_robust.h5"
11
  IMG_SIZE = (224, 224)
12
+ NUM_FRAMES = 20 # number of frames sampled per video
13
  CASCADE_PATH = cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
14
 
15
  # ==================================================
16
+ # LOAD MODEL
17
  # ==================================================
18
  print("[INFO] Loading TensorFlow model...")
19
  model = tf.keras.models.load_model(MODEL_PATH)
20
  print("[INFO] Model loaded successfully!")
21
 
22
+ # Enable GPU memory growth (optional, prevents OOM)
23
  gpus = tf.config.experimental.list_physical_devices('GPU')
24
  if gpus:
25
  for g in gpus:
 
29
  face_cascade = cv2.CascadeClassifier(CASCADE_PATH)
30
 
31
  # ==================================================
32
+ # HELPER: Extract faces
33
  # ==================================================
34
  def extract_faces(video_path, num_frames=NUM_FRAMES, size=IMG_SIZE):
35
+ """Extract faces (or full frames if no faces detected) from a video."""
36
  cap = cv2.VideoCapture(video_path)
37
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
38
  step = max(1, total_frames // num_frames)
 
61
  return np.array(frames)
62
 
63
  # ==================================================
64
+ # PREDICTION FUNCTION
65
  # ==================================================
66
  def predict(video):
67
  if not video:
68
  return "⚠️ Please upload a video."
69
+
70
  try:
71
  frames = extract_faces(video)
72
+
73
+ # Dummy handcrafted feature vector (3 features per frame)
74
+ dummy_features = np.zeros((frames.shape[0], 3), dtype=np.float32)
75
+
76
+ # Run inference with both inputs
77
+ preds = model.predict([frames, dummy_features], verbose=0)
78
  score = float(np.mean(preds))
79
  label = "🧠 FAKE" if score > 0.5 else "✅ REAL"
80
+
81
  return f"**Prediction:** {label}\nConfidence: {score:.2f}"
82
+
83
  except Exception as e:
84
  return f"❌ Error processing video: {e}"
85
 
 
90
  fn=predict,
91
  inputs=gr.Video(label="🎥 Upload a short video (≤ 20 s)"),
92
  outputs=gr.Markdown(),
93
+ title="Deepfake Detection Demo (Video + Dummy Features)",
94
  description=(
95
+ "Upload a short video to test the model. "
96
+ "This demo uses your trained multimodal deepfake detector, "
97
+ "with dummy blink features for quick inference."
98
  ),
99
  )
100