Fa2 / app.py
Ashrafb's picture
Update app.py
a2bfe0a
raw
history blame
No virus
2.21 kB
import streamlit as st
import subprocess
import shutil
import os
from PIL import Image
import numpy as np
def run_scripts(target, source, use_face_enhancer):
if target is None or (not use_face_enhancer and source is None):
return None
target_extension = os.path.splitext(target.name)[-1]
output_path1 = "output1" + target_extension
output_path2 = "output2" + target_extension
if not use_face_enhancer:
# Run both scripts
cmd1 = ["python3", "run.py", "-s", source.name, "-t", target.name, "-o", output_path1, "--frame-processor", "face_swapper"]
subprocess.run(cmd1)
# Run the second script
cmd2 = ["python3", "run.py", "-t", target.name if use_face_enhancer else output_path1, "-o", output_path2, "--frame-processor", "face_enhancer"]
subprocess.run(cmd2)
if not use_face_enhancer:
os.remove(source.name)
os.remove(target.name)
return output_path2
def main():
st.markdown('<p style="color:#191970;text-align:center;font-size:30px;">Aiconvert.online</p>', unsafe_allow_html=True)
st.title("AIconvert Face swap")
st.markdown('<style>h1{color: Crimson; text-align: center;}</style>', unsafe_allow_html=True)
source_file = st.file_uploader("Upload Source Image", type=["jpg", "png", "jpeg"])
target_file = st.file_uploader("Upload Target Image", type=["jpg", "png", "jpeg"])
doFaceEnhancer = st.checkbox("Enable Face Enhancer")
if source_file is not None and target_file is not None:
source_image = Image.open(source_file)
target_image = Image.open(target_file)
st.image(source_image, caption="Source Image", use_column_width=True)
st.image(target_image, caption="Target Image", use_column_width=True)
if st.button("Swap Faces"):
with st.spinner("Swapping Faces..."):
output_path = run_scripts(
target_file,
source_file if not doFaceEnhancer else None,
doFaceEnhancer,
)
output_image = Image.open(output_path)
st.image(output_image, caption="Result", use_column_width=True)
if __name__ == "__main__":
main()