Ashrafb commited on
Commit
f5e12da
·
1 Parent(s): d7b473c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -3,26 +3,36 @@ import subprocess
3
  import shutil
4
  import os
5
 
 
 
 
 
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
 
@@ -35,6 +45,6 @@ target_file = st.file_uploader("Upload target image/video")
35
  use_face_enhancer = st.checkbox("Use only Face Enhancer", False)
36
 
37
  if source_file and target_file and 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
 
3
  import shutil
4
  import os
5
 
6
+
7
+ # Create 'temp' directory if it doesn't exist
8
+ os.makedirs('temp', exist_ok=True)
9
+
10
  def run_scripts(target, source, use_face_enhancer):
11
  if target is None or (not use_face_enhancer and source is None):
12
  return None
13
  with st.spinner("Processing..."):
14
+ # Save uploaded files to temporary directory
15
+ with open(f'temp/{target.name}', 'wb') as f:
16
+ f.write(target.getvalue())
17
+ with open(f'temp/{source.name}', 'wb') as f:
18
+ f.write(source.getvalue())
19
+
20
+ target_extension = os.path.splitext(target.name)[-1]
21
  output_path1 = "output1" + target_extension
22
  output_path2 = "output2" + target_extension
23
 
24
  if not use_face_enhancer:
25
  # Run both scripts
26
+ cmd1 = ["python3", "run.py", "-s", f'temp/{source.name}', "-t", f'temp/{target.name}', "-o", output_path1, "--frame-processor", "face_swapper"]
27
  subprocess.run(cmd1)
28
 
29
  # Run the second script
30
+ cmd2 = ["python3", "run.py", "-t", f'temp/{target.name}' if use_face_enhancer else output_path1, "-o", output_path2, "--frame-processor", "face_enhancer"]
31
  subprocess.run(cmd2)
32
 
33
  if not use_face_enhancer:
34
+ os.remove(f'temp/{source.name}')
35
+ os.remove(f'temp/{target.name}')
36
 
37
  return output_path2
38
 
 
45
  use_face_enhancer = st.checkbox("Use only Face Enhancer", False)
46
 
47
  if source_file and target_file and st.button("Swap Faces"):
48
+ result = run_scripts(target_file, source_file, use_face_enhancer)
49
  if result:
50
  st.image(result) # Display the result as an image