-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
345 lines (232 loc) · 7.19 KB
/
Copy pathapp.py
File metadata and controls
345 lines (232 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import os
import tempfile
import streamlit as st
import matplotlib.pyplot as plt
from src.inference.video_predictor import VideoPredictor
# ==================================================
# Page Configuration
# ==================================================
st.set_page_config(
page_title="DeepShield",
page_icon="🛡️",
layout="wide"
)
st.markdown("""
<style>
.block-container{
padding-top:2rem;
padding-bottom:2rem;
}
</style>
""",
unsafe_allow_html=True)
# ==================================================
# Sidebar
# ==================================================
st.sidebar.success("DeepShield v1.0")
st.sidebar.divider()
st.sidebar.markdown("### 🤖 Model")
st.sidebar.info("EfficientNetB0\n\nFine-Tuned CNN")
st.sidebar.markdown("### 📂 Dataset")
st.sidebar.info("Celeb-DF")
st.sidebar.markdown("### 📹 Supported Formats")
st.sidebar.code("MP4\nAVI\nMOV")
st.sidebar.markdown("### 🎯 Prediction")
st.sidebar.success("Real / Fake")
st.sidebar.divider()
st.sidebar.caption("Developed by Tanmay Tawade")
# ==================================================
# Title
# ==================================================
st.title("🛡️ DeepShield")
st.subheader("AI Powered Deepfake Detection")
st.caption(
"Real-Time Video Deepfake Detection using EfficientNetB0 Fine-Tuned CNN"
)
st.info(
"""
🛡 **DeepShield** is an AI-powered system for detecting manipulated facial videos.
Workflow
📹 Upload Video
➡ Extract Frames
➡ Detect Faces
➡ CNN Classification
➡ Majority Voting
➡ Final Prediction
"""
)
st.divider()
# ==================================================
# Upload
# ==================================================
st.markdown("## 📤 Upload Video")
st.caption("Supported formats: MP4 • AVI • MOV")
uploaded_video = st.file_uploader(
"",
type=["mp4", "avi", "mov"]
)
if uploaded_video is None:
st.info("Upload a video to begin.")
st.stop()
# ==================================================
# Save Uploaded Video
# ==================================================
temp_video = tempfile.NamedTemporaryFile(
delete=False,
suffix=".mp4"
)
temp_video.write(uploaded_video.getbuffer())
temp_video.flush()
temp_video.close()
video_path = temp_video.name
# ==================================================
# Preview
# ==================================================
st.markdown("## 🎥 Video Preview")
st.video(uploaded_video.getvalue())
# ==================================================
# Analyze Button
# ==================================================
if st.button(
"🚀 Analyze Video",
use_container_width=True
):
MODEL_PATH = "models/finetune/best_cnn_finetuned.keras"
# Create progress widgets
progress_bar = st.progress(0)
status = st.empty()
def update_progress(progress, message):
progress_bar.progress(int(progress * 100))
status.info(message)
predictor = VideoPredictor(MODEL_PATH)
result = predictor.predict_video(
video_path,
progress_callback=update_progress
)
progress_bar.empty()
status.success("✅ AI Analysis Completed Successfully")
st.divider()
col1, col2 = st.columns(2)
with col1:
st.markdown("## 🤖 AI Prediction")
if result["prediction"] == "Fake":
st.error(
"🚨 Fake Video Detected\n\nThe uploaded video shows strong signs of manipulation."
)
else:
st.success(
"✅ Authentic Video\n\nNo significant manipulation was detected."
)
confidence = result["confidence"] * 100
st.markdown("### Model Confidence")
st.progress(int(confidence))
st.markdown(
f"<h3 style='text-align:center'>{confidence:.2f}%</h3>",
unsafe_allow_html=True
)
st.divider()
st.markdown("## 📊 Analysis Summary")
row1_col1, row1_col2 = st.columns(2)
with row1_col1:
st.metric(
"📷 Frames extracted",
result["frames"]
)
with row1_col2:
st.metric(
"😀 Faces detected",
result["faces"]
)
row2_col1, row2_col2, row2_col3 = st.columns(3)
with row2_col1:
st.metric(
"🟢 Real Frames",
result["real_frames"]
)
with row2_col2:
st.metric(
"🔴 Fake Frames",
result["fake_frames"]
)
with row2_col3:
st.metric(
"⏱ Processing Time",
f"{result['time']:.2f} sec"
)
st.divider()
left_col, right_col = st.columns([1, 1])
# ==================================================
# Donut Chart
# ==================================================
with left_col:
st.subheader("📊 Frame Prediction Distribution")
real = result["real_frames"]
fake = result["fake_frames"]
fig, ax = plt.subplots(figsize=(3.4, 3.4))
colors = ["#4CAF50", "#F44336"]
wedges, texts, autotexts = ax.pie(
[real, fake],
colors=colors,
startangle=90,
counterclock=False,
wedgeprops=dict(width=0.42, edgecolor="white"),
autopct=lambda p: f"{p:.1f}%" if p > 0 else "",
pctdistance=0.78,
textprops={
"fontsize":9,
"fontweight":"bold",
"color":"white"
}
)
ax.legend(
wedges,
[f"🟢 Real ({real})", f"🔴 Fake ({fake})"],
loc="lower center",
bbox_to_anchor=(0.5, -0.15),
ncol=2,
frameon=False,
fontsize=9
)
ax.set(aspect="equal")
plt.tight_layout()
st.pyplot(fig)
plt.close(fig)
# ==================================================
# Sample Faces
# ==================================================
with right_col:
st.markdown("## 🖼 Sample Analyzed Faces")
st.caption(
"Representative face crops used by the model during analysis."
)
frames = result["sample_frames"]
if len(frames) == 0:
st.info("No sample frames available.")
else:
cols = st.columns(2)
for index, frame in enumerate(frames):
with cols[index % 2]:
st.image(
frame["path"],
use_container_width=True
)
confidence = frame["confidence"] * 100
if frame["label"] == "Fake":
st.error(
f"🔴 Fake ({confidence:.2f}%)"
)
else:
st.success(
f"🟢 Real ({confidence:.2f}%)"
)
st.divider()
st.markdown(
"""
<div style="text-align:center;color:gray;font-size:13px;opacity:0.8;">
DeepShield v1.0
Powered by TensorFlow • EfficientNetB0 • OpenCV • MTCNN • Streamlit
© 2026 Tanmay Tawade
</div>
""",
unsafe_allow_html=True
)