Spaces:
Running
Running
Arrcttacsrks
commited on
Commit
•
b6da6b0
1
Parent(s):
8eb64f2
Upload face-swap-timestamp.py
Browse files- face-swap-timestamp.py +96 -0
face-swap-timestamp.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding:UTF-8 -*-
|
2 |
+
#!/usr/bin/env python
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
import roop.globals
|
6 |
+
from roop.core import (
|
7 |
+
start,
|
8 |
+
decode_execution_providers,
|
9 |
+
suggest_max_memory,
|
10 |
+
suggest_execution_threads,
|
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 |
+
from datetime import datetime
|
17 |
+
|
18 |
+
def swap_face(source_file, target_file, doFaceEnhancer):
|
19 |
+
# Save input images
|
20 |
+
source_path = "input.jpg"
|
21 |
+
target_path = "target.jpg"
|
22 |
+
source_image = Image.fromarray(source_file)
|
23 |
+
source_image.save(source_path)
|
24 |
+
target_image = Image.fromarray(target_file)
|
25 |
+
target_image.save(target_path)
|
26 |
+
|
27 |
+
print("source_path: ", source_path)
|
28 |
+
print("target_path: ", target_path)
|
29 |
+
|
30 |
+
# Set global paths
|
31 |
+
roop.globals.source_path = source_path
|
32 |
+
roop.globals.target_path = target_path
|
33 |
+
|
34 |
+
# Generate timestamp-based output filename
|
35 |
+
timestamp = datetime.now().strftime("%S%M%H%d%m%Y")
|
36 |
+
output_path = f"Image{timestamp}.jpg"
|
37 |
+
|
38 |
+
roop.globals.output_path = normalize_output_path(
|
39 |
+
roop.globals.source_path, roop.globals.target_path, output_path
|
40 |
+
)
|
41 |
+
|
42 |
+
# Configure face processing options
|
43 |
+
if doFaceEnhancer:
|
44 |
+
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
45 |
+
else:
|
46 |
+
roop.globals.frame_processors = ["face_swapper"]
|
47 |
+
|
48 |
+
# Set global parameters
|
49 |
+
roop.globals.headless = True
|
50 |
+
roop.globals.keep_fps = True
|
51 |
+
roop.globals.keep_audio = True
|
52 |
+
roop.globals.keep_frames = False
|
53 |
+
roop.globals.many_faces = False
|
54 |
+
roop.globals.video_encoder = "libx264"
|
55 |
+
roop.globals.video_quality = 18
|
56 |
+
roop.globals.max_memory = suggest_max_memory()
|
57 |
+
roop.globals.execution_providers = decode_execution_providers(["cuda"])
|
58 |
+
roop.globals.execution_threads = suggest_execution_threads()
|
59 |
+
|
60 |
+
print(
|
61 |
+
"start process",
|
62 |
+
roop.globals.source_path,
|
63 |
+
roop.globals.target_path,
|
64 |
+
roop.globals.output_path,
|
65 |
+
)
|
66 |
+
|
67 |
+
# Check frame processors
|
68 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
69 |
+
if not frame_processor.pre_check():
|
70 |
+
return
|
71 |
+
|
72 |
+
start()
|
73 |
+
return output_path
|
74 |
+
|
75 |
+
# Gradio interface setup
|
76 |
+
title = "Face - Интегратор"
|
77 |
+
description = r"""
|
78 |
+
"""
|
79 |
+
article = r"""
|
80 |
+
<br><br><br><br><br>
|
81 |
+
"""
|
82 |
+
|
83 |
+
app = gr.Interface(
|
84 |
+
fn=swap_face,
|
85 |
+
title=title,
|
86 |
+
description=description,
|
87 |
+
article=article,
|
88 |
+
inputs=[
|
89 |
+
gr.Image(),
|
90 |
+
gr.Image(),
|
91 |
+
gr.Checkbox(label="Применить алгоритм?", info="Улучшение качества изображения")
|
92 |
+
],
|
93 |
+
outputs="image"
|
94 |
+
)
|
95 |
+
|
96 |
+
app.launch()
|