Upload 2 files
Browse files- app.py +84 -0
- inswapper_128.onnx +3 -0
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import insightface
|
6 |
+
from insightface.app import FaceAnalysis
|
7 |
+
from insightface.model_zoo import get_model
|
8 |
+
import tempfile
|
9 |
+
|
10 |
+
# Initialize face analysis app
|
11 |
+
app = FaceAnalysis(name='buffalo_l')
|
12 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
13 |
+
|
14 |
+
# Load the face swapper model
|
15 |
+
swapper = get_model('inswapper_128.onnx', download=False, download_zip=False)
|
16 |
+
|
17 |
+
def swap_faces(img1, img2):
|
18 |
+
face1 = app.get(img1)[0]
|
19 |
+
face2 = app.get(img2)[0]
|
20 |
+
|
21 |
+
img1_swapped = img1.copy()
|
22 |
+
img2_swapped = img2.copy()
|
23 |
+
|
24 |
+
img1_swapped = swapper.get(img1_swapped, face1, face2, paste_back=True)
|
25 |
+
img2_swapped = swapper.get(img2_swapped, face2, face1, paste_back=True)
|
26 |
+
|
27 |
+
return img1_swapped, img2_swapped
|
28 |
+
|
29 |
+
def main():
|
30 |
+
st.title("Face Swap App")
|
31 |
+
|
32 |
+
# Upload source image
|
33 |
+
source_image = st.file_uploader("Upload Source Image", type=["jpg", "jpeg", "png"])
|
34 |
+
# Upload target image
|
35 |
+
target_image = st.file_uploader("Upload Target Image", type=["jpg", "jpeg", "png"])
|
36 |
+
|
37 |
+
if source_image is not None and target_image is not None:
|
38 |
+
# Read images
|
39 |
+
img1 = cv2.imdecode(np.frombuffer(source_image.read(), np.uint8), 1)
|
40 |
+
img2 = cv2.imdecode(np.frombuffer(target_image.read(), np.uint8), 1)
|
41 |
+
|
42 |
+
# Swap faces
|
43 |
+
img1_swapped, img2_swapped = swap_faces(img1, img2)
|
44 |
+
|
45 |
+
# Display original and swapped images
|
46 |
+
col1, col2 = st.columns(2)
|
47 |
+
|
48 |
+
with col1:
|
49 |
+
st.subheader("Source Image")
|
50 |
+
st.image(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB), channels="RGB")
|
51 |
+
|
52 |
+
with col2:
|
53 |
+
st.subheader("Target Image")
|
54 |
+
st.image(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB), channels="RGB")
|
55 |
+
|
56 |
+
st.subheader("Swapped Images")
|
57 |
+
col3, col4 = st.columns(2)
|
58 |
+
|
59 |
+
with col3:
|
60 |
+
st.image(cv2.cvtColor(img1_swapped, cv2.COLOR_BGR2RGB), channels="RGB", caption="Source with Target Face")
|
61 |
+
# Save button for Source with Target Face
|
62 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
63 |
+
cv2.imwrite(tmp_file.name, img1_swapped)
|
64 |
+
st.download_button(
|
65 |
+
label="Download Source with Target Face",
|
66 |
+
data=open(tmp_file.name, "rb").read(),
|
67 |
+
file_name="source_with_target_face.jpg",
|
68 |
+
mime="image/jpeg"
|
69 |
+
)
|
70 |
+
|
71 |
+
with col4:
|
72 |
+
st.image(cv2.cvtColor(img2_swapped, cv2.COLOR_BGR2RGB), channels="RGB", caption="Target with Source Face")
|
73 |
+
# Save button for Target with Source Face
|
74 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
75 |
+
cv2.imwrite(tmp_file.name, img2_swapped)
|
76 |
+
st.download_button(
|
77 |
+
label="Download Target with Source Face",
|
78 |
+
data=open(tmp_file.name, "rb").read(),
|
79 |
+
file_name="target_with_source_face.jpg",
|
80 |
+
mime="image/jpeg"
|
81 |
+
)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
main()
|
inswapper_128.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e4a3f08c753cb72d04e10aa0f7dbe3deebbf39567d4ead6dce08e98aa49e16af
|
3 |
+
size 554253681
|