Revert
Browse files
app.py
CHANGED
|
@@ -1,5 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
x = st.slider('Select a value')
|
| 4 |
-
st.write(x, 'squared is', x *x)
|
| 5 |
|
|
|
|
|
|
|
|
|
| 1 |
+
import os.path
|
| 2 |
+
import re
|
| 3 |
+
import torch
|
| 4 |
+
import time
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
import streamlit as st
|
| 8 |
+
from training.zoo.classifiers import DeepFakeClassifier
|
| 9 |
+
from kernel_utils import VideoReader, FaceExtractor, confident_strategy, predict_on_video_set
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_model():
|
| 13 |
+
path = 'weights/final_999_DeepFakeClassifier_tf_efficientnet_b7_ns_0_23'
|
| 14 |
+
model = DeepFakeClassifier(encoder="tf_efficientnet_b7_ns")
|
| 15 |
+
print("loading state dict {}".format(path))
|
| 16 |
+
checkpoint = torch.load(path, map_location="cpu")
|
| 17 |
+
state_dict = checkpoint.get("state_dict", checkpoint)
|
| 18 |
+
model.load_state_dict(
|
| 19 |
+
{re.sub("^module.", "", k): v for k, v in state_dict.items()},
|
| 20 |
+
strict=True)
|
| 21 |
+
model.eval()
|
| 22 |
+
del checkpoint
|
| 23 |
+
return model
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def write_bytesio_to_file(filename, bytesio):
|
| 27 |
+
with open(filename, "wb") as outfile:
|
| 28 |
+
outfile.write(bytesio.getbuffer())
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def load_video():
|
| 32 |
+
uploaded_file = st.file_uploader(label='Pick a video (mp4) file to test')
|
| 33 |
+
if uploaded_file is not None:
|
| 34 |
+
video_data = uploaded_file.getvalue()
|
| 35 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 36 |
+
tfile.write(video_data)
|
| 37 |
+
return tfile.name
|
| 38 |
+
else:
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def inference(model, test_video):
|
| 43 |
+
frames_per_video = 32
|
| 44 |
+
video_reader = VideoReader()
|
| 45 |
+
video_read_fn = lambda x: video_reader.read_frames(
|
| 46 |
+
x, num_frames=frames_per_video)
|
| 47 |
+
face_extractor = FaceExtractor(video_read_fn)
|
| 48 |
+
input_size = 380
|
| 49 |
+
strategy = confident_strategy
|
| 50 |
+
|
| 51 |
+
test_videos = [test_video]
|
| 52 |
+
print("Predicting {} videos".format(len(test_videos)))
|
| 53 |
+
models = [model]
|
| 54 |
+
predictions = predict_on_video_set(face_extractor=face_extractor,
|
| 55 |
+
input_size=input_size, models=models,
|
| 56 |
+
strategy=strategy,
|
| 57 |
+
frames_per_video=frames_per_video,
|
| 58 |
+
videos=test_videos,
|
| 59 |
+
num_workers=6, test_dir="test_video")
|
| 60 |
+
st.write("Prediction: ", predictions[0])
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
st.title('Deepfake video inference demo')
|
| 65 |
+
model = load_model()
|
| 66 |
+
video_data_path = load_video()
|
| 67 |
+
|
| 68 |
+
if video_data_path is not None and os.path.exists(video_data_path):
|
| 69 |
+
st.video(video_data_path)
|
| 70 |
+
|
| 71 |
+
result = st.button('Run on video')
|
| 72 |
+
if result:
|
| 73 |
+
st.write("Inference on video...")
|
| 74 |
+
stime = time.time()
|
| 75 |
+
inference(model, video_data_path)
|
| 76 |
+
st.write("Elapsed time: ", time.time() - stime, " seconds")
|
| 77 |
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
if __name__ == '__main__':
|
| 80 |
+
main()
|