Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
from tensorflow.keras import layers, models
|
4 |
from tensorflow.keras.applications import Xception
|
5 |
import cv2
|
6 |
import numpy as np
|
|
|
7 |
|
8 |
def build_deepfake_detection_model():
|
9 |
cnn_base = Xception(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
|
@@ -21,7 +24,7 @@ def build_deepfake_detection_model():
|
|
21 |
model = models.Model(inputs=input_layer, outputs=output)
|
22 |
return model
|
23 |
|
24 |
-
# Load the model (
|
25 |
model = build_deepfake_detection_model()
|
26 |
model.load_weights('dfdc_cnn_lstm_model_finetuned.keras')
|
27 |
|
@@ -40,25 +43,35 @@ def process_video(video_path):
|
|
40 |
|
41 |
def predict_deepfake(video):
|
42 |
frames = process_video(video)
|
|
|
43 |
predictions = []
|
44 |
-
|
|
|
|
|
45 |
frame = np.expand_dims(frame, axis=0) # Add batch dimension
|
46 |
frame = np.expand_dims(frame, axis=0) # Add time dimension
|
47 |
prediction = model.predict(frame)
|
48 |
predictions.append(prediction[0][0])
|
|
|
|
|
|
|
|
|
49 |
|
|
|
50 |
avg_prediction = np.mean(predictions)
|
51 |
result = "Real" if avg_prediction > 0.5 else "Fake"
|
52 |
confidence = avg_prediction if result == "Real" else 1 - avg_prediction
|
53 |
|
54 |
-
|
|
|
55 |
|
56 |
iface = gr.Interface(
|
57 |
fn=predict_deepfake,
|
58 |
inputs=gr.Video(),
|
59 |
outputs="text",
|
60 |
title="Deepfake Detection",
|
61 |
-
description="Upload a video to check if it's a deepfake or not."
|
|
|
62 |
)
|
63 |
|
64 |
-
iface.launch()
|
|
|
1 |
+
|
2 |
+
##model.load_weights('dfdc_cnn_lstm_model_finetuned.keras')
|
3 |
import gradio as gr
|
4 |
import tensorflow as tf
|
5 |
from tensorflow.keras import layers, models
|
6 |
from tensorflow.keras.applications import Xception
|
7 |
import cv2
|
8 |
import numpy as np
|
9 |
+
import time
|
10 |
|
11 |
def build_deepfake_detection_model():
|
12 |
cnn_base = Xception(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
|
|
|
24 |
model = models.Model(inputs=input_layer, outputs=output)
|
25 |
return model
|
26 |
|
27 |
+
# Load the model (ensure to replace the path with your actual model weights)
|
28 |
model = build_deepfake_detection_model()
|
29 |
model.load_weights('dfdc_cnn_lstm_model_finetuned.keras')
|
30 |
|
|
|
43 |
|
44 |
def predict_deepfake(video):
|
45 |
frames = process_video(video)
|
46 |
+
total_frames = len(frames)
|
47 |
predictions = []
|
48 |
+
|
49 |
+
# Process video frame by frame and yield progress
|
50 |
+
for i, frame in enumerate(frames):
|
51 |
frame = np.expand_dims(frame, axis=0) # Add batch dimension
|
52 |
frame = np.expand_dims(frame, axis=0) # Add time dimension
|
53 |
prediction = model.predict(frame)
|
54 |
predictions.append(prediction[0][0])
|
55 |
+
|
56 |
+
# Calculate progress and yield the status update
|
57 |
+
progress = (i + 1) / total_frames * 100
|
58 |
+
yield f"Processing video: {progress:.2f}%"
|
59 |
|
60 |
+
# After processing all frames, compute the final result
|
61 |
avg_prediction = np.mean(predictions)
|
62 |
result = "Real" if avg_prediction > 0.5 else "Fake"
|
63 |
confidence = avg_prediction if result == "Real" else 1 - avg_prediction
|
64 |
|
65 |
+
# Final result
|
66 |
+
yield f"{result} with {confidence:.2%} confidence"
|
67 |
|
68 |
iface = gr.Interface(
|
69 |
fn=predict_deepfake,
|
70 |
inputs=gr.Video(),
|
71 |
outputs="text",
|
72 |
title="Deepfake Detection",
|
73 |
+
description="Upload a video to check if it's a deepfake or not.",
|
74 |
+
live=True # This allows real-time progress updates in Gradio
|
75 |
)
|
76 |
|
77 |
+
iface.launch()
|