Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -12,20 +12,24 @@ from roop.core import (
|
|
12 |
from roop.processors.frame.core import get_frame_processors_modules
|
13 |
from roop.utilities import normalize_output_path
|
14 |
import os
|
|
|
15 |
from PIL import Image
|
16 |
-
import uuid
|
17 |
import onnxruntime as ort
|
18 |
import cv2
|
19 |
from roop.face_analyser import get_one_face
|
20 |
|
21 |
@spaces.GPU
|
22 |
def swap_face(source_file, target_file, doFaceEnhancer):
|
23 |
-
|
24 |
-
session_dir = f"temp/{session_id}"
|
25 |
os.makedirs(session_dir, exist_ok=True)
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
source_image = Image.fromarray(source_file)
|
31 |
source_image.save(source_path)
|
@@ -45,12 +49,11 @@ def swap_face(source_file, target_file, doFaceEnhancer):
|
|
45 |
if target_face is None:
|
46 |
raise gradio.exceptions.Error("No face in target path detected.")
|
47 |
|
48 |
-
output_path = os.path.join(session_dir,
|
49 |
normalized_output_path = normalize_output_path(source_path, target_path, output_path)
|
50 |
|
51 |
frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
|
52 |
|
53 |
-
|
54 |
for frame_processor in get_frame_processors_modules(frame_processors):
|
55 |
if not frame_processor.pre_check():
|
56 |
print(f"Pre-check failed for {frame_processor}")
|
|
|
12 |
from roop.processors.frame.core import get_frame_processors_modules
|
13 |
from roop.utilities import normalize_output_path
|
14 |
import os
|
15 |
+
import random
|
16 |
from PIL import Image
|
|
|
17 |
import onnxruntime as ort
|
18 |
import cv2
|
19 |
from roop.face_analyser import get_one_face
|
20 |
|
21 |
@spaces.GPU
|
22 |
def swap_face(source_file, target_file, doFaceEnhancer):
|
23 |
+
session_dir = "temp" # Sử dụng thư mục cố định
|
|
|
24 |
os.makedirs(session_dir, exist_ok=True)
|
25 |
|
26 |
+
# Tạo tên file ngẫu nhiên
|
27 |
+
source_filename = f"source_{random.randint(1000, 9999)}.jpg"
|
28 |
+
target_filename = f"target_{random.randint(1000, 9999)}.jpg"
|
29 |
+
output_filename = f"output_{random.randint(1000, 9999)}.jpg"
|
30 |
+
|
31 |
+
source_path = os.path.join(session_dir, source_filename)
|
32 |
+
target_path = os.path.join(session_dir, target_filename)
|
33 |
|
34 |
source_image = Image.fromarray(source_file)
|
35 |
source_image.save(source_path)
|
|
|
49 |
if target_face is None:
|
50 |
raise gradio.exceptions.Error("No face in target path detected.")
|
51 |
|
52 |
+
output_path = os.path.join(session_dir, output_filename)
|
53 |
normalized_output_path = normalize_output_path(source_path, target_path, output_path)
|
54 |
|
55 |
frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
|
56 |
|
|
|
57 |
for frame_processor in get_frame_processors_modules(frame_processors):
|
58 |
if not frame_processor.pre_check():
|
59 |
print(f"Pre-check failed for {frame_processor}")
|