forked from haofanwang/inswapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
swapper.py
264 lines (213 loc) · 11.2 KB
/
swapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
This project is developed by Haofan Wang to support face swap in single frame. Multi-frame will be supported soon!
It is highly built on the top of insightface, sd-webui-roop and CodeFormer.
"""
import os
import cv2
import copy
import argparse
import insightface
import onnxruntime
import numpy as np
from PIL import Image
from typing import List, Union, Dict, Set, Tuple
def getFaceSwapModel(model_path: str):
model = insightface.model_zoo.get_model(model_path)
return model
def getFaceAnalyser(model_path: str, providers,
det_size=(320, 320)):
face_analyser = insightface.app.FaceAnalysis(name="buffalo_l", root="./checkpoints", providers=providers)
face_analyser.prepare(ctx_id=0, det_size=det_size)
return face_analyser
def get_one_face(face_analyser,
frame:np.ndarray):
face = face_analyser.get(frame)
try:
return min(face, key=lambda x: x.bbox[0])
except ValueError:
return None
def get_many_faces(face_analyser,
frame:np.ndarray):
"""
get faces from left to right by order
"""
try:
face = face_analyser.get(frame)
return sorted(face, key=lambda x: x.bbox[0])
except IndexError:
return None
def swap_face(face_swapper,
source_faces,
target_faces,
source_index,
target_index,
temp_frame):
"""
paste source_face on target image
"""
source_face = source_faces[source_index]
target_face = target_faces[target_index]
return face_swapper.get(temp_frame, target_face, source_face, paste_back=True)
def process(source_img: Union[Image.Image, List],
target_img: Image.Image,
source_indexes: str,
target_indexes: str,
model: str):
# load machine default available providers
providers = onnxruntime.get_available_providers()
# load face_analyser
face_analyser = getFaceAnalyser(model, providers)
# load face_swapper
model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), model)
face_swapper = getFaceSwapModel(model_path)
# read target image
target_img = cv2.cvtColor(np.array(target_img), cv2.COLOR_RGB2BGR)
# detect faces that will be replaced in the target image
target_faces = get_many_faces(face_analyser, target_img)
num_target_faces = len(target_faces)
num_source_images = len(source_img)
if target_faces is not None:
temp_frame = copy.deepcopy(target_img)
if isinstance(source_img, list) and num_source_images == num_target_faces:
print("Replacing faces in target image from the left to the right by order")
for i in range(num_target_faces):
source_faces = get_many_faces(face_analyser, cv2.cvtColor(np.array(source_img[i]), cv2.COLOR_RGB2BGR))
source_index = i
target_index = i
if source_faces is None:
raise Exception("No source faces found!")
temp_frame = swap_face(
face_swapper,
source_faces,
target_faces,
source_index,
target_index,
temp_frame
)
elif num_source_images == 1:
# detect source faces that will be replaced into the target image
source_faces = get_many_faces(face_analyser, cv2.cvtColor(np.array(source_img[0]), cv2.COLOR_RGB2BGR))
num_source_faces = len(source_faces)
print(f"Source faces: {num_source_faces}")
print(f"Target faces: {num_target_faces}")
if source_faces is None:
raise Exception("No source faces found!")
if target_indexes == "-1":
if num_source_faces == 1:
print("Replacing all faces in target image with the same face from the source image")
num_iterations = num_target_faces
elif num_source_faces < num_target_faces:
print("There are less faces in the source image than the target image, replacing as many as we can")
num_iterations = num_source_faces
elif num_target_faces < num_source_faces:
print("There are less faces in the target image than the source image, replacing as many as we can")
num_iterations = num_target_faces
else:
print("Replacing all faces in the target image with the faces from the source image")
num_iterations = num_target_faces
for i in range(num_iterations):
source_index = 0 if num_source_faces == 1 else i
target_index = i
temp_frame = swap_face(
face_swapper,
source_faces,
target_faces,
source_index,
target_index,
temp_frame
)
else:
print("Replacing specific face(s) in the target image with specific face(s) from the source image")
if source_indexes == "-1":
source_indexes = ','.join(map(lambda x: str(x), range(num_source_faces)))
if target_indexes == "-1":
target_indexes = ','.join(map(lambda x: str(x), range(num_target_faces)))
source_indexes = source_indexes.split(',')
target_indexes = target_indexes.split(',')
num_source_faces_to_swap = len(source_indexes)
num_target_faces_to_swap = len(target_indexes)
if num_source_faces_to_swap > num_source_faces:
raise Exception("Number of source indexes is greater than the number of faces in the source image")
if num_target_faces_to_swap > num_target_faces:
raise Exception("Number of target indexes is greater than the number of faces in the target image")
if num_source_faces_to_swap > num_target_faces_to_swap:
num_iterations = num_source_faces_to_swap
else:
num_iterations = num_target_faces_to_swap
if num_source_faces_to_swap == num_target_faces_to_swap:
for index in range(num_iterations):
source_index = int(source_indexes[index])
target_index = int(target_indexes[index])
if source_index > num_source_faces-1:
raise ValueError(f"Source index {source_index} is higher than the number of faces in the source image")
if target_index > num_target_faces-1:
raise ValueError(f"Target index {target_index} is higher than the number of faces in the target image")
temp_frame = swap_face(
face_swapper,
source_faces,
target_faces,
source_index,
target_index,
temp_frame
)
else:
raise Exception("Unsupported face configuration")
result = temp_frame
else:
print("No target faces found!")
result_image = Image.fromarray(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
return result_image
def parse_args():
parser = argparse.ArgumentParser(description="Face swap.")
parser.add_argument("--source_img", type=str, required=True, help="The path of source image, it can be multiple images, dir;dir2;dir3.")
parser.add_argument("--target_img", type=str, required=True, help="The path of target image.")
parser.add_argument("--output_img", type=str, required=False, default="result.png", help="The path and filename of output image.")
parser.add_argument("--source_indexes", type=str, required=False, default="-1", help="Comma separated list of the face indexes to use (left to right) in the source image, starting at 0 (-1 uses all faces in the source image")
parser.add_argument("--target_indexes", type=str, required=False, default="-1", help="Comma separated list of the face indexes to swap (left to right) in the target image, starting at 0 (-1 swaps all faces in the target image")
parser.add_argument("--face_restore", action="store_true", help="The flag for face restoration.")
parser.add_argument("--background_enhance", action="store_true", help="The flag for background enhancement.")
parser.add_argument("--face_upsample", action="store_true", help="The flag for face upsample.")
parser.add_argument("--upscale", type=int, default=1, help="The upscale value, up to 4.")
parser.add_argument("--codeformer_fidelity", type=float, default=0.5, help="The codeformer fidelity.")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
source_img_paths = args.source_img.split(';')
print("Source image paths:", source_img_paths)
target_img_path = args.target_img
source_img = [Image.open(img_path) for img_path in source_img_paths]
target_img = Image.open(target_img_path)
# download from https://huggingface.co/deepinsight/inswapper/tree/main
model = "./checkpoints/inswapper_128.onnx"
result_image = process(source_img, target_img, args.source_indexes, args.target_indexes, model)
if args.face_restore:
from restoration import *
# make sure the ckpts downloaded successfully
check_ckpts()
# https://huggingface.co/spaces/sczhou/CodeFormer
upsampler = set_realesrgan()
device = torch.device("mps" if torch.backends.mps.is_available() else "cuda" if torch.cuda.is_available() else "cpu")
codeformer_net = ARCH_REGISTRY.get("CodeFormer")(dim_embd=512,
codebook_size=1024,
n_head=8,
n_layers=9,
connect_list=["32", "64", "128", "256"],
).to(device)
ckpt_path = "CodeFormer/CodeFormer/weights/CodeFormer/codeformer.pth"
checkpoint = torch.load(ckpt_path)["params_ema"]
codeformer_net.load_state_dict(checkpoint)
codeformer_net.eval()
result_image = cv2.cvtColor(np.array(result_image), cv2.COLOR_RGB2BGR)
result_image = face_restoration(result_image,
args.background_enhance,
args.face_upsample,
args.upscale,
args.codeformer_fidelity,
upsampler,
codeformer_net,
device)
result_image = Image.fromarray(result_image)
# save result
result_image.save(args.output_img)
print(f'Result saved successfully: {args.output_img}')