Merge pull request #280 from s0md3v/next

support multi-face replacament
This commit is contained in:
Somdev Sangwan 2023-06-02 15:04:15 +05:30 committed by GitHub
commit f7e029e6f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 29 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
__pycache__
*.onnx

View File

@ -12,9 +12,16 @@ def get_face_analyser():
return FACE_ANALYSER return FACE_ANALYSER
def get_face(img_data): def get_face_single(img_data):
face = get_face_analyser().get(img_data) face = get_face_analyser().get(img_data)
try: try:
return sorted(face, key=lambda x: x.bbox[0])[0] return sorted(face, key=lambda x: x.bbox[0])[0]
except IndexError: except IndexError:
return None return None
def get_face_many(img_data):
try:
return get_face_analyser().get(img_data)
except IndexError:
return None

View File

@ -1,7 +1,8 @@
import onnxruntime import onnxruntime
use_gpu = False use_gpu = False
providers = onnxruntime.get_available_providers() all_faces = False
providers = onnxruntime.get_available_providers()
if 'TensorrtExecutionProvider' in providers:
providers.remove('TensorrtExecutionProvider') if 'TensorrtExecutionProvider' in providers:
providers.remove('TensorrtExecutionProvider')

View File

@ -3,7 +3,7 @@ from tqdm import tqdm
import cv2 import cv2
import insightface import insightface
import core.globals import core.globals
from core.analyser import get_face from core.analyser import get_face_single, get_face_many
FACE_SWAPPER = None FACE_SWAPPER = None
@ -16,19 +16,41 @@ def get_face_swapper():
return FACE_SWAPPER return FACE_SWAPPER
def swap_face_in_frame(source_face, target_face, frame):
if target_face:
return get_face_swapper().get(frame, target_face, source_face, paste_back=True)
return frame
def process_faces(source_face, frame, progress, all_faces=False):
if all_faces:
many_faces = get_face_many(frame)
if many_faces:
for face in many_faces:
frame = swap_face_in_frame(source_face, face, frame)
progress.set_postfix(status='.', refresh=True)
else:
progress.set_postfix(status='S', refresh=True)
else:
face = get_face_single(frame)
if face:
frame = swap_face_in_frame(source_face, face, frame)
progress.set_postfix(status='.', refresh=True)
else:
progress.set_postfix(status='S', refresh=True)
return frame
def process_video(source_img, frame_paths): def process_video(source_img, frame_paths):
source_face = get_face(cv2.imread(source_img)) source_face = get_face_single(cv2.imread(source_img))
with tqdm(total=len(frame_paths), desc="Processing", unit="frame", dynamic_ncols=True, bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]') as progress: progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
with tqdm(total=len(frame_paths), desc="Processing", unit="frame", dynamic_ncols=True, bar_format=progress_bar_format) as progress:
for frame_path in frame_paths: for frame_path in frame_paths:
frame = cv2.imread(frame_path) frame = cv2.imread(frame_path)
try: try:
face = get_face(frame) result = process_faces(source_face, frame, progress, core.globals.all_faces)
if face: cv2.imwrite(frame_path, result)
result = get_face_swapper().get(frame, face, source_face, paste_back=True)
cv2.imwrite(frame_path, result)
progress.set_postfix(status='.', refresh=True)
else:
progress.set_postfix(status='S', refresh=True)
except Exception: except Exception:
progress.set_postfix(status='E', refresh=True) progress.set_postfix(status='E', refresh=True)
pass pass
@ -37,8 +59,8 @@ def process_video(source_img, frame_paths):
def process_img(source_img, target_path, output_file): def process_img(source_img, target_path, output_file):
frame = cv2.imread(target_path) frame = cv2.imread(target_path)
face = get_face(frame) face = get_face_single(frame)
source_face = get_face(cv2.imread(source_img)) source_face = get_face_single(cv2.imread(source_img))
result = get_face_swapper().get(frame, face, source_face, paste_back=True) result = get_face_swapper().get(frame, face, source_face, paste_back=True)
cv2.imwrite(output_file, result) cv2.imwrite(output_file, result)
print("\n\nImage saved as:", output_file, "\n\n") print("\n\nImage saved as:", output_file, "\n\n")

28
run.py
View File

@ -22,7 +22,7 @@ from PIL import Image, ImageTk
import core.globals import core.globals
from core.swapper import process_video, process_img from core.swapper import process_video, process_img
from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames, rreplace from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames, rreplace
from core.analyser import get_face from core.analyser import get_face_single
if 'ROCMExecutionProvider' in core.globals.providers: if 'ROCMExecutionProvider' in core.globals.providers:
del torch del torch
@ -40,6 +40,7 @@ parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps',
parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False) parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False)
parser.add_argument('--max-memory', help='maximum amount of RAM in GB to be used', type=int) parser.add_argument('--max-memory', help='maximum amount of RAM in GB to be used', type=int)
parser.add_argument('--max-cores', help='number of cores to be use for CPU mode', dest='cores_count', type=int, default=max(psutil.cpu_count() - 2, 2)) parser.add_argument('--max-cores', help='number of cores to be use for CPU mode', dest='cores_count', type=int, default=max(psutil.cpu_count() - 2, 2))
parser.add_argument('--all-faces', help='swap all faces in frame', dest='all_faces', action='store_true', default=False)
for name, value in vars(parser.parse_args()).items(): for name, value in vars(parser.parse_args()).items():
args[name] = value args[name] = value
@ -149,6 +150,10 @@ def toggle_fps_limit():
args['keep_fps'] = int(limit_fps.get() != True) args['keep_fps'] = int(limit_fps.get() != True)
def toggle_all_faces():
core.globals.all_faces = True if all_faces.get() == 1 else False
def toggle_keep_frames(): def toggle_keep_frames():
args['keep_frames'] = int(keep_frames.get()) args['keep_frames'] = int(keep_frames.get())
@ -181,12 +186,12 @@ def start():
global pool global pool
pool = mp.Pool(args['cores_count']) pool = mp.Pool(args['cores_count'])
target_path = args['target_path'] target_path = args['target_path']
test_face = get_face(cv2.imread(args['source_img'])) test_face = get_face_single(cv2.imread(args['source_img']))
if not test_face: if not test_face:
print("\n[WARNING] No face detected in source image. Please try with another one.\n") print("\n[WARNING] No face detected in source image. Please try with another one.\n")
return return
if is_img(target_path): if is_img(target_path):
if predict_image(target_path) > 0.7: if predict_image(target_path) > 0.85:
quit() quit()
process_img(args['source_img'], target_path, args['output_file']) process_img(args['source_img'], target_path, args['output_file'])
status("swap successful!") status("swap successful!")
@ -196,7 +201,7 @@ def start():
quit() quit()
video_name_full = target_path.split("/")[-1] video_name_full = target_path.split("/")[-1]
video_name = os.path.splitext(video_name_full)[0] video_name = os.path.splitext(video_name_full)[0]
output_dir = os.path.dirname(target_path) + "/" + video_name output_dir = os.path.dirname(target_path) + "/" + video_name if os.path.dirname(target_path) else video_name
Path(output_dir).mkdir(exist_ok=True) Path(output_dir).mkdir(exist_ok=True)
status("detecting video's FPS...") status("detecting video's FPS...")
fps, exact_fps = detect_fps(target_path) fps, exact_fps = detect_fps(target_path)
@ -252,15 +257,20 @@ if __name__ == "__main__":
target_button = tk.Button(window, text="Select a target", command=select_target, bg="#2d3436", fg="#74b9ff", highlightthickness=4, relief="flat", highlightbackground="#74b9ff", activebackground="#74b9ff", borderwidth=4) target_button = tk.Button(window, text="Select a target", command=select_target, bg="#2d3436", fg="#74b9ff", highlightthickness=4, relief="flat", highlightbackground="#74b9ff", activebackground="#74b9ff", borderwidth=4)
target_button.place(x=360,y=320,width=180,height=80) target_button.place(x=360,y=320,width=180,height=80)
# All faces checkbox
all_faces = tk.IntVar()
all_faces_checkbox = tk.Checkbutton(window, anchor="w", relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Process all faces in frame", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=all_faces, command=toggle_all_faces)
all_faces_checkbox.place(x=60,y=500,width=240,height=31)
# FPS limit checkbox # FPS limit checkbox
limit_fps = tk.IntVar(None, not args['keep_fps']) limit_fps = tk.IntVar(None, not args['keep_fps'])
fps_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit) fps_checkbox = tk.Checkbutton(window, anchor="w", relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit)
fps_checkbox.place(x=30,y=500,width=240,height=31) fps_checkbox.place(x=60,y=475,width=240,height=31)
# Keep frames checkbox # Keep frames checkbox
keep_frames = tk.IntVar(None, args['keep_frames']) keep_frames = tk.IntVar(None, args['keep_frames'])
frames_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames) frames_checkbox = tk.Checkbutton(window, anchor="w", relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames)
frames_checkbox.place(x=37,y=450,width=240,height=31) frames_checkbox.place(x=60,y=450,width=240,height=31)
# Start button # Start button
start_button = tk.Button(window, text="Start", bg="#f1c40f", relief="flat", borderwidth=0, highlightthickness=0, command=lambda: [save_file(), start()]) start_button = tk.Button(window, text="Start", bg="#f1c40f", relief="flat", borderwidth=0, highlightthickness=0, command=lambda: [save_file(), start()])
@ -269,5 +279,5 @@ if __name__ == "__main__":
# Status label # Status label
status_label = tk.Label(window, width=580, justify="center", text="Status: waiting for input...", fg="#2ecc71", bg="#2d3436") status_label = tk.Label(window, width=580, justify="center", text="Status: waiting for input...", fg="#2ecc71", bg="#2d3436")
status_label.place(x=10,y=640,width=580,height=30) status_label.place(x=10,y=640,width=580,height=30)
window.mainloop() window.mainloop()