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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -1,38 +1,47 @@
1
  import streamlit as st
2
  import subprocess
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
 
 
1
  import streamlit as st
2
  import subprocess
3
  import shutil
4
+ from io import BytesIO
5
  import os
6
 
7
 
8
  # Create 'temp' directory if it doesn't exist
9
  os.makedirs('temp', exist_ok=True)
10
 
11
+
12
+
13
  def run_scripts(target, source, use_face_enhancer):
14
  if target is None or (not use_face_enhancer and source is None):
15
  return None
16
  with st.spinner("Processing..."):
 
 
 
 
 
 
17
  target_extension = os.path.splitext(target.name)[-1]
18
  output_path1 = "output1" + target_extension
19
  output_path2 = "output2" + target_extension
20
 
21
+ target_bytes = target.read() # Read target file as bytes
22
+ source_bytes = source.read() # Read source file as bytes
23
+
24
+ target_io = BytesIO(target_bytes) # Convert bytes to BytesIO
25
+ source_io = BytesIO(source_bytes) # Convert bytes to BytesIO
26
+
27
+ # Save files to temporary directory
28
+ with open(f'temp/target{target_extension}', 'wb') as f:
29
+ f.write(target_bytes)
30
+ with open(f'temp/source{target_extension}', 'wb') as f:
31
+ f.write(source_bytes)
32
+
33
  if not use_face_enhancer:
34
  # Run both scripts
35
+ cmd1 = ["python3", "run.py", "-s", f'temp/source{target_extension}', "-t", f'temp/target{target_extension}', "-o", output_path1, "--frame-processor", "face_swapper"]
36
  subprocess.run(cmd1)
37
 
38
  # Run the second script
39
+ cmd2 = ["python3", "run.py", "-t", f'temp/target{target_extension}' if use_face_enhancer else output_path1, "-o", output_path2, "--frame-processor", "face_enhancer"]
40
  subprocess.run(cmd2)
41
 
42
  if not use_face_enhancer:
43
+ os.remove(f'temp/source{target_extension}')
44
+ os.remove(f'temp/target{target_extension}')
45
 
46
  return output_path2
47