Spaces:
Running
Running
prithivMLmods
commited on
Commit
•
9ea7c68
1
Parent(s):
c0bdbd3
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,88 @@
|
|
1 |
-
# -*- coding:UTF-8 -*-
|
2 |
-
# !/usr/bin/env python
|
3 |
-
import spaces
|
4 |
-
import numpy as np
|
5 |
-
import gradio as gr
|
6 |
-
import gradio.exceptions
|
7 |
-
import roop.globals
|
8 |
-
from roop.core import (
|
9 |
-
start,
|
10 |
-
decode_execution_providers,
|
11 |
-
)
|
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 |
-
session_id = str(uuid.uuid4()) # Tạo một UUID duy nhất cho mỗi phiên làm việc
|
24 |
-
session_dir = f"temp/{session_id}"
|
25 |
-
os.makedirs(session_dir, exist_ok=True)
|
26 |
-
|
27 |
-
source_path = os.path.join(session_dir, "input.jpg")
|
28 |
-
target_path = os.path.join(session_dir, "target.jpg")
|
29 |
-
|
30 |
-
source_image = Image.fromarray(source_file)
|
31 |
-
source_image.save(source_path)
|
32 |
-
target_image = Image.fromarray(target_file)
|
33 |
-
target_image.save(target_path)
|
34 |
-
|
35 |
-
print("source_path: ", source_path)
|
36 |
-
print("target_path: ", target_path)
|
37 |
-
|
38 |
-
# Check if a face is detected in the source image
|
39 |
-
source_face = get_one_face(cv2.imread(source_path))
|
40 |
-
if source_face is None:
|
41 |
-
raise gradio.exceptions.Error("No face in source path detected.")
|
42 |
-
|
43 |
-
# Check if a face is detected in the target image
|
44 |
-
target_face = get_one_face(cv2.imread(target_path))
|
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, "output.jpg")
|
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}")
|
57 |
-
raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")
|
58 |
-
|
59 |
-
roop.globals.source_path = source_path
|
60 |
-
roop.globals.target_path = target_path
|
61 |
-
roop.globals.output_path = normalized_output_path
|
62 |
-
roop.globals.frame_processors = frame_processors
|
63 |
-
roop.globals.headless = True
|
64 |
-
roop.globals.keep_fps = True
|
65 |
-
roop.globals.keep_audio = True
|
66 |
-
roop.globals.keep_frames = False
|
67 |
-
roop.globals.many_faces = False
|
68 |
-
roop.globals.video_encoder = "libx264"
|
69 |
-
roop.globals.video_quality = 18
|
70 |
-
roop.globals.execution_providers = ["CUDAExecutionProvider"]
|
71 |
-
roop.globals.reference_face_position = 0
|
72 |
-
roop.globals.similar_face_distance = 0.6
|
73 |
-
roop.globals.max_memory = 60
|
74 |
-
roop.globals.execution_threads = 50
|
75 |
-
|
76 |
-
start()
|
77 |
-
return normalized_output_path
|
78 |
-
|
79 |
-
app = gr.Interface(
|
80 |
-
fn=swap_face,
|
81 |
-
inputs=[
|
82 |
-
gr.Image(),
|
83 |
-
gr.Image(),
|
84 |
-
gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")
|
85 |
-
],
|
86 |
-
outputs="image"
|
87 |
-
)
|
88 |
app.launch()
|
|
|
1 |
+
# -*- coding:UTF-8 -*-
|
2 |
+
# !/usr/bin/env python
|
3 |
+
import spaces
|
4 |
+
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
import gradio.exceptions
|
7 |
+
import roop.globals
|
8 |
+
from roop.core import (
|
9 |
+
start,
|
10 |
+
decode_execution_providers,
|
11 |
+
)
|
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 |
+
session_id = str(uuid.uuid4()) # Tạo một UUID duy nhất cho mỗi phiên làm việc
|
24 |
+
session_dir = f"temp/{session_id}"
|
25 |
+
os.makedirs(session_dir, exist_ok=True)
|
26 |
+
|
27 |
+
source_path = os.path.join(session_dir, "input.jpg")
|
28 |
+
target_path = os.path.join(session_dir, "target.jpg")
|
29 |
+
|
30 |
+
source_image = Image.fromarray(source_file)
|
31 |
+
source_image.save(source_path)
|
32 |
+
target_image = Image.fromarray(target_file)
|
33 |
+
target_image.save(target_path)
|
34 |
+
|
35 |
+
print("source_path: ", source_path)
|
36 |
+
print("target_path: ", target_path)
|
37 |
+
|
38 |
+
# Check if a face is detected in the source image
|
39 |
+
source_face = get_one_face(cv2.imread(source_path))
|
40 |
+
if source_face is None:
|
41 |
+
raise gradio.exceptions.Error("No face in source path detected.")
|
42 |
+
|
43 |
+
# Check if a face is detected in the target image
|
44 |
+
target_face = get_one_face(cv2.imread(target_path))
|
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, "output.jpg")
|
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}")
|
57 |
+
raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")
|
58 |
+
|
59 |
+
roop.globals.source_path = source_path
|
60 |
+
roop.globals.target_path = target_path
|
61 |
+
roop.globals.output_path = normalized_output_path
|
62 |
+
roop.globals.frame_processors = frame_processors
|
63 |
+
roop.globals.headless = True
|
64 |
+
roop.globals.keep_fps = True
|
65 |
+
roop.globals.keep_audio = True
|
66 |
+
roop.globals.keep_frames = False
|
67 |
+
roop.globals.many_faces = False
|
68 |
+
roop.globals.video_encoder = "libx264"
|
69 |
+
roop.globals.video_quality = 18
|
70 |
+
roop.globals.execution_providers = ["CUDAExecutionProvider"]
|
71 |
+
roop.globals.reference_face_position = 0
|
72 |
+
roop.globals.similar_face_distance = 0.6
|
73 |
+
roop.globals.max_memory = 60
|
74 |
+
roop.globals.execution_threads = 50
|
75 |
+
|
76 |
+
start()
|
77 |
+
return normalized_output_path
|
78 |
+
|
79 |
+
app = gr.Interface(
|
80 |
+
fn=swap_face,
|
81 |
+
inputs=[
|
82 |
+
gr.Image(),
|
83 |
+
gr.Image(),
|
84 |
+
gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")
|
85 |
+
],
|
86 |
+
outputs="image"
|
87 |
+
)
|
88 |
app.launch()
|