| import numpy as np |
| import cv2 |
| import streamlit as st |
| import insightface |
| from insightface.app import FaceAnalysis |
| from PIL import Image |
| from nudenet import NudeDetector |
|
|
| |
| app = FaceAnalysis(name='buffalo_l') |
| app.prepare(ctx_id=0, det_size=(640, 640)) |
|
|
| |
| swapper = insightface.model_zoo.get_model('insightface/inswapper_128.onnx', download=True) |
|
|
| |
| detector = NudeDetector() |
|
|
| |
| def check_for_adult_content(image): |
| |
| img = np.array(image) |
|
|
| |
| results = detector.detect(img) |
|
|
| |
| explicit_labels = [ |
| 'EXPOSED_ANUS', |
| 'EXPOSED_BREAST_F', |
| 'EXPOSED_GENITALIA_F', |
| 'EXPOSED_GENITALIA_M' |
| ] |
|
|
| |
| for item in results: |
| if 'label' in item and item['label'] in explicit_labels: |
| return True |
|
|
| return False |
|
|
| |
| def swap_faces(destination_image, source_image): |
| |
| img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR) |
| test = cv2.cvtColor(np.array(source_image), cv2.COLOR_RGB2BGR) |
|
|
| |
| faces = app.get(img) |
| test_faces = app.get(test) |
|
|
| if not faces or not test_faces: |
| return "No faces detected in one or both images." |
|
|
| test_face = test_faces[0] |
|
|
| |
| res = img.copy() |
| try: |
| for face in faces: |
| res = swapper.get(res, face, test_face, paste_back=True) |
| except MemoryError: |
| return "Memory error: Face swapping operation failed due to memory overload." |
|
|
| |
| kernel = np.array([[0, -1, 0], |
| [-1, 5, -1], |
| [0, -1, 0]]) |
| sharpened_res = cv2.filter2D(res, -1, kernel) |
|
|
| |
| smoothed_res = cv2.GaussianBlur(res, (5, 5), 0) |
|
|
| |
| sharpened_res_rgb = cv2.cvtColor(sharpened_res, cv2.COLOR_BGR2RGB) |
|
|
| return sharpened_res_rgb |
|
|
| |
| def main(): |
| st.set_page_config(page_title="SnapSwap", page_icon=":camera:") |
|
|
| |
| st.markdown(""" |
| <style> |
| .header { |
| font-size: 2em; |
| font-weight: bold; |
| color: #4e73df; |
| } |
| .subtitle { |
| font-size: 1.2em; |
| color: #4e73df; |
| } |
| .content { |
| font-size: 1em; |
| } |
| </style> |
| """, unsafe_allow_html=True) |
|
|
| |
| st.header("SnapSwap :camera:") |
| st.write("Welcome to the face swapping app! Upload two images and watch the magic happen!") |
|
|
| |
| with st.sidebar: |
| st.subheader("Upload Images for Face Swapping and hit Swap Faces button") |
| source_image = st.file_uploader("Upload Source Image", type=["jpg", "png", "jpeg"]) |
| destination_image = st.file_uploader("Upload Destination Image", type=["jpg", "png", "jpeg"]) |
|
|
| if destination_image and source_image: |
| |
| destination_img = Image.open(destination_image) |
| if check_for_adult_content(destination_img): |
| st.error("The destination image does not fall within ethical guidelines.") |
| else: |
| st.write("Both images uploaded! Click below to swap faces.") |
| if st.button("Swap Faces"): |
| with st.spinner("Processing..."): |
| |
| source_img = Image.open(source_image) |
|
|
| |
| result = swap_faces(destination_img, source_img) |
|
|
| if isinstance(result, str): |
| st.error(result) |
| else: |
| st.session_state.result = result |
| st.success("Face swapping complete!") |
|
|
| |
| st.subheader("How to Use the App:") |
| st.markdown(""" |
| 1. **Upload the destination and source images**: Select the images you want to use for face swapping. |
| 2. **Click on the \"Swap Faces\" button**: The app will process the images and swap the faces. |
| 3. **View the output**: The swapped face image will appear after processing. |
| """) |
|
|
| |
| if 'result' in st.session_state: |
| st.image(st.session_state.result, caption="Swapped Face", use_container_width=True) |
|
|
| |
| st.markdown("Developed with ❤ by Aklavya") |
|
|
| |
| if __name__ == '__main__': |
| main() |