Skip to content

Commit

Permalink
Upgraded gradio to 4.43, hoping their connectivity issue is fixed
Browse files Browse the repository at this point in the history
Added new action when no face detected -> use last swapped
Specified image format for image controls - opening new tabs on preview images possible again!
Hardcoded image output format for livecam to jpeg - might be faster than previous webp
Chain events to be only executed if previous was a success
  • Loading branch information
C0untFloyd committed Sep 6, 2024
1 parent 4363730 commit c970afd
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--extra-index-url https://download.pytorch.org/whl/cu118

numpy==1.26.4
gradio==4.42.0
gradio==4.43.0
fastapi<0.113.0
opencv-python==4.9.0.80
onnx==1.16.0
Expand Down
18 changes: 15 additions & 3 deletions roop/ProcessMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class eNoFaceAction():
USE_ORIGINAL_FRAME = 0
RETRY_ROTATED = 1
SKIP_FRAME = 2
SKIP_FRAME_IF_DISSIMILAR = 3
SKIP_FRAME_IF_DISSIMILAR = 3,
USE_LAST_SWAPPED = 4



Expand Down Expand Up @@ -70,7 +71,8 @@ class ProcessMgr():
progress_gradio = None
total_frames = 0


num_frames_no_face = 0
last_swapped_frame = None


plugins = {
Expand Down Expand Up @@ -103,6 +105,8 @@ def reuseOldProcessor(self, name:str):
def initialize(self, input_faces, target_faces, options):
self.input_face_datas = input_faces
self.target_face_datas = target_faces
self.num_frames_no_face = 0
self.last_swapped_frame = None
self.options = options
devicename = get_device()

Expand Down Expand Up @@ -329,8 +333,16 @@ def process_frame(self, frame:Frame):
if roop.globals.no_face_action == eNoFaceAction.SKIP_FRAME_IF_DISSIMILAR:
if len(self.input_face_datas) > num_swapped:
return None
self.num_frames_no_face = 0
self.last_swapped_frame = temp_frame.copy()
return temp_frame
if roop.globals.no_face_action == eNoFaceAction.USE_ORIGINAL_FRAME:
if roop.globals.no_face_action == eNoFaceAction.USE_LAST_SWAPPED:
if self.last_swapped_frame is not None and self.num_frames_no_face < self.options.max_num_reuse_frame:
self.num_frames_no_face += 1
return self.last_swapped_frame.copy()
return frame

elif roop.globals.no_face_action == eNoFaceAction.USE_ORIGINAL_FRAME:
return frame
if roop.globals.no_face_action == eNoFaceAction.SKIP_FRAME:
#This only works with in-mem processing, as it simply skips the frame.
Expand Down
3 changes: 2 additions & 1 deletion roop/ProcessOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ def __init__(self, processordefines:dict, face_distance, blend_ratio, swap_mode
self.show_face_area_overlay = show_face_area
self.show_face_masking = show_mask
self.subsample_size = subsample_size
self.restore_original_mouth = restore_original_mouth
self.restore_original_mouth = restore_original_mouth
self.max_num_reuse_frame = 15
2 changes: 1 addition & 1 deletion roop/metadata.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name = 'roop unleashed'
version = '4.2.1'
version = '4.2.3'
2 changes: 1 addition & 1 deletion ui/tabs/facemgr_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def facemgr_tab() -> None:
Add multiple reference images into a faceset file.
""")
with gr.Row():
videoimagefst = gr.Image(label="Cut face from video frame", height=576, interactive=False, visible=True)
videoimagefst = gr.Image(label="Cut face from video frame", height=576, interactive=False, visible=True, format="jpeg")
with gr.Row():
frame_num_fst = gr.Slider(1, 1, value=1, label="Frame Number", info='0:00:00', step=1.0, interactive=False)
fb_cutfromframe = gr.Button("Use faces from this frame", variant='secondary', interactive=False)
Expand Down
18 changes: 12 additions & 6 deletions ui/tabs/faceswap_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
is_processing = False

list_files_process : list[ProcessEntry] = []
no_face_choices = ["Use untouched original frame","Retry rotated", "Skip Frame", "Skip Frame if no similar face"]
no_face_choices = ["Use untouched original frame","Retry rotated", "Skip Frame", "Skip Frame if no similar face", "Use last swapped"]

current_video_fps = 50

Expand Down Expand Up @@ -144,7 +144,7 @@ def faceswap_tab():
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)

with gr.Column(scale=2):
previewimage = gr.Image(label="Preview Image", height=576, interactive=False, visible=True)
previewimage = gr.Image(label="Preview Image", height=576, interactive=False, visible=True, format=get_gradio_output_format())
maskimage = gr.ImageEditor(label="Manual mask Image", sources=["clipboard"], transforms="", type="numpy",
brush=gr.Brush(color_mode="fixed", colors=["rgba(255, 255, 255, 1"]), interactive=True, visible=False)
with gr.Row(variant='panel'):
Expand Down Expand Up @@ -212,7 +212,7 @@ def faceswap_tab():
previewinputs = [preview_frame_num, bt_destfiles, fake_preview, ui.globals.ui_selected_enhancer, selected_face_detection,
max_face_distance, ui.globals.ui_blend_ratio, selected_mask_engine, clip_text, no_face_action, vr_mode, autorotate, maskimage, chk_showmaskoffsets, chk_restoreoriginalmouth, num_swap_steps, ui.globals.ui_upscale]
previewoutputs = [previewimage, maskimage, preview_frame_num]
input_faces.select(on_select_input_face, None, None).then(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
input_faces.select(on_select_input_face, None, None).success(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)

bt_move_left_input.click(fn=move_selected_input, inputs=[bt_move_left_input], outputs=[input_faces])
bt_move_right_input.click(fn=move_selected_input, inputs=[bt_move_right_input], outputs=[input_faces])
Expand All @@ -234,8 +234,8 @@ def faceswap_tab():
bt_remove_selected_target_face.click(fn=remove_selected_target_face, outputs=[target_faces])

forced_fps.change(fn=on_fps_changed, inputs=[forced_fps], show_progress='hidden')
bt_destfiles.change(fn=on_destfiles_changed, inputs=[bt_destfiles], outputs=[preview_frame_num, text_frame_clip], show_progress='hidden').then(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden')
bt_destfiles.select(fn=on_destfiles_selected, outputs=[preview_frame_num, text_frame_clip, forced_fps], show_progress='hidden').then(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden')
bt_destfiles.change(fn=on_destfiles_changed, inputs=[bt_destfiles], outputs=[preview_frame_num, text_frame_clip], show_progress='hidden').success(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden')
bt_destfiles.select(fn=on_destfiles_selected, outputs=[preview_frame_num, text_frame_clip, forced_fps], show_progress='hidden').success(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden')
bt_destfiles.clear(fn=on_clear_destfiles, outputs=[target_faces, selected_face_detection])
resultfiles.select(fn=on_resultfiles_selected, inputs=[resultfiles], outputs=[resultimage, resultvideo])

Expand All @@ -252,7 +252,7 @@ def faceswap_tab():
inputs=[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],
outputs=[bt_start, bt_stop, resultfiles], show_progress='full')
after_swap_event = start_event.then(fn=on_resultfiles_finished, inputs=[resultfiles], outputs=[resultimage, resultvideo])
after_swap_event = start_event.success(fn=on_resultfiles_finished, inputs=[resultfiles], outputs=[resultimage, resultvideo])

bt_stop.click(fn=stop_swap, cancels=[start_event, after_swap_event], outputs=[bt_start, bt_stop], queue=False)

Expand Down Expand Up @@ -813,6 +813,12 @@ def on_resultfiles_finished(files):
return display_output(filename)


def get_gradio_output_format():
if roop.globals.CFG.output_image_format == "jpg":
return "jpeg"
return roop.globals.CFG.output_image_format


def display_output(filename):
if util.is_video(filename) and roop.globals.CFG.output_show_video:
return gr.Image(visible=False), gr.Video(visible=True, value=filename)
Expand Down
2 changes: 1 addition & 1 deletion ui/tabs/livecam_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def livecam_tab():
dd_reso = gr.Dropdown(choices=["640x480","1280x720", "1920x1080"], value="1280x720", label="Fake Camera Resolution", interactive=True)

with gr.Row():
fake_cam_image = gr.Image(label='Fake Camera Output', interactive=False)
fake_cam_image = gr.Image(label='Fake Camera Output', interactive=False, format="jpeg")

start_event = bt_start.click(fn=start_cam, inputs=[cb_obs, camera_num, dd_reso, ui.globals.ui_selected_enhancer, ui.globals.ui_blend_ratio, ui.globals.ui_upscale],outputs=[bt_start, bt_stop,fake_cam_image])
bt_stop.click(fn=stop_swap, cancels=[start_event], outputs=[bt_start, bt_stop], queue=False)
Expand Down

0 comments on commit c970afd

Please sign in to comment.