Update src/streamlit_app.py
Browse files- src/streamlit_app.py +1 -147
src/streamlit_app.py
CHANGED
|
@@ -1,159 +1,13 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
import tempfile
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
-
# VASR pipeline imports
|
| 7 |
-
from generate import generate_face_image
|
| 8 |
-
from faceswap import face_swap_multiple_identities
|
| 9 |
-
from video_utils import extract_frames, recombine_frames
|
| 10 |
-
from face_detection import detect_faces_insightface_from_frames
|
| 11 |
-
from grouping import group_faces_by_identity_facenet_fixed, split_csv_by_identity
|
| 12 |
-
from load_models import warm_up_models
|
| 13 |
-
from main import fix_faces
|
| 14 |
-
from image_utils import add_watermark_to_frames
|
| 15 |
-
|
| 16 |
-
# Streamlit config
|
| 17 |
-
st.set_page_config(page_title="VASR", layout="centered")
|
| 18 |
-
st.title("𧬠VASR - Video Anonymisation")
|
| 19 |
-
|
| 20 |
-
# Create a Hugging Face writable temp directory
|
| 21 |
HF_TMP = tempfile.mkdtemp()
|
| 22 |
|
| 23 |
-
# Define output paths
|
| 24 |
-
frames_folder = os.path.join(HF_TMP, "frames")
|
| 25 |
-
csv_path = os.path.join(HF_TMP, "frames_detections.csv")
|
| 26 |
-
output_faces = os.path.join(HF_TMP, "output_faces")
|
| 27 |
-
grouped_faces_dir = os.path.join(HF_TMP, "grouped_faces_facenet")
|
| 28 |
-
identity_csv_dir = os.path.join(HF_TMP, "identity_csvs")
|
| 29 |
-
output_frames = os.path.join(HF_TMP, "output_frames")
|
| 30 |
-
output_video_path = os.path.join(HF_TMP, "anonymised_output.mp4")
|
| 31 |
-
|
| 32 |
-
# UI elements
|
| 33 |
uploaded_video = st.file_uploader("πΉ Upload video file", type=["mp4", "mov"])
|
| 34 |
-
num_ids = st.number_input("π₯ Number of identities", min_value=1, value=2)
|
| 35 |
-
|
| 36 |
if uploaded_video is not None:
|
| 37 |
-
#video_bytes = uploaded_video.read() # β
read once
|
| 38 |
-
#st.write("Uploaded size:", len(video_bytes))
|
| 39 |
-
|
| 40 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4", dir=HF_TMP) as tmp_file:
|
| 41 |
-
tmp_file.write(
|
| 42 |
input_video_path = tmp_file.name
|
| 43 |
|
| 44 |
st.video(input_video_path)
|
| 45 |
st.success("β
Video uploaded and saved.")
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if st.button("π Start"):
|
| 49 |
-
progress = st.progress(0)
|
| 50 |
-
|
| 51 |
-
with st.spinner("π§ Warming up models..."):
|
| 52 |
-
warm_up_models()
|
| 53 |
-
progress.progress(10)
|
| 54 |
-
|
| 55 |
-
with st.spinner("π½ Extracting frames..."):
|
| 56 |
-
extract_frames(input_video_path, frames_folder)
|
| 57 |
-
progress.progress(30)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
with st.spinner("π΅οΈ Detecting faces..."):
|
| 61 |
-
detect_faces_insightface_from_frames(
|
| 62 |
-
frames_folder,
|
| 63 |
-
csv_path,
|
| 64 |
-
streamlit_progress=progress,
|
| 65 |
-
progress_range=(30, 50)
|
| 66 |
-
)
|
| 67 |
-
progress.progress(50)
|
| 68 |
-
|
| 69 |
-
with st.spinner("π₯ Grouping identities..."):
|
| 70 |
-
group_faces_by_identity_facenet_fixed(csv_path, output_faces, grouped_faces_dir)
|
| 71 |
-
split_csv_by_identity(csv_path, grouped_faces_dir, identity_csv_dir)
|
| 72 |
-
progress.progress(60)
|
| 73 |
-
|
| 74 |
-
st.session_state.identity_csv_paths = [
|
| 75 |
-
os.path.join(identity_csv_dir, f) for f in sorted(os.listdir(identity_csv_dir)) if f.endswith(".csv")
|
| 76 |
-
]
|
| 77 |
-
st.session_state.grouped_faces_dir = grouped_faces_dir
|
| 78 |
-
st.session_state.generated_faces = [None] * num_ids
|
| 79 |
-
st.session_state.identity_index = 0
|
| 80 |
-
st.session_state.video_ready = True
|
| 81 |
-
|
| 82 |
-
st.success("β
Ready to generate anonymised faces.")
|
| 83 |
-
progress.progress(70)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# Identity selection loop
|
| 89 |
-
if "generated_faces" in st.session_state:
|
| 90 |
-
current_index = st.session_state.identity_index
|
| 91 |
-
total_faces = len(st.session_state.generated_faces)
|
| 92 |
-
|
| 93 |
-
if current_index < total_faces:
|
| 94 |
-
st.header(f"𧬠Identity {current_index + 1}/{total_faces}")
|
| 95 |
-
|
| 96 |
-
# Show original sample image
|
| 97 |
-
sample_dir = os.path.join(grouped_faces_dir, f"identity_{current_index}")
|
| 98 |
-
sample_path = None
|
| 99 |
-
|
| 100 |
-
if os.path.exists(sample_dir):
|
| 101 |
-
image_files = sorted([
|
| 102 |
-
f for f in os.listdir(sample_dir)
|
| 103 |
-
if f.lower().endswith(('.jpg', '.png'))
|
| 104 |
-
])
|
| 105 |
-
if image_files:
|
| 106 |
-
sample_path = os.path.join(sample_dir, image_files[0])
|
| 107 |
-
else:
|
| 108 |
-
st.warning(f"β Folder not found: {sample_dir}")
|
| 109 |
-
|
| 110 |
-
if sample_path and os.path.exists(sample_path):
|
| 111 |
-
st.image(sample_path, caption="π Original Face", width=256)
|
| 112 |
-
else:
|
| 113 |
-
st.warning("β οΈ No sample image found for this identity.")
|
| 114 |
-
|
| 115 |
-
# Show current generated face if exists
|
| 116 |
-
if st.session_state.generated_faces[current_index] is not None:
|
| 117 |
-
st.image(st.session_state.generated_faces[current_index], caption="𧬠Generated Face", width=256)
|
| 118 |
-
|
| 119 |
-
# Generate new face button
|
| 120 |
-
if st.button("π Generate New Identity"):
|
| 121 |
-
with st.spinner("π§ Generating new face..."):
|
| 122 |
-
new_face = generate_face_image()
|
| 123 |
-
st.session_state.generated_faces[current_index] = new_face
|
| 124 |
-
st.image(new_face, caption="𧬠Generated Face", width=256)
|
| 125 |
-
|
| 126 |
-
# Confirm and move to next identity
|
| 127 |
-
if st.button("β
Confirm & Next Identity"):
|
| 128 |
-
if st.session_state.generated_faces[current_index] is None:
|
| 129 |
-
st.warning("β οΈ Please generate a face before continuing.")
|
| 130 |
-
else:
|
| 131 |
-
st.session_state.identity_index += 1
|
| 132 |
-
|
| 133 |
-
# If all identities confirmed, show anonymise option
|
| 134 |
-
if current_index >= total_faces:
|
| 135 |
-
st.success("β
All identities reviewed.")
|
| 136 |
-
if st.button("π Anonymise Video"):
|
| 137 |
-
|
| 138 |
-
swap_progress = st.progress(0)
|
| 139 |
-
with st.spinner("π Fixing CSVs..."):
|
| 140 |
-
identity_csvs = fix_faces(total_faces, st.session_state.identity_csv_paths)
|
| 141 |
-
swap_progress.progress(1)
|
| 142 |
-
with st.spinner("π€ Swapping faces..."):
|
| 143 |
-
face_swap_multiple_identities(
|
| 144 |
-
frame_folder=frames_folder,
|
| 145 |
-
output_folder=output_frames,
|
| 146 |
-
generated_images=st.session_state.generated_faces,
|
| 147 |
-
identity_csv_paths=identity_csvs,
|
| 148 |
-
streamlit_progress=swap_progress,
|
| 149 |
-
progress_range=(1, 96)
|
| 150 |
-
)
|
| 151 |
-
add_watermark_to_frames(output_frames,output_frames)
|
| 152 |
-
|
| 153 |
-
swap_progress.progress(96)
|
| 154 |
-
with st.spinner("π Recombining video..."):
|
| 155 |
-
recombine_frames(input_video_path, frames_folder=output_frames, output_video=output_video_path)
|
| 156 |
-
swap_progress.progress(100)
|
| 157 |
-
|
| 158 |
-
st.success("π Anonymised video complete!")
|
| 159 |
-
st.video(output_video_path, format="video/mp4")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import tempfile
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
HF_TMP = tempfile.mkdtemp()
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
uploaded_video = st.file_uploader("πΉ Upload video file", type=["mp4", "mov"])
|
|
|
|
|
|
|
| 7 |
if uploaded_video is not None:
|
|
|
|
|
|
|
|
|
|
| 8 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4", dir=HF_TMP) as tmp_file:
|
| 9 |
+
tmp_file.write(uploaded_video.read())
|
| 10 |
input_video_path = tmp_file.name
|
| 11 |
|
| 12 |
st.video(input_video_path)
|
| 13 |
st.success("β
Video uploaded and saved.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|