Ashrafb commited on
Commit
f0ccc44
·
1 Parent(s): 4170421

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -6,39 +6,37 @@ import os
6
  def run_scripts(target, source, use_face_enhancer):
7
  if target is None or (not use_face_enhancer and source is None):
8
  return None
9
- target_extension = os.path.splitext(target.name)[-1]
10
- output_path1 = "output1" + target_extension
11
- output_path2 = "output2" + target_extension
 
12
 
13
- if not use_face_enhancer:
14
- # Run both scripts
15
- cmd1 = ["python3", "run.py", "-s", source.name, "-t", target.name, "-o", output_path1, "--frame-processor", "face_swapper"]
16
- subprocess.run(cmd1)
17
 
18
- # Run the second script
19
- cmd2 = ["python3", "run.py", "-t", target.name if use_face_enhancer else output_path1, "-o", output_path2, "--frame-processor", "face_enhancer"]
20
- subprocess.run(cmd2)
21
 
22
- if not use_face_enhancer:
23
- os.remove(source.name)
24
- os.remove(target.name)
25
 
26
- return output_path2
27
 
28
  # Streamlit UI
29
  st.title("Face Swapper")
30
- st.sidebar.header("Options")
31
- source_file = st.sidebar.file_uploader("Upload source image")
32
- target_file = st.sidebar.file_uploader("Upload target image/video")
33
- use_face_enhancer = st.sidebar.checkbox("Use only Face Enhancer", False)
34
 
35
- if st.sidebar.button("Swap Faces"):
36
- result = run_scripts(target_file, source_file, use_face_enhancer)
 
 
 
 
37
  if result:
38
- st.video(result) # Display the result as a video
39
 
40
- # Cleanup uploaded files after processing
41
- if source_file:
42
- os.remove(source_file.name)
43
- if target_file:
44
- os.remove(target_file.name)
 
6
  def run_scripts(target, source, use_face_enhancer):
7
  if target is None or (not use_face_enhancer and source is None):
8
  return None
9
+ with st.spinner("Processing..."):
10
+ target_extension = os.path.splitext(target)[-1]
11
+ output_path1 = "output1" + target_extension
12
+ output_path2 = "output2" + target_extension
13
 
14
+ if not use_face_enhancer:
15
+ # Run both scripts
16
+ cmd1 = ["python3", "run.py", "-s", source, "-t", target, "-o", output_path1, "--frame-processor", "face_swapper"]
17
+ subprocess.run(cmd1)
18
 
19
+ # Run the second script
20
+ cmd2 = ["python3", "run.py", "-t", target if use_face_enhancer else output_path1, "-o", output_path2, "--frame-processor", "face_enhancer"]
21
+ subprocess.run(cmd2)
22
 
23
+ if not use_face_enhancer:
24
+ os.remove(source)
25
+ os.remove(target)
26
 
27
+ return output_path2
28
 
29
  # Streamlit UI
30
  st.title("Face Swapper")
31
+ st.write("Upload a target image/video and a source image to swap faces.")
 
 
 
32
 
33
+ source_file = st.file_uploader("Upload source image")
34
+ target_file = st.file_uploader("Upload target image/video")
35
+ use_face_enhancer = st.checkbox("Use only Face Enhancer", False)
36
+
37
+ if st.button("Swap Faces"):
38
+ result = run_scripts(target_file.name, source_file.name, use_face_enhancer)
39
  if result:
40
+ st.image(result) # Display the result as an image
41
 
42
+ # No need to remove uploaded files, as they are now passed as strings.