Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch face swap option #1050

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions roop/FaceSet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np

class FaceSet:
# The filename of the faceset
name = ''
faces = []
ref_images = []
embedding_average = 'None'
Expand Down
74 changes: 50 additions & 24 deletions roop/ProcessMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def initialize(self, input_faces, target_faces, options):
def run_batch(self, source_files, target_files, threads:int = 1):
progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
self.total_frames = len(source_files)
if roop.globals.loop_through_all_faces:
self.total_frames *= len(self.input_face_datas)
self.num_threads = threads
with tqdm(total=self.total_frames, desc='Processing', unit='frame', dynamic_ncols=True, bar_format=progress_bar_format) as progress:
with ThreadPoolExecutor(max_workers=threads) as executor:
Expand All @@ -199,19 +201,36 @@ def process_frames(self, source_files: List[str], target_files: List[str], curre

# Decode the byte array into an OpenCV image
temp_frame = cv2.imdecode(np.fromfile(f, dtype=np.uint8), cv2.IMREAD_COLOR)
if temp_frame is not None:
if self.options.frame_processing:
for p in self.processors:
frame = p.Run(temp_frame)
resimg = frame
else:
resimg = self.process_frame(temp_frame)
if resimg is not None:
i = source_files.index(f)
# Also let numpy write the file to support utf-8/16 filenames
cv2.imencode(f'.{roop.globals.CFG.output_image_format}',resimg)[1].tofile(target_files[i])
if update:
update()

input_face_amount = 1

if roop.globals.loop_through_all_faces:
input_face_amount = len(self.input_face_datas)

for input_face_idx in range(input_face_amount):
if temp_frame is not None:
if self.options.frame_processing:
for p in self.processors:
frame = p.Run(temp_frame)
resimg = frame
else:
resimg = self.process_frame(temp_frame, input_face_idx)
if resimg is not None:
i = source_files.index(f)
target_file = target_files[i]

# Modify filename when processing multiple faces
if roop.globals.loop_through_all_faces:
# Prepend the face name to the target filename for unique identification
dir_name, base_name = os.path.split(target_file)
faceset_name = roop.globals.INPUT_FACESETS[input_face_idx].name
new_base_name = faceset_name + "-" + base_name
target_file = os.path.join(dir_name, new_base_name)

# Also let numpy write the file to support utf-8/16 filenames
cv2.imencode(f'.{roop.globals.CFG.output_image_format}',resimg)[1].tofile(target_file)
if update:
update()



Expand Down Expand Up @@ -359,11 +378,11 @@ def update_progress(self, progress: Any = None) -> None:



def process_frame(self, frame:Frame):
def process_frame(self, frame:Frame, input_face_idx:int = -1):
if len(self.input_face_datas) < 1 and not self.options.show_face_masking:
return frame
temp_frame = frame.copy()
num_swapped, temp_frame = self.swap_faces(frame, temp_frame)
num_swapped, temp_frame = self.swap_faces(frame, temp_frame, input_face_idx)
if num_swapped > 0:
if roop.globals.no_face_action == eNoFaceAction.SKIP_FRAME_IF_DISSIMILAR:
if len(self.input_face_datas) > num_swapped:
Expand All @@ -386,38 +405,45 @@ def process_frame(self, frame:Frame):
#alternatively, it could mark all the necessary frames for deletion, delete them at the end, then rename the remaining frames that might work?
return None
else:
return self.retry_rotated(frame)
return self.retry_rotated(frame, input_face_idx)

def retry_rotated(self, frame):
def retry_rotated(self, frame, input_face_idx:int = -1):
copyframe = frame.copy()
copyframe = rotate_clockwise(copyframe)
temp_frame = copyframe.copy()
num_swapped, temp_frame = self.swap_faces(copyframe, temp_frame)
num_swapped, temp_frame = self.swap_faces(copyframe, temp_frame, input_face_idx)
if num_swapped > 0:
return rotate_anticlockwise(temp_frame)

copyframe = frame.copy()
copyframe = rotate_anticlockwise(copyframe)
temp_frame = copyframe.copy()
num_swapped, temp_frame = self.swap_faces(copyframe, temp_frame)
num_swapped, temp_frame = self.swap_faces(copyframe, temp_frame, input_face_idx)
if num_swapped > 0:
return rotate_clockwise(temp_frame)
del copyframe
return frame



def swap_faces(self, frame, temp_frame):
def swap_faces(self, frame, temp_frame, input_face_idx:int = -1):
num_faces_found = 0

selected_face_index = self.options.selected_index

# Dynamically overrides the selected index if we are processing multiple faces
if roop.globals.loop_through_all_faces:
if input_face_idx != -1:
selected_face_index = input_face_idx

if self.options.swap_mode == "first":
face = get_first_face(frame)

if face is None:
return num_faces_found, frame

num_faces_found += 1
temp_frame = self.process_face(self.options.selected_index, face, temp_frame)
temp_frame = self.process_face(selected_face_index, face, temp_frame)
del face

else:
Expand All @@ -428,7 +454,7 @@ def swap_faces(self, frame, temp_frame):
if self.options.swap_mode == "all":
for face in faces:
num_faces_found += 1
temp_frame = self.process_face(self.options.selected_index, face, temp_frame)
temp_frame = self.process_face(selected_face_index, face, temp_frame)

elif self.options.swap_mode == "all_input" or self.options.swap_mode == "all_random":
for i,face in enumerate(faces):
Expand All @@ -446,7 +472,7 @@ def swap_faces(self, frame, temp_frame):
if compute_cosine_distance(tf.embedding, face.embedding) <= self.options.face_distance_threshold:
if i < len(self.input_face_datas):
if use_index:
temp_frame = self.process_face(self.options.selected_index, face, temp_frame)
temp_frame = self.process_face(selected_face_index, face, temp_frame)
else:
temp_frame = self.process_face(i, face, temp_frame)
num_faces_found += 1
Expand All @@ -457,7 +483,7 @@ def swap_faces(self, frame, temp_frame):
for face in faces:
if face.sex == gender:
num_faces_found += 1
temp_frame = self.process_face(self.options.selected_index, face, temp_frame)
temp_frame = self.process_face(selected_face_index, face, temp_frame)

# might be slower but way more clean to release everything here
for face in faces:
Expand Down
33 changes: 30 additions & 3 deletions roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,27 @@ def batch_process(output_method, files:list[ProcessEntry], use_new_method) -> No
imagefiles.append(f)

elif util.is_video(fullname) or util.has_extension(fullname, ['gif']):
destination = util.get_destfilename_from_path(fullname, roop.globals.output_path, f'__temp.{roop.globals.CFG.output_video_format}')
f.finalname = destination
videofiles.append(f)
input_face_amount = 1
selected_index = None

# If enabled, add video entries for the amount of all faces
if roop.globals.loop_through_all_faces:
input_face_amount = len(roop.globals.INPUT_FACESETS)
selected_index = process_mgr.options.selected_index

for _ in range(input_face_amount):
new_f = ProcessEntry(f.filename, f.startframe, f.endframe, f.fps)
prefix = ''
# Modify filename when processing multiple faces
if roop.globals.loop_through_all_faces:
# Prepend the face name to the target filename for unique identification
prefix = roop.globals.INPUT_FACESETS[selected_index].name + '-'
selected_index += 1
if selected_index >= len(roop.globals.INPUT_FACESETS):
selected_index = 0
destination = util.get_destfilename_from_path(fullname, roop.globals.output_path, f'__temp.{roop.globals.CFG.output_video_format}', prefix)
new_f.finalname = destination
videofiles.append(new_f)



Expand Down Expand Up @@ -375,6 +393,15 @@ def batch_process(output_method, files:list[ProcessEntry], use_new_method) -> No
elapsed_time = time() - start_processing
average_fps = (v.endframe - v.startframe) / elapsed_time
update_status(f'\nProcessing {os.path.basename(destination)} took {elapsed_time:.2f} secs, {average_fps:.2f} frames/s')

if roop.globals.loop_through_all_faces:
# last video, no need to switch faceset anymore
if index != len(videofiles) - 1:
process_mgr.options.selected_index += 1
if process_mgr.options.selected_index >= len(roop.globals.INPUT_FACESETS):
process_mgr.options.selected_index = 0
update_status("Switching to next face, face name: " + roop.globals.INPUT_FACESETS[process_mgr.options.selected_index].name)

end_processing('Finished')


Expand Down
2 changes: 2 additions & 0 deletions roop/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
distance_threshold = 0.65
default_det_size = True

loop_through_all_faces = False

no_face_action = 0

processing = False
Expand Down
6 changes: 3 additions & 3 deletions roop/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ def normalize_output_path(source_path: str, target_path: str, output_path: str)


def get_destfilename_from_path(
srcfilepath: str, destfilepath: str, extension: str
srcfilepath: str, destfilepath: str, extension: str, prefix: str = ""
) -> str:
fn, ext = os.path.splitext(os.path.basename(srcfilepath))
if "." in extension:
return os.path.join(destfilepath, f"{fn}{extension}")
return os.path.join(destfilepath, f"{fn}{extension}{ext}")
return os.path.join(destfilepath, f"{prefix}{fn}{extension}")
return os.path.join(destfilepath, f"{prefix}{fn}{extension}{ext}")


def replace_template(file_path: str, index: int = 0) -> str:
Expand Down
8 changes: 6 additions & 2 deletions ui/tabs/faceswap_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def faceswap_tab():
bt_srcfiles = gr.Files(label='Source Images or Facesets', file_count="multiple", file_types=["image", ".fsz", ".webp"], elem_id='filelist', height=233)
bt_destfiles = gr.Files(label='Target File(s)', file_count="multiple", file_types=["image", "video", ".webp"], elem_id='filelist', height=233)
with gr.Row(variant='panel'):
roop.globals.loop_through_all_faces = gr.Checkbox(label="Batch face swap", info="Loop through all faces for all target file(s)", value=False)
ui.globals.ui_selected_swap_model = gr.Dropdown(model_swap_choices, value=model_swap_choices[0], label="Specify Face Swap Model")
forced_fps = gr.Slider(minimum=0, maximum=120, value=0, label="Video FPS", info='Overrides detected fps if not 0', step=1.0, interactive=True, container=True)

Expand Down Expand Up @@ -254,7 +255,7 @@ def faceswap_tab():

start_event = bt_start.click(fn=start_swap,
inputs=[ui.globals.ui_selected_swap_model, output_method, ui.globals.ui_selected_enhancer, selected_face_detection, roop.globals.keep_frames, roop.globals.wait_after_extraction,
roop.globals.skip_audio, max_face_distance, ui.globals.ui_blend_ratio, selected_mask_engine, clip_text,video_swapping_method, no_face_action, vr_mode, autorotate, chk_restoreoriginalmouth, num_swap_steps, ui.globals.ui_upscale, maskimage],
roop.globals.skip_audio, max_face_distance, ui.globals.ui_blend_ratio, selected_mask_engine, clip_text,video_swapping_method, no_face_action, vr_mode, autorotate, chk_restoreoriginalmouth, num_swap_steps, ui.globals.ui_upscale, roop.globals.loop_through_all_faces, maskimage],
outputs=[bt_start, bt_stop, resultfiles], show_progress='full')
after_swap_event = start_event.success(fn=on_resultfiles_finished, inputs=[resultfiles], outputs=[resultimage, resultvideo])

Expand Down Expand Up @@ -354,6 +355,7 @@ def on_srcfile_changed(srcfiles, progress=gr.Progress()):
if len(face_set.faces) > 0:
if len(face_set.faces) > 1:
face_set.AverageEmbeddings()
face_set.name = os.path.basename(source_path).split('.')[0]
roop.globals.INPUT_FACESETS.append(face_set)

elif util.has_image_extension(source_path):
Expand All @@ -368,6 +370,7 @@ def on_srcfile_changed(srcfiles, progress=gr.Progress()):
face_set.faces.append(face)
image = util.convert_to_gradio(f[1])
ui.globals.ui_input_thumbs.append(image)
face_set.name = os.path.basename(source_path).split('.')[0]
roop.globals.INPUT_FACESETS.append(face_set)

progress(1.0)
Expand Down Expand Up @@ -686,7 +689,7 @@ def translate_swap_mode(dropdown_text):


def start_swap( swap_model, output_method, enhancer, detection, keep_frames, wait_after_extraction, skip_audio, face_distance, blend_ratio,
selected_mask_engine, clip_text, processing_method, no_face_action, vr_mode, autorotate, restore_original_mouth, num_swap_steps, upsample, imagemask, progress=gr.Progress()):
selected_mask_engine, clip_text, processing_method, no_face_action, vr_mode, autorotate, restore_original_mouth, num_swap_steps, upsample, loop_through_all_faces, imagemask, progress=gr.Progress()):
from ui.main import prepare_environment
from roop.core import batch_process_regular
global is_processing, list_files_process
Expand Down Expand Up @@ -715,6 +718,7 @@ def start_swap( swap_model, output_method, enhancer, detection, keep_frames, wai
roop.globals.vr_mode = vr_mode
roop.globals.autorotate_faces = autorotate
roop.globals.subsample_size = int(upsample[:3])
roop.globals.loop_through_all_faces = loop_through_all_faces
mask_engine = map_mask_engine(selected_mask_engine, clip_text)

if roop.globals.face_swap_mode == 'selected':
Expand Down