Ii
commited on
Update refacer.py
Browse files- refacer.py +101 -49
refacer.py
CHANGED
@@ -20,15 +20,12 @@ from insightface.app.common import Face
|
|
20 |
from insightface.utils.storage import ensure_available
|
21 |
import re
|
22 |
import subprocess
|
23 |
-
import tempfile
|
24 |
-
|
25 |
|
26 |
class RefacerMode(Enum):
|
27 |
-
|
28 |
-
|
29 |
|
30 |
class Refacer:
|
31 |
-
def __init__(self,
|
32 |
self.first_face = False
|
33 |
self.force_cpu = force_cpu
|
34 |
self.colab_performance = colab_performance
|
@@ -38,7 +35,7 @@ class Refacer:
|
|
38 |
self.__init_apps()
|
39 |
|
40 |
def __check_providers(self):
|
41 |
-
if self.force_cpu:
|
42 |
self.providers = ['CPUExecutionProvider']
|
43 |
else:
|
44 |
self.providers = rt.get_available_providers()
|
@@ -49,18 +46,18 @@ class Refacer:
|
|
49 |
|
50 |
if len(self.providers) == 1 and 'CPUExecutionProvider' in self.providers:
|
51 |
self.mode = RefacerMode.CPU
|
52 |
-
self.use_num_cpus = mp.cpu_count()
|
53 |
-
self.sess_options.intra_op_num_threads = int(self.use_num_cpus
|
54 |
print(f"CPU mode with providers {self.providers}")
|
55 |
elif self.colab_performance:
|
56 |
self.mode = RefacerMode.TENSORRT
|
57 |
-
self.use_num_cpus = mp.cpu_count()
|
58 |
-
self.sess_options.intra_op_num_threads = int(self.use_num_cpus
|
59 |
print(f"TENSORRT mode with providers {self.providers}")
|
60 |
elif 'CoreMLExecutionProvider' in self.providers:
|
61 |
self.mode = RefacerMode.COREML
|
62 |
-
self.use_num_cpus = mp.cpu_count()
|
63 |
-
self.sess_options.intra_op_num_threads = int(self.use_num_cpus
|
64 |
print(f"CoreML mode with providers {self.providers}")
|
65 |
elif 'CUDAExecutionProvider' in self.providers:
|
66 |
self.mode = RefacerMode.CUDA
|
@@ -69,31 +66,42 @@ class Refacer:
|
|
69 |
if 'TensorrtExecutionProvider' in self.providers:
|
70 |
self.providers.remove('TensorrtExecutionProvider')
|
71 |
print(f"CUDA mode with providers {self.providers}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
def __init_apps(self):
|
74 |
assets_dir = ensure_available('models', 'buffalo_l', root='~/.insightface')
|
75 |
|
76 |
model_path = os.path.join(assets_dir, 'det_10g.onnx')
|
77 |
sess_face = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
78 |
-
self.face_detector = SCRFD(model_path,
|
79 |
-
self.face_detector.prepare(0,
|
80 |
|
81 |
-
model_path = os.path.join(assets_dir, 'w600k_r50.onnx')
|
82 |
sess_rec = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
83 |
-
self.rec_app = ArcFaceONNX(model_path,
|
84 |
self.rec_app.prepare(0)
|
85 |
|
86 |
model_path = 'inswapper_128.onnx'
|
87 |
sess_swap = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
88 |
-
self.face_swapper = INSwapper(model_path,
|
89 |
|
90 |
def prepare_faces(self, faces):
|
91 |
-
self.replacement_faces
|
92 |
for face in faces:
|
|
|
93 |
if "origin" in face:
|
94 |
face_threshold = face['threshold']
|
95 |
-
bboxes1, kpss1 = self.face_detector.autodetect(face['origin'], max_num=1)
|
96 |
-
if len(kpss1)
|
97 |
raise Exception('No face detected on "Face to replace" image')
|
98 |
feat_original = self.rec_app.get(face['origin'], kpss1[0])
|
99 |
else:
|
@@ -101,19 +109,21 @@ class Refacer:
|
|
101 |
self.first_face = True
|
102 |
feat_original = None
|
103 |
print('No origin image: First face change')
|
104 |
-
|
105 |
-
|
|
|
106 |
raise Exception('No face detected on "Destination face" image')
|
107 |
-
self.replacement_faces.append((feat_original,
|
108 |
|
109 |
-
def __convert_video(self,
|
110 |
if self.video_has_audio:
|
111 |
print("Merging audio with the refaced video...")
|
112 |
-
new_path = output_video_path + str(random.randint(0,
|
|
|
113 |
in1 = ffmpeg.input(output_video_path)
|
114 |
in2 = ffmpeg.input(video_path)
|
115 |
-
out = ffmpeg.output(in1.video, in2.audio, new_path,
|
116 |
-
out.run(overwrite_output=True,
|
117 |
else:
|
118 |
new_path = output_video_path
|
119 |
print("The video doesn't have audio, so post-processing is not necessary")
|
@@ -121,8 +131,9 @@ class Refacer:
|
|
121 |
print(f"The process has finished.\nThe refaced video can be found at {os.path.abspath(new_path)}")
|
122 |
return new_path
|
123 |
|
124 |
-
def __get_faces(self,
|
125 |
-
|
|
|
126 |
|
127 |
if bboxes.shape[0] == 0:
|
128 |
return []
|
@@ -138,42 +149,42 @@ class Refacer:
|
|
138 |
ret.append(face)
|
139 |
return ret
|
140 |
|
141 |
-
def process_first_face(self,
|
142 |
-
faces = self.__get_faces(frame,
|
143 |
if len(faces) != 0:
|
144 |
frame = self.face_swapper.get(frame, faces[0], self.replacement_faces[0][1], paste_back=True)
|
145 |
return frame
|
146 |
|
147 |
-
def process_faces(self,
|
148 |
-
faces = self.__get_faces(frame,
|
149 |
for rep_face in self.replacement_faces:
|
150 |
for i in range(len(faces) - 1, -1, -1):
|
151 |
sim = self.rec_app.compute_sim(rep_face[0], faces[i].embedding)
|
152 |
-
if sim
|
153 |
frame = self.face_swapper.get(frame, faces[i], rep_face[1], paste_back=True)
|
154 |
del faces[i]
|
155 |
break
|
156 |
return frame
|
157 |
|
158 |
-
def __check_video_has_audio(self,
|
159 |
self.video_has_audio = False
|
160 |
probe = ffmpeg.probe(video_path)
|
161 |
audio_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'audio'), None)
|
162 |
if audio_stream is not None:
|
163 |
self.video_has_audio = True
|
164 |
-
|
165 |
def reface_group(self, faces, frames, output):
|
166 |
-
with ThreadPoolExecutor(max_workers=self.use_num_cpus) as executor:
|
167 |
if self.first_face:
|
168 |
-
results = list(tqdm(executor.map(self.process_first_face, frames), total=len(frames),
|
169 |
else:
|
170 |
-
results = list(tqdm(executor.map(self.process_faces, frames), total=len(frames),
|
171 |
for result in results:
|
172 |
output.write(result)
|
173 |
|
174 |
def reface(self, video_path, faces):
|
175 |
self.__check_video_has_audio(video_path)
|
176 |
-
output_video_path = os.path.join('out',
|
177 |
self.prepare_faces(faces)
|
178 |
|
179 |
cap = cv2.VideoCapture(video_path)
|
@@ -187,24 +198,65 @@ class Refacer:
|
|
187 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
188 |
output = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
|
189 |
|
190 |
-
frames
|
191 |
self.k = 1
|
192 |
-
with tqdm(total=total_frames,
|
193 |
while cap.isOpened():
|
194 |
flag, frame = cap.read()
|
195 |
-
if flag and len(frame)
|
196 |
frames.append(frame.copy())
|
197 |
pbar.update()
|
198 |
else:
|
199 |
break
|
200 |
-
if len(frames) > 1000:
|
201 |
-
self.reface_group(faces,
|
202 |
-
frames
|
203 |
|
204 |
cap.release()
|
205 |
pbar.close()
|
206 |
|
207 |
-
self.reface_group(faces,
|
208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
from insightface.utils.storage import ensure_available
|
21 |
import re
|
22 |
import subprocess
|
|
|
|
|
23 |
|
24 |
class RefacerMode(Enum):
|
25 |
+
CPU, CUDA, COREML, TENSORRT = range(1, 5)
|
|
|
26 |
|
27 |
class Refacer:
|
28 |
+
def __init__(self,force_cpu=False,colab_performance=False):
|
29 |
self.first_face = False
|
30 |
self.force_cpu = force_cpu
|
31 |
self.colab_performance = colab_performance
|
|
|
35 |
self.__init_apps()
|
36 |
|
37 |
def __check_providers(self):
|
38 |
+
if self.force_cpu :
|
39 |
self.providers = ['CPUExecutionProvider']
|
40 |
else:
|
41 |
self.providers = rt.get_available_providers()
|
|
|
46 |
|
47 |
if len(self.providers) == 1 and 'CPUExecutionProvider' in self.providers:
|
48 |
self.mode = RefacerMode.CPU
|
49 |
+
self.use_num_cpus = mp.cpu_count()-1
|
50 |
+
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
51 |
print(f"CPU mode with providers {self.providers}")
|
52 |
elif self.colab_performance:
|
53 |
self.mode = RefacerMode.TENSORRT
|
54 |
+
self.use_num_cpus = mp.cpu_count()-1
|
55 |
+
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
56 |
print(f"TENSORRT mode with providers {self.providers}")
|
57 |
elif 'CoreMLExecutionProvider' in self.providers:
|
58 |
self.mode = RefacerMode.COREML
|
59 |
+
self.use_num_cpus = mp.cpu_count()-1
|
60 |
+
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
61 |
print(f"CoreML mode with providers {self.providers}")
|
62 |
elif 'CUDAExecutionProvider' in self.providers:
|
63 |
self.mode = RefacerMode.CUDA
|
|
|
66 |
if 'TensorrtExecutionProvider' in self.providers:
|
67 |
self.providers.remove('TensorrtExecutionProvider')
|
68 |
print(f"CUDA mode with providers {self.providers}")
|
69 |
+
"""
|
70 |
+
elif 'TensorrtExecutionProvider' in self.providers:
|
71 |
+
self.mode = RefacerMode.TENSORRT
|
72 |
+
#self.use_num_cpus = 1
|
73 |
+
#self.sess_options.intra_op_num_threads = 1
|
74 |
+
self.use_num_cpus = mp.cpu_count()-1
|
75 |
+
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
76 |
+
print(f"TENSORRT mode with providers {self.providers}")
|
77 |
+
"""
|
78 |
+
|
79 |
|
80 |
def __init_apps(self):
|
81 |
assets_dir = ensure_available('models', 'buffalo_l', root='~/.insightface')
|
82 |
|
83 |
model_path = os.path.join(assets_dir, 'det_10g.onnx')
|
84 |
sess_face = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
85 |
+
self.face_detector = SCRFD(model_path,sess_face)
|
86 |
+
self.face_detector.prepare(0,input_size=(640, 640))
|
87 |
|
88 |
+
model_path = os.path.join(assets_dir , 'w600k_r50.onnx')
|
89 |
sess_rec = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
90 |
+
self.rec_app = ArcFaceONNX(model_path,sess_rec)
|
91 |
self.rec_app.prepare(0)
|
92 |
|
93 |
model_path = 'inswapper_128.onnx'
|
94 |
sess_swap = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
95 |
+
self.face_swapper = INSwapper(model_path,sess_swap)
|
96 |
|
97 |
def prepare_faces(self, faces):
|
98 |
+
self.replacement_faces=[]
|
99 |
for face in faces:
|
100 |
+
#image1 = cv2.imread(face.origin)
|
101 |
if "origin" in face:
|
102 |
face_threshold = face['threshold']
|
103 |
+
bboxes1, kpss1 = self.face_detector.autodetect(face['origin'], max_num=1)
|
104 |
+
if len(kpss1)<1:
|
105 |
raise Exception('No face detected on "Face to replace" image')
|
106 |
feat_original = self.rec_app.get(face['origin'], kpss1[0])
|
107 |
else:
|
|
|
109 |
self.first_face = True
|
110 |
feat_original = None
|
111 |
print('No origin image: First face change')
|
112 |
+
#image2 = cv2.imread(face.destination)
|
113 |
+
_faces = self.__get_faces(face['destination'],max_num=1)
|
114 |
+
if len(_faces)<1:
|
115 |
raise Exception('No face detected on "Destination face" image')
|
116 |
+
self.replacement_faces.append((feat_original,_faces[0],face_threshold))
|
117 |
|
118 |
+
def __convert_video(self,video_path,output_video_path):
|
119 |
if self.video_has_audio:
|
120 |
print("Merging audio with the refaced video...")
|
121 |
+
new_path = output_video_path + str(random.randint(0,999)) + "_c.mp4"
|
122 |
+
#stream = ffmpeg.input(output_video_path)
|
123 |
in1 = ffmpeg.input(output_video_path)
|
124 |
in2 = ffmpeg.input(video_path)
|
125 |
+
out = ffmpeg.output(in1.video, in2.audio, new_path,video_bitrate=self.ffmpeg_video_bitrate,vcodec=self.ffmpeg_video_encoder)
|
126 |
+
out.run(overwrite_output=True,quiet=True)
|
127 |
else:
|
128 |
new_path = output_video_path
|
129 |
print("The video doesn't have audio, so post-processing is not necessary")
|
|
|
131 |
print(f"The process has finished.\nThe refaced video can be found at {os.path.abspath(new_path)}")
|
132 |
return new_path
|
133 |
|
134 |
+
def __get_faces(self,frame,max_num=0):
|
135 |
+
|
136 |
+
bboxes, kpss = self.face_detector.detect(frame,max_num=max_num,metric='default')
|
137 |
|
138 |
if bboxes.shape[0] == 0:
|
139 |
return []
|
|
|
149 |
ret.append(face)
|
150 |
return ret
|
151 |
|
152 |
+
def process_first_face(self,frame):
|
153 |
+
faces = self.__get_faces(frame,max_num=1)
|
154 |
if len(faces) != 0:
|
155 |
frame = self.face_swapper.get(frame, faces[0], self.replacement_faces[0][1], paste_back=True)
|
156 |
return frame
|
157 |
|
158 |
+
def process_faces(self,frame):
|
159 |
+
faces = self.__get_faces(frame,max_num=0)
|
160 |
for rep_face in self.replacement_faces:
|
161 |
for i in range(len(faces) - 1, -1, -1):
|
162 |
sim = self.rec_app.compute_sim(rep_face[0], faces[i].embedding)
|
163 |
+
if sim>=rep_face[2]:
|
164 |
frame = self.face_swapper.get(frame, faces[i], rep_face[1], paste_back=True)
|
165 |
del faces[i]
|
166 |
break
|
167 |
return frame
|
168 |
|
169 |
+
def __check_video_has_audio(self,video_path):
|
170 |
self.video_has_audio = False
|
171 |
probe = ffmpeg.probe(video_path)
|
172 |
audio_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'audio'), None)
|
173 |
if audio_stream is not None:
|
174 |
self.video_has_audio = True
|
175 |
+
|
176 |
def reface_group(self, faces, frames, output):
|
177 |
+
with ThreadPoolExecutor(max_workers = self.use_num_cpus) as executor:
|
178 |
if self.first_face:
|
179 |
+
results = list(tqdm(executor.map(self.process_first_face, frames), total=len(frames),desc="Processing frames"))
|
180 |
else:
|
181 |
+
results = list(tqdm(executor.map(self.process_faces, frames), total=len(frames),desc="Processing frames"))
|
182 |
for result in results:
|
183 |
output.write(result)
|
184 |
|
185 |
def reface(self, video_path, faces):
|
186 |
self.__check_video_has_audio(video_path)
|
187 |
+
output_video_path = os.path.join('out',Path(video_path).name)
|
188 |
self.prepare_faces(faces)
|
189 |
|
190 |
cap = cv2.VideoCapture(video_path)
|
|
|
198 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
199 |
output = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
|
200 |
|
201 |
+
frames=[]
|
202 |
self.k = 1
|
203 |
+
with tqdm(total=total_frames,desc="Extracting frames") as pbar:
|
204 |
while cap.isOpened():
|
205 |
flag, frame = cap.read()
|
206 |
+
if flag and len(frame)>0:
|
207 |
frames.append(frame.copy())
|
208 |
pbar.update()
|
209 |
else:
|
210 |
break
|
211 |
+
if (len(frames) > 1000):
|
212 |
+
self.reface_group(faces,frames,output)
|
213 |
+
frames=[]
|
214 |
|
215 |
cap.release()
|
216 |
pbar.close()
|
217 |
|
218 |
+
self.reface_group(faces,frames,output)
|
219 |
+
frames=[]
|
220 |
+
output.release()
|
221 |
+
|
222 |
+
return self.__convert_video(video_path,output_video_path)
|
223 |
+
|
224 |
+
def __try_ffmpeg_encoder(self, vcodec):
|
225 |
+
print(f"Trying FFMPEG {vcodec} encoder")
|
226 |
+
command = ['ffmpeg', '-y', '-f','lavfi','-i','testsrc=duration=1:size=1280x720:rate=30','-vcodec',vcodec,'testsrc.mp4']
|
227 |
+
try:
|
228 |
+
subprocess.run(command, check=True, capture_output=True).stderr
|
229 |
+
except subprocess.CalledProcessError as e:
|
230 |
+
print(f"FFMPEG {vcodec} encoder doesn't work -> Disabled.")
|
231 |
+
return False
|
232 |
+
print(f"FFMPEG {vcodec} encoder works")
|
233 |
+
return True
|
234 |
+
|
235 |
+
def __check_encoders(self):
|
236 |
+
self.ffmpeg_video_encoder='libx264'
|
237 |
+
self.ffmpeg_video_bitrate='0'
|
238 |
+
|
239 |
+
pattern = r"encoders: ([a-zA-Z0-9_]+(?: [a-zA-Z0-9_]+)*)"
|
240 |
+
command = ['ffmpeg', '-codecs', '--list-encoders']
|
241 |
+
commandout = subprocess.run(command, check=True, capture_output=True).stdout
|
242 |
+
result = commandout.decode('utf-8').split('\n')
|
243 |
+
for r in result:
|
244 |
+
if "264" in r:
|
245 |
+
encoders = re.search(pattern, r).group(1).split(' ')
|
246 |
+
for v_c in Refacer.VIDEO_CODECS:
|
247 |
+
for v_k in encoders:
|
248 |
+
if v_c == v_k:
|
249 |
+
if self.__try_ffmpeg_encoder(v_k):
|
250 |
+
self.ffmpeg_video_encoder=v_k
|
251 |
+
self.ffmpeg_video_bitrate=Refacer.VIDEO_CODECS[v_k]
|
252 |
+
print(f"Video codec for FFMPEG: {self.ffmpeg_video_encoder}")
|
253 |
+
return
|
254 |
|
255 |
+
VIDEO_CODECS = {
|
256 |
+
'h264_videotoolbox':'0', #osx HW acceleration
|
257 |
+
'h264_nvenc':'0', #NVIDIA HW acceleration
|
258 |
+
#'h264_qsv', #Intel HW acceleration
|
259 |
+
#'h264_vaapi', #Intel HW acceleration
|
260 |
+
#'h264_omx', #HW acceleration
|
261 |
+
'libx264':'0' #No HW acceleration
|
262 |
+
}
|