Rooni commited on
Commit
3266b60
·
verified ·
1 Parent(s): 849f47f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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
+ # Создаем уникальные имена файлов
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
+ # Сохраняем изображения по указанным путям
35
+ source_image = Image.open(source_file.name)
36
+ source_image.save(source_path)
37
+ target_image = Image.open(target_file.name)
38
+ target_image.save(target_path)
39
+
40
+ print("source_path: ", source_path)
41
+ print("target_path: ", target_path)
42
+
43
+ # Проверяем, обнаружена ли лицо на изображении-источнике
44
+ source_face = get_one_face(cv2.imread(source_path))
45
+ if source_face is None:
46
+ raise gradio.exceptions.Error("No face in source path detected.")
47
+
48
+ # Проверяем, обнаружена ли лицо на изображении-цели
49
+ target_face = get_one_face(cv2.imread(target_path))
50
+ if target_face is None:
51
+ raise gradio.exceptions.Error("No face in target path detected.")
52
+
53
+ output_path = os.path.join(session_dir, output_filename)
54
+ normalized_output_path = normalize_output_path(source_path, target_path, output_path)
55
+
56
+ frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
57
+
58
+ for frame_processor in get_frame_processors_modules(frame_processors):
59
+ if not frame_processor.pre_check():
60
+ print(f"Pre-check failed for {frame_processor}")
61
+ raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")
62
+
63
+ roop.globals.source_path = source_path
64
+ roop.globals.target_path = target_path
65
+ roop.globals.output_path = normalized_output_path
66
+ roop.globals.frame_processors = frame_processors
67
+ roop.globals.headless = True
68
+ roop.globals.keep_fps = True
69
+ roop.globals.keep_audio = True
70
+ roop.globals.keep_frames = False
71
+ roop.globals.many_faces = False
72
+ roop.globals.video_encoder = "libx264"
73
+ roop.globals.video_quality = 18
74
+ roop.globals.execution_providers = decode_execution_providers(['cpu'])
75
+ roop.globals.reference_face_position = 0
76
+ roop.globals.similar_face_distance = 0.6
77
+ roop.globals.max_memory = 60
78
+ roop.globals.execution_threads = 8
79
+
80
+ start()
81
+ return normalized_output_path
82
+
83
+ app = gr.Interface(
84
+ fn=swap_face,
85
+ inputs=[
86
+ gr.Image(type="filepath"), # Изменено на "filepath"
87
+ gr.Image(type="filepath"), # Изменено на "filepath"
88
+ gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")
89
+ ],
90
+ outputs="image"
91
+ )
92
+ app.launch()