Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ app.prepare(ctx_id=0, det_size=(640, 640)) # Ensure the model runs on the CPU i
|
|
| 12 |
# Load the face swapping model (downloading on-demand)
|
| 13 |
swapper = insightface.model_zoo.get_model('insightface/inswapper_128.onnx', download=True)
|
| 14 |
|
| 15 |
-
# Face swapping function
|
| 16 |
def swap_faces(destination_image, source_image):
|
| 17 |
# Load the destination and source images from Streamlit inputs
|
| 18 |
img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR)
|
|
@@ -41,10 +41,13 @@ def swap_faces(destination_image, source_image):
|
|
| 41 |
[0, -1, 0]])
|
| 42 |
sharpened_res = cv2.filter2D(res, -1, kernel)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
# Convert the result to RGB for display in Streamlit
|
| 45 |
-
|
| 46 |
|
| 47 |
-
return
|
| 48 |
|
| 49 |
# Streamlit app layout
|
| 50 |
def main():
|
|
@@ -72,15 +75,8 @@ def main():
|
|
| 72 |
st.header("Face Swapping App :camera:")
|
| 73 |
st.write("Welcome to the face swapping app! Upload two images and watch the magic happen!")
|
| 74 |
|
| 75 |
-
# Sidebar for
|
| 76 |
with st.sidebar:
|
| 77 |
-
st.subheader("How to Use the App:")
|
| 78 |
-
st.markdown("""
|
| 79 |
-
1. **Upload the destination and source images**: Select the images you want to use for face swapping.
|
| 80 |
-
2. **Click on the "Swap Faces" button**: The app will process the images and swap the faces.
|
| 81 |
-
3. **View the output**: The swapped face image will appear below after processing.
|
| 82 |
-
""")
|
| 83 |
-
|
| 84 |
st.subheader("Upload Images for Face Swapping")
|
| 85 |
destination_image = st.file_uploader("Upload Destination Image", type=["jpg", "png", "jpeg"])
|
| 86 |
source_image = st.file_uploader("Upload Source Image", type=["jpg", "png", "jpeg"])
|
|
@@ -102,6 +98,14 @@ def main():
|
|
| 102 |
st.session_state.result = result # Save the result in session state
|
| 103 |
st.success("Face swapping complete!")
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# Main content area for displaying the swapped face image
|
| 106 |
if 'result' in st.session_state:
|
| 107 |
st.image(st.session_state.result, caption="Swapped Face", use_container_width=True)
|
|
|
|
| 12 |
# Load the face swapping model (downloading on-demand)
|
| 13 |
swapper = insightface.model_zoo.get_model('insightface/inswapper_128.onnx', download=True)
|
| 14 |
|
| 15 |
+
# Face swapping function with added smoothing
|
| 16 |
def swap_faces(destination_image, source_image):
|
| 17 |
# Load the destination and source images from Streamlit inputs
|
| 18 |
img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR)
|
|
|
|
| 41 |
[0, -1, 0]])
|
| 42 |
sharpened_res = cv2.filter2D(res, -1, kernel)
|
| 43 |
|
| 44 |
+
# Add smoothing (blurring) for a more natural look
|
| 45 |
+
smoothed_res = cv2.GaussianBlur(sharpened_res, (5, 5), 0)
|
| 46 |
+
|
| 47 |
# Convert the result to RGB for display in Streamlit
|
| 48 |
+
smoothed_res_rgb = cv2.cvtColor(smoothed_res, cv2.COLOR_BGR2RGB)
|
| 49 |
|
| 50 |
+
return smoothed_res_rgb
|
| 51 |
|
| 52 |
# Streamlit app layout
|
| 53 |
def main():
|
|
|
|
| 75 |
st.header("Face Swapping App :camera:")
|
| 76 |
st.write("Welcome to the face swapping app! Upload two images and watch the magic happen!")
|
| 77 |
|
| 78 |
+
# Sidebar for file uploads and instructions
|
| 79 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
st.subheader("Upload Images for Face Swapping")
|
| 81 |
destination_image = st.file_uploader("Upload Destination Image", type=["jpg", "png", "jpeg"])
|
| 82 |
source_image = st.file_uploader("Upload Source Image", type=["jpg", "png", "jpeg"])
|
|
|
|
| 98 |
st.session_state.result = result # Save the result in session state
|
| 99 |
st.success("Face swapping complete!")
|
| 100 |
|
| 101 |
+
# Instructions on how to use the app (below upload buttons and swap button)
|
| 102 |
+
st.subheader("How to Use the App:")
|
| 103 |
+
st.markdown("""
|
| 104 |
+
1. **Upload the destination and source images**: Select the images you want to use for face swapping.
|
| 105 |
+
2. **Click on the "Swap Faces" button**: The app will process the images and swap the faces.
|
| 106 |
+
3. **View the output**: The swapped face image will appear below after processing.
|
| 107 |
+
""")
|
| 108 |
+
|
| 109 |
# Main content area for displaying the swapped face image
|
| 110 |
if 'result' in st.session_state:
|
| 111 |
st.image(st.session_state.result, caption="Swapped Face", use_container_width=True)
|