Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
###
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
|
@@ -7,114 +6,134 @@ from insightface.app import FaceAnalysis
|
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
|
| 11 |
-
app = FaceAnalysis(name='buffalo_l')
|
| 12 |
-
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def swap_faces_in_video(image, video, progress):
|
| 18 |
-
"""
|
| 19 |
-
Swaps faces from a source image with faces detected in a video and returns the path to the output video file.
|
| 20 |
-
|
| 21 |
-
image: Source image (as an array)
|
| 22 |
-
video: Path to the input video file
|
| 23 |
-
progress: Streamlit progress object
|
| 24 |
-
"""
|
| 25 |
source_faces = app.get(image)
|
| 26 |
-
|
| 27 |
if len(source_faces) == 0:
|
| 28 |
-
st.error("No face detected in the source image.")
|
| 29 |
return None
|
| 30 |
|
| 31 |
source_face = source_faces[0]
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
# Open the video file
|
| 37 |
cap = cv2.VideoCapture(video)
|
| 38 |
|
| 39 |
-
# Get video properties for output
|
| 40 |
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 41 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 42 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 43 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
fourcc = cv2.VideoWriter_fourcc(*'
|
| 47 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 48 |
|
| 49 |
-
|
|
|
|
| 50 |
ret, frame = cap.read()
|
| 51 |
if not ret:
|
| 52 |
-
break
|
| 53 |
|
| 54 |
-
# Detect faces in the current frame
|
| 55 |
target_faces = app.get(frame)
|
| 56 |
-
|
| 57 |
-
# Create a copy of the frame for the result
|
| 58 |
result_frame = frame.copy()
|
| 59 |
|
| 60 |
-
# Swap faces for each detected face in the video frame
|
| 61 |
for target_face in target_faces:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
# Write the result frame to the output video
|
| 65 |
out.write(result_frame)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
# Release resources
|
| 71 |
cap.release()
|
| 72 |
out.release()
|
| 73 |
-
|
| 74 |
return output_path
|
| 75 |
|
|
|
|
| 76 |
# Streamlit UI
|
| 77 |
-
|
| 78 |
-
st.
|
|
|
|
| 79 |
|
| 80 |
-
#
|
| 81 |
image_file = st.file_uploader("Upload Source Image", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
if st.button("Swap Faces"):
|
| 87 |
-
if image_file is not None and video_file is not None:
|
| 88 |
-
# Read the source image
|
| 89 |
-
source_image = cv2.imdecode(np.frombuffer(image_file.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 90 |
-
|
| 91 |
-
# Save the uploaded video temporarily
|
| 92 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
|
| 93 |
tmp_video.write(video_file.read())
|
| 94 |
tmp_video_path = tmp_video.name
|
| 95 |
|
| 96 |
-
|
| 97 |
-
with st.spinner("Processing video..."):
|
| 98 |
progress_bar = st.progress(0)
|
| 99 |
output_video_path = swap_faces_in_video(source_image, tmp_video_path, progress_bar)
|
| 100 |
|
| 101 |
if output_video_path:
|
| 102 |
-
st.success("Face swapping completed!")
|
| 103 |
-
|
| 104 |
st.video(output_video_path)
|
| 105 |
|
| 106 |
-
# Provide an option to download the processed video
|
| 107 |
with open(output_video_path, "rb") as f:
|
| 108 |
st.download_button(
|
| 109 |
-
label="Download Processed Video",
|
| 110 |
data=f,
|
| 111 |
-
file_name="output_swapped_video.
|
| 112 |
-
mime="video/
|
| 113 |
)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
os.remove(tmp_video_path)
|
| 117 |
-
# Optionally
|
| 118 |
-
# os.remove(output_video_path)
|
| 119 |
else:
|
| 120 |
-
st.error("Please upload both
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
|
|
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="Face Swapper", layout="centered")
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# ------------------------------
|
| 12 |
+
# Sidebar options
|
| 13 |
+
# ------------------------------
|
| 14 |
+
st.sidebar.title("⚙️ Settings")
|
| 15 |
|
| 16 |
+
# CPU / GPU selection
|
| 17 |
+
device_option = st.sidebar.radio("Choose Device", ["CPU", "GPU"], index=0)
|
| 18 |
+
ctx_id = 0 if device_option == "GPU" else -1
|
| 19 |
+
|
| 20 |
+
# ------------------------------
|
| 21 |
+
# Load models
|
| 22 |
+
# ------------------------------
|
| 23 |
+
@st.cache_resource
|
| 24 |
+
def load_models(ctx_id):
|
| 25 |
+
app = FaceAnalysis(name='buffalo_l')
|
| 26 |
+
app.prepare(ctx_id=ctx_id, det_size=(640, 640))
|
| 27 |
+
|
| 28 |
+
swapper = insightface.model_zoo.get_model(
|
| 29 |
+
'inswapper_128.onnx', download=False, download_zip=False
|
| 30 |
+
)
|
| 31 |
+
return app, swapper
|
| 32 |
+
|
| 33 |
+
app, swapper = load_models(ctx_id)
|
| 34 |
+
|
| 35 |
+
# ------------------------------
|
| 36 |
+
# Face swapping function
|
| 37 |
+
# ------------------------------
|
| 38 |
def swap_faces_in_video(image, video, progress):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
source_faces = app.get(image)
|
|
|
|
| 40 |
if len(source_faces) == 0:
|
| 41 |
+
st.error("❌ No face detected in the source image.")
|
| 42 |
return None
|
| 43 |
|
| 44 |
source_face = source_faces[0]
|
| 45 |
|
| 46 |
+
# Temporary output file (MP4 for better compatibility)
|
| 47 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_out:
|
| 48 |
+
output_path = tmp_out.name
|
| 49 |
|
|
|
|
| 50 |
cap = cv2.VideoCapture(video)
|
| 51 |
|
|
|
|
| 52 |
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 53 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 54 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 55 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 56 |
|
| 57 |
+
# MP4 writer
|
| 58 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 59 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 60 |
|
| 61 |
+
i = 0
|
| 62 |
+
while cap.isOpened():
|
| 63 |
ret, frame = cap.read()
|
| 64 |
if not ret:
|
| 65 |
+
break
|
| 66 |
|
|
|
|
| 67 |
target_faces = app.get(frame)
|
|
|
|
|
|
|
| 68 |
result_frame = frame.copy()
|
| 69 |
|
|
|
|
| 70 |
for target_face in target_faces:
|
| 71 |
+
# depending on insightface version, frame or result_frame may be needed
|
| 72 |
+
try:
|
| 73 |
+
result_frame = swapper.get(frame, target_face, source_face, paste_back=True)
|
| 74 |
+
except:
|
| 75 |
+
result_frame = swapper.get(result_frame, target_face, source_face, paste_back=True)
|
| 76 |
|
|
|
|
| 77 |
out.write(result_frame)
|
| 78 |
|
| 79 |
+
i += 1
|
| 80 |
+
if frame_count > 0:
|
| 81 |
+
progress.progress(min(1.0, i / frame_count))
|
| 82 |
|
|
|
|
| 83 |
cap.release()
|
| 84 |
out.release()
|
|
|
|
| 85 |
return output_path
|
| 86 |
|
| 87 |
+
# ------------------------------
|
| 88 |
# Streamlit UI
|
| 89 |
+
# ------------------------------
|
| 90 |
+
st.title("🎭 Face Swapper in Video")
|
| 91 |
+
st.write("Upload a **source image** and a **target video**, preview them, then swap faces.")
|
| 92 |
|
| 93 |
+
# Upload files
|
| 94 |
image_file = st.file_uploader("Upload Source Image", type=["jpg", "jpeg", "png"])
|
| 95 |
+
video_file = st.file_uploader("Upload Target Video", type=["mp4", "avi"])
|
| 96 |
+
|
| 97 |
+
# Preview uploaded files
|
| 98 |
+
if image_file:
|
| 99 |
+
st.subheader("📷 Source Image Preview")
|
| 100 |
+
st.image(image_file, caption="Source Image", use_column_width=True)
|
| 101 |
+
|
| 102 |
+
if video_file:
|
| 103 |
+
st.subheader("🎬 Target Video Preview")
|
| 104 |
+
st.video(video_file)
|
| 105 |
|
| 106 |
+
# Button to process
|
| 107 |
+
if st.button("🚀 Start Face Swap"):
|
| 108 |
+
if image_file and video_file:
|
| 109 |
+
source_image = cv2.imdecode(
|
| 110 |
+
np.frombuffer(image_file.read(), np.uint8), cv2.IMREAD_COLOR
|
| 111 |
+
)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
|
| 114 |
tmp_video.write(video_file.read())
|
| 115 |
tmp_video_path = tmp_video.name
|
| 116 |
|
| 117 |
+
with st.spinner("Processing video... Please wait ⏳"):
|
|
|
|
| 118 |
progress_bar = st.progress(0)
|
| 119 |
output_video_path = swap_faces_in_video(source_image, tmp_video_path, progress_bar)
|
| 120 |
|
| 121 |
if output_video_path:
|
| 122 |
+
st.success("✅ Face swapping completed!")
|
| 123 |
+
st.subheader("📺 Output Video Preview")
|
| 124 |
st.video(output_video_path)
|
| 125 |
|
|
|
|
| 126 |
with open(output_video_path, "rb") as f:
|
| 127 |
st.download_button(
|
| 128 |
+
label="⬇️ Download Processed Video",
|
| 129 |
data=f,
|
| 130 |
+
file_name="output_swapped_video.mp4",
|
| 131 |
+
mime="video/mp4"
|
| 132 |
)
|
| 133 |
|
| 134 |
+
# Cleanup
|
| 135 |
+
os.remove(tmp_video_path)
|
| 136 |
+
# Optionally delete output file after preview
|
| 137 |
+
# os.remove(output_video_path)
|
| 138 |
else:
|
| 139 |
+
st.error("⚠️ Please upload both a source image and a video.")
|