-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (80 loc) · 3.39 KB
/
main.py
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
import streamlit as st
import cv2
import numpy as np
from tempfile import NamedTemporaryFile
from tensorflow.lite.python.interpreter import Interpreter
from PIL import Image
from datetime import datetime
import os
# Load the TFLite model
interpreter = Interpreter(model_path="deepfake_detection_model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Function to preprocess video frames
def preprocess_frames(frames):
preprocessed_frames = []
for frame in frames:
frame = cv2.resize(frame, (224, 224))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
preprocessed_frames.append(frame)
return np.array(preprocessed_frames)
# Function to extract frames from video
def extract_frames(video_bytes, num_frames=10):
frames = []
with NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
tmp_file.write(video_bytes)
tmp_file_path = tmp_file.name
cap = cv2.VideoCapture(tmp_file_path)
while len(frames) < num_frames:
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
cap.release()
# Delete temporary file
os.unlink(tmp_file_path)
return frames
# Function to predict if the video is real or deepfake
def predict_video(uploaded_file, status_log):
video_bytes = uploaded_file.read() # Read byte data from file object
frames = extract_frames(video_bytes, num_frames=10)
preprocessed_frames = preprocess_frames(frames)
preprocessed_frames = preprocessed_frames.reshape((1, 10, 224, 224, 3))
# Run inference
interpreter.set_tensor(input_details[0]['index'], preprocessed_frames.astype('float32'))
interpreter.invoke()
prediction = interpreter.get_tensor(output_details[0]['index'])
accuracy = prediction[0][0] if prediction[0][0] > 0.5 else 1 - prediction[0][0]
result = "Deepfake" if prediction[0][0] > 0.5 else "Real"
status_log.text(f"[{timestamp}] - The video is {result} with {accuracy * 100:.2f}% accuracy.\n")
return result, accuracy
def main():
st.title("Deepfake Detection App")
# Section 1: Title and Navigation Bar
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to:", ("Home", "About"))
# Section 2: Video and Model Detection
if page == "Home":
st.header("Video Player and Model Detection")
uploaded_file = st.file_uploader("Upload a video file:", type=["mp4"])
# Logs Section
st.header("Logs:")
status_log = st.empty()
if uploaded_file is not None:
status_log.text(f"[{timestamp}] - Video uploaded successfully.\n")
st.video(uploaded_file)
if st.button("Detect"):
status_log.text(f"[{timestamp}] - Detecting video...\n")
result, accuracy = predict_video(uploaded_file, status_log)
# Table displaying model name, live accuracy, and status
st.subheader("Model Detection Status")
data = {"Model Name": ["Resnet+LSTM"], "Accuracy": [f"{accuracy * 100:.2f}%"], "Status": [result]}
st.table(data)
# Section 3: Log Section
elif page == "About":
st.header("Log Section")
st.write("System logs will be displayed here.")
if __name__ == "__main__":
main()