Spaces:
Sleeping
Sleeping
Commit
·
b5a5217
1
Parent(s):
c759fb9
Add sample files
Browse files- .gitignore +1 -0
- app.py +58 -25
- file_picker.py +45 -0
- sample_files/fem_fake_img.jpg +3 -0
- sample_files/fem_real_img.jpg +3 -0
- sample_files/jokowi deepfake - cupid.mp4 +3 -0
- sample_files/messi_deepfake.mp4 +3 -0
- sample_files/obama_real.mp4 +3 -0
.gitignore
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
temps/*
|
2 |
!temps/.gitkeep
|
3 |
|
|
|
4 |
env
|
|
|
1 |
temps/*
|
2 |
!temps/.gitkeep
|
3 |
|
4 |
+
sample_files/images/*
|
5 |
env
|
app.py
CHANGED
@@ -14,55 +14,88 @@ from PIL import Image
|
|
14 |
|
15 |
import shlex
|
16 |
import subprocess
|
|
|
17 |
|
18 |
import os
|
19 |
|
|
|
|
|
20 |
def main():
|
21 |
st.markdown("###")
|
22 |
uploaded_file = st.file_uploader('Upload a picture', type=['mp4', 'jpg', 'jpeg', 'png'], accept_multiple_files=False)
|
|
|
|
|
|
|
23 |
if uploaded_file:
|
24 |
random_id = uuid.uuid1()
|
|
|
25 |
filename = "{}.{}".format(random_id, uploaded_file.type.split("/")[-1])
|
26 |
file_type = uploaded_file.type.split("/")[0]
|
|
|
27 |
|
28 |
if uploaded_file.type == 'video/mp4':
|
29 |
with open(f"temps/{filename}", mode='wb') as f:
|
30 |
f.write(uploaded_file.read())
|
31 |
st.video(uploaded_file)
|
32 |
-
pass
|
33 |
else:
|
34 |
img = Image.open(uploaded_file).convert('RGB')
|
35 |
ext = uploaded_file.type.split("/")[-1]
|
36 |
with open(f"temps/{filename}", mode='wb') as f:
|
37 |
f.write(uploaded_file.getbuffer())
|
38 |
st.image(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
st.text("")
|
65 |
-
st.text(
|
|
|
|
|
|
|
|
|
66 |
|
67 |
|
68 |
def setup():
|
|
|
14 |
|
15 |
import shlex
|
16 |
import subprocess
|
17 |
+
from file_picker import st_file_selector
|
18 |
|
19 |
import os
|
20 |
|
21 |
+
DEBUG = False
|
22 |
+
|
23 |
def main():
|
24 |
st.markdown("###")
|
25 |
uploaded_file = st.file_uploader('Upload a picture', type=['mp4', 'jpg', 'jpeg', 'png'], accept_multiple_files=False)
|
26 |
+
st.markdown("### or")
|
27 |
+
selected_file = st_file_selector(st, path='.\sample_files', key = 'selected', label = 'Choose a sample image/video')
|
28 |
+
|
29 |
if uploaded_file:
|
30 |
random_id = uuid.uuid1()
|
31 |
+
base_folder = "temps"
|
32 |
filename = "{}.{}".format(random_id, uploaded_file.type.split("/")[-1])
|
33 |
file_type = uploaded_file.type.split("/")[0]
|
34 |
+
filepath = f"{base_folder}/{filename}"
|
35 |
|
36 |
if uploaded_file.type == 'video/mp4':
|
37 |
with open(f"temps/{filename}", mode='wb') as f:
|
38 |
f.write(uploaded_file.read())
|
39 |
st.video(uploaded_file)
|
|
|
40 |
else:
|
41 |
img = Image.open(uploaded_file).convert('RGB')
|
42 |
ext = uploaded_file.type.split("/")[-1]
|
43 |
with open(f"temps/{filename}", mode='wb') as f:
|
44 |
f.write(uploaded_file.getbuffer())
|
45 |
st.image(img)
|
46 |
+
elif selected_file:
|
47 |
+
base_folder = "sample_files"
|
48 |
+
file_type = selected_file.split(".")[-1]
|
49 |
+
filename = selected_file.split("/")[-1]
|
50 |
+
filepath = f"{base_folder}/{selected_file}"
|
51 |
+
|
52 |
+
if file_type == 'mp4':
|
53 |
+
video_file = open(filepath, 'rb')
|
54 |
+
video_bytes = video_file.read()
|
55 |
+
st.video(video_bytes)
|
56 |
+
else:
|
57 |
+
image_file = open(filepath, 'rb')
|
58 |
+
image_bytes = image_file.read()
|
59 |
+
st.image(image_bytes)
|
60 |
+
else:
|
61 |
+
return
|
62 |
+
|
63 |
|
64 |
+
|
65 |
+
|
66 |
+
with st.spinner(f'Processing {file_type}...'):
|
67 |
+
processing_stdout = subprocess.run(shlex.split(f"""python extract_video.py --device cpu --max_frames 50 --bs 2 --frame_interval 60 --confidence_threshold 0.997 --data_path "{filepath}" """), capture_output=True)
|
68 |
+
st.text(f'1. Processing {file_type} ✅')
|
69 |
+
with st.spinner(f'Analyzing {file_type}...'):
|
70 |
+
analyze_stdout = subprocess.run(shlex.split(f"""python inference.py --weight weights/model_params_ffpp_c23.pickle --device cpu --image_folder "{base_folder}/images/{filename}" """), capture_output=True)
|
71 |
+
st.text(f'2. Analyzing {file_type} ✅')
|
72 |
+
|
73 |
+
if len(os.listdir("{}/images/{}".format(base_folder, filename.split(".")[0]))) < 1:
|
74 |
+
st.text("No faces could be detected! 🚨")
|
75 |
+
return
|
76 |
+
|
77 |
+
try:
|
78 |
+
fake_probability = float(analyze_stdout.stdout.decode('utf-8').split('Mean prediction: ')[-1])
|
79 |
+
if fake_probability > 0.6:
|
80 |
+
st.error(' FAKE! ', icon="🚨")
|
81 |
+
else:
|
82 |
+
st.success(" REAL FOOTAGE! ", icon="✅")
|
83 |
+
st.text("fake probability {:.2f}".format(fake_probability))
|
84 |
+
|
85 |
+
# os.remove(f"{base_folder}/{filename}")
|
86 |
+
folder_name = ".".join(filename.split(".")[:-1])
|
87 |
+
shutil.rmtree(f"{base_folder}/images/{folder_name}")
|
88 |
+
except Exception as e:
|
89 |
+
if DEBUG:
|
90 |
+
st.text(processing_stdout.stdout.decode('utf-8'))
|
91 |
+
st.text(analyze_stdout.stdout.decode('utf-8'))
|
92 |
|
93 |
st.text("")
|
94 |
+
st.text(processing_stdout)
|
95 |
+
st.text(analyze_stdout)
|
96 |
+
st.write(e)
|
97 |
+
else:
|
98 |
+
st.text("Encountered a problem while analyzing video/image 🚨")
|
99 |
|
100 |
|
101 |
def setup():
|
file_picker.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""FilePicker for streamlit.
|
2 |
+
Still doesn't seem to be a good solution for a way to select files to process from the server Streamlit is running on.
|
3 |
+
Here's a pretty functional solution.
|
4 |
+
Usage:
|
5 |
+
```
|
6 |
+
import streamlit as st
|
7 |
+
from filepicker import st_file_selector
|
8 |
+
tif_file = st_file_selector(st, key = 'tif', label = 'Choose tif file')
|
9 |
+
```
|
10 |
+
"""
|
11 |
+
|
12 |
+
import os
|
13 |
+
import streamlit as st
|
14 |
+
|
15 |
+
def update_dir(key):
|
16 |
+
choice = st.session_state[key]
|
17 |
+
if os.path.isdir(os.path.join(st.session_state[key+'curr_dir'], choice)):
|
18 |
+
st.session_state[key+'curr_dir'] = os.path.normpath(os.path.join(st.session_state[key+'curr_dir'], choice))
|
19 |
+
files = sorted(os.listdir(st.session_state[key+'curr_dir']))
|
20 |
+
files.remove("images")
|
21 |
+
st.session_state[key+'files'] = files
|
22 |
+
|
23 |
+
def st_file_selector(st_placeholder, path='.', label='Select a file/folder', key = 'selected'):
|
24 |
+
if key+'curr_dir' not in st.session_state:
|
25 |
+
base_path = '.' if path is None or path == '' else path
|
26 |
+
base_path = base_path if os.path.isdir(base_path) else os.path.dirname(base_path)
|
27 |
+
base_path = '.' if base_path is None or base_path is '' else base_path
|
28 |
+
|
29 |
+
files = sorted(os.listdir(base_path))
|
30 |
+
files.insert(0, 'Choose a file...')
|
31 |
+
files.remove("images")
|
32 |
+
st.session_state[key+'files'] = files
|
33 |
+
st.session_state[key+'curr_dir'] = base_path
|
34 |
+
else:
|
35 |
+
base_path = st.session_state[key+'curr_dir']
|
36 |
+
|
37 |
+
selected_file = st_placeholder.selectbox(label=label,
|
38 |
+
options=st.session_state[key+'files'],
|
39 |
+
key=key,
|
40 |
+
on_change = lambda: update_dir(key))
|
41 |
+
|
42 |
+
if selected_file == "Choose a file...":
|
43 |
+
return None
|
44 |
+
|
45 |
+
return selected_file
|
sample_files/fem_fake_img.jpg
ADDED
![]() |
Git LFS Details
|
sample_files/fem_real_img.jpg
ADDED
![]() |
Git LFS Details
|
sample_files/jokowi deepfake - cupid.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:83f057b648c76a1d1a92224767222cf9a74f8d8849ea4979a0201d0d8e14d906
|
3 |
+
size 2163429
|
sample_files/messi_deepfake.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:88ac25406e754c467b50686961fae2c7975eb816dfe8dc2974304ac9d57510ac
|
3 |
+
size 2520688
|
sample_files/obama_real.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c5fb30fd607ed6395c56644eec2dbe71e32354c69e68c52d667e25645b648050
|
3 |
+
size 4856656
|