Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +100 -0
- deepfake_detection_model.png +0 -0
- deepfake_detection_model.tflite +3 -0
- image-1.png +0 -0
- image.png +0 -0
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tempfile import NamedTemporaryFile
|
| 5 |
+
from tensorflow.lite.python.interpreter import Interpreter
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Load the TFLite model
|
| 11 |
+
interpreter = Interpreter(model_path="deepfake_detection_model.tflite")
|
| 12 |
+
interpreter.allocate_tensors()
|
| 13 |
+
input_details = interpreter.get_input_details()
|
| 14 |
+
output_details = interpreter.get_output_details()
|
| 15 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 16 |
+
|
| 17 |
+
# Function to preprocess video frames
|
| 18 |
+
def preprocess_frames(frames):
|
| 19 |
+
preprocessed_frames = []
|
| 20 |
+
for frame in frames:
|
| 21 |
+
frame = cv2.resize(frame, (224, 224))
|
| 22 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 23 |
+
preprocessed_frames.append(frame)
|
| 24 |
+
return np.array(preprocessed_frames)
|
| 25 |
+
|
| 26 |
+
# Function to extract frames from video
|
| 27 |
+
def extract_frames(video_bytes, num_frames=10):
|
| 28 |
+
frames = []
|
| 29 |
+
with NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
| 30 |
+
tmp_file.write(video_bytes)
|
| 31 |
+
tmp_file_path = tmp_file.name
|
| 32 |
+
|
| 33 |
+
cap = cv2.VideoCapture(tmp_file_path)
|
| 34 |
+
while len(frames) < num_frames:
|
| 35 |
+
ret, frame = cap.read()
|
| 36 |
+
if not ret:
|
| 37 |
+
break
|
| 38 |
+
frames.append(frame)
|
| 39 |
+
cap.release()
|
| 40 |
+
|
| 41 |
+
# Delete temporary file
|
| 42 |
+
os.unlink(tmp_file_path)
|
| 43 |
+
|
| 44 |
+
return frames
|
| 45 |
+
|
| 46 |
+
# Function to predict if the video is real or deepfake
|
| 47 |
+
def predict_video(uploaded_file, status_log):
|
| 48 |
+
video_bytes = uploaded_file.read() # Read byte data from file object
|
| 49 |
+
frames = extract_frames(video_bytes, num_frames=10)
|
| 50 |
+
preprocessed_frames = preprocess_frames(frames)
|
| 51 |
+
preprocessed_frames = preprocessed_frames.reshape((1, 10, 224, 224, 3))
|
| 52 |
+
|
| 53 |
+
# Run inference
|
| 54 |
+
interpreter.set_tensor(input_details[0]['index'], preprocessed_frames.astype('float32'))
|
| 55 |
+
interpreter.invoke()
|
| 56 |
+
prediction = interpreter.get_tensor(output_details[0]['index'])
|
| 57 |
+
|
| 58 |
+
accuracy = prediction[0][0] if prediction[0][0] > 0.5 else 1 - prediction[0][0]
|
| 59 |
+
result = "Deepfake" if prediction[0][0] > 0.5 else "Real"
|
| 60 |
+
|
| 61 |
+
status_log.text(f"[{timestamp}] - The video is {result} with {accuracy * 100:.2f}% accuracy.\n")
|
| 62 |
+
|
| 63 |
+
return result, accuracy
|
| 64 |
+
|
| 65 |
+
def main():
|
| 66 |
+
st.title("Deepfake Detection App")
|
| 67 |
+
|
| 68 |
+
# Section 1: Title and Navigation Bar
|
| 69 |
+
st.sidebar.title("Navigation")
|
| 70 |
+
page = st.sidebar.radio("Go to:", ("Home", "About"))
|
| 71 |
+
|
| 72 |
+
# Section 2: Video and Model Detection
|
| 73 |
+
if page == "Home":
|
| 74 |
+
st.header("Video Player and Model Detection")
|
| 75 |
+
uploaded_file = st.file_uploader("Upload a video file:", type=["mp4"])
|
| 76 |
+
|
| 77 |
+
# Logs Section
|
| 78 |
+
st.header("Logs:")
|
| 79 |
+
status_log = st.empty()
|
| 80 |
+
|
| 81 |
+
if uploaded_file is not None:
|
| 82 |
+
status_log.text(f"[{timestamp}] - Video uploaded successfully.\n")
|
| 83 |
+
st.video(uploaded_file)
|
| 84 |
+
|
| 85 |
+
if st.button("Detect"):
|
| 86 |
+
status_log.text(f"[{timestamp}] - Detecting video...\n")
|
| 87 |
+
result, accuracy = predict_video(uploaded_file, status_log)
|
| 88 |
+
|
| 89 |
+
# Table displaying model name, live accuracy, and status
|
| 90 |
+
st.subheader("Model Detection Status")
|
| 91 |
+
data = {"Model Name": ["Resnet+LSTM"], "Accuracy": [f"{accuracy * 100:.2f}%"], "Status": [result]}
|
| 92 |
+
st.table(data)
|
| 93 |
+
|
| 94 |
+
# Section 3: Log Section
|
| 95 |
+
elif page == "About":
|
| 96 |
+
st.header("Log Section")
|
| 97 |
+
st.write("System logs will be displayed here.")
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|
deepfake_detection_model.png
ADDED
|
deepfake_detection_model.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:efc20630d33891ee1a3dff4085382bcd511ff073def94c67a5a25da116e1536e
|
| 3 |
+
size 26305120
|
image-1.png
ADDED
|
image.png
ADDED
|