Skip to content

Commit

Permalink
Bugfix: Layer Mask crashing without valid image
Browse files Browse the repository at this point in the history
Removed unnecessary activate live-cam option
Added time info to preview frame and changed frame slider event to allow faster changes
Upgraded to gradio 4.21.0
  • Loading branch information
C0untFloyd committed Mar 18, 2024
1 parent 3bb6e2b commit 64e2275
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ Additional commandline arguments are currently unsupported and settings should b


### Changelog
**18.03.2024** v3.6.0

- Updated to Gradio 4.21.0 - requiring many changes under the hood
- New manual masking (draw the mask yourself)
- Extras Tab, streamlined cutting/joining videos
- Re-added face selection by gender (on-demand loading, default turned off)
- Removed unnecessary activate live-cam option
- Added time info to preview frame and changed frame slider event to allow faster changes


**10.03.2024** v3.5.5

Expand Down
1 change: 0 additions & 1 deletion config_colab.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
clear_output: true
force_cpu: false
live_cam_start_active: false
max_threads: 3
memory_limit: 0
output_image_format: png
Expand Down
4 changes: 2 additions & 2 deletions 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.2
gradio==4.19.2
gradio==4.21.0
opencv-python==4.8.1.78
onnx==1.15.0
insightface==0.7.3
Expand All @@ -13,7 +13,7 @@ torchvision==0.16.2; sys_platform == 'darwin'
onnxruntime==1.16.3; sys_platform == 'darwin' and platform_machine != 'arm64'
onnxruntime-silicon==1.13.1; sys_platform == 'darwin' and platform_machine == 'arm64'
onnxruntime-gpu==1.16.3; sys_platform != 'darwin'
tqdm==4.66.1
tqdm==4.66.2
ftfy
regex
pyvirtualcam
2 changes: 0 additions & 2 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def load(self):
self.output_video_codec = self.default_get(data, 'output_video_codec', 'libx264')
self.video_quality = self.default_get(data, 'video_quality', 14)
self.clear_output = self.default_get(data, 'clear_output', True)
self.live_cam_start_active = self.default_get(data, 'live_cam_start_active', False)
self.max_threads = self.default_get(data, 'max_threads', 2)
self.memory_limit = self.default_get(data, 'memory_limit', 0)
self.provider = self.default_get(data, 'provider', 'cuda')
Expand All @@ -53,7 +52,6 @@ def save(self):
'output_video_codec' : self.output_video_codec,
'video_quality' : self.video_quality,
'clear_output' : self.clear_output,
'live_cam_start_active' : self.live_cam_start_active,
'max_threads' : self.max_threads,
'memory_limit' : self.memory_limit,
'provider' : self.provider,
Expand Down
2 changes: 0 additions & 2 deletions ui/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
ui_blend_ratio = None
ui_input_thumbs = []
ui_target_thumbs = []

ui_live_cam_active = False
ui_camera_frame = None


Expand Down
1 change: 0 additions & 1 deletion ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def run():
overflow-y: auto !important;
}
"""
uii.ui_live_cam_active = roop.globals.CFG.live_cam_start_active

while run_server:
server_name = roop.globals.CFG.server_name
Expand Down
34 changes: 19 additions & 15 deletions ui/tabs/faceswap_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def faceswap_tab():

with gr.Column(scale=2):
previewimage = gr.Image(label="Preview Image", height=576, interactive=False, visible=True)
maskimage = gr.ImageEditor(label="Manual mask Image", sources=["clipboard"], transforms="",
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'):
fake_preview = gr.Checkbox(label="Face swap frames", value=False)
Expand Down Expand Up @@ -126,8 +126,8 @@ def faceswap_tab():
resultvideo = gr.Video(label='Final Video', interactive=False, visible=False)

previewinputs = [preview_frame_num, bt_destfiles, fake_preview, ui.globals.ui_selected_enhancer, selected_face_detection,
max_face_distance, ui.globals.ui_blend_ratio, chk_useclip, clip_text, no_face_action, vr_mode, autorotate, previewimage, maskimage]
previewoutputs = [preview_frame_num,previewimage, maskimage, mask_top, mask_bottom, mask_left, mask_right]
max_face_distance, ui.globals.ui_blend_ratio, chk_useclip, clip_text, no_face_action, vr_mode, autorotate, maskimage]
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)
bt_remove_selected_input_face.click(fn=remove_selected_input_face, outputs=[input_faces])
bt_srcfiles.change(fn=on_srcfile_changed, show_progress='full', inputs=bt_srcfiles, outputs=[dynamic_face_selection, face_selection, input_faces])
Expand Down Expand Up @@ -168,7 +168,7 @@ def faceswap_tab():
bt_refresh_preview.click(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
bt_toggle_masking.click(fn=on_toggle_masking, inputs=[previewimage, maskimage], outputs=[previewimage, maskimage])
fake_preview.change(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
preview_frame_num.change(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden')
preview_frame_num.release(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden', )
bt_use_face_from_preview.click(fn=on_use_face_from_selected, show_progress='full', inputs=[bt_destfiles, preview_frame_num], outputs=[dynamic_face_selection, face_selection, target_faces, selected_face_detection])
set_frame_start.click(fn=on_set_frame, inputs=[set_frame_start, preview_frame_num], outputs=[text_frame_clip])
set_frame_end.click(fn=on_set_frame, inputs=[set_frame_end, preview_frame_num], outputs=[text_frame_clip])
Expand Down Expand Up @@ -373,8 +373,8 @@ def on_end_face_selection():


def on_preview_frame_changed(frame_num, files, fake_preview, enhancer, detection, face_distance, blend_ratio,
use_clip, clip_text, no_face_action, vr_mode, auto_rotate, previewimage, maskimage):
global SELECTED_INPUT_FACE_INDEX, is_processing, manual_masking
use_clip, clip_text, no_face_action, vr_mode, auto_rotate, maskimage):
global SELECTED_INPUT_FACE_INDEX, manual_masking, current_video_fps

from roop.core import live_swap

Expand All @@ -386,29 +386,33 @@ def on_preview_frame_changed(frame_num, files, fake_preview, enhancer, detection
mask_offsets = roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets

timeinfo = '0:00:00'
if is_processing or files is None or selected_preview_index >= len(files) or frame_num is None:
return gr.Slider(info=timeinfo),None,gr.ImageEditor(visible=False), mask_offsets[0], mask_offsets[1], mask_offsets[2], mask_offsets[3]
if files is None or selected_preview_index >= len(files) or frame_num is None:
return None,None, gr.Slider(info=timeinfo)

filename = files[selected_preview_index].name
# time.sleep(0.3)
if util.is_video(filename) or filename.lower().endswith('gif'):
current_frame = get_video_frame(filename, frame_num)
secs = frame_num / current_video_fps
if current_video_fps == 0:
current_video_fps = 1
secs = (frame_num - 1) / current_video_fps
minutes = secs / 60
secs = secs % 60
hours = minutes / 60
timeinfo = f"{int(hours):0>2}:{int(minutes):0>2}:{int(secs):0>2}"
minutes = minutes % 60
milliseconds = (secs - int(secs)) * 1000
timeinfo = f"{int(hours):0>2}:{int(minutes):0>2}:{int(secs):0>2}.{int(milliseconds):0>3}"
else:
current_frame = get_image_frame(filename)
if current_frame is None:
return gr.Slider(info=timeinfo), gr.Image(visible=True), gr.ImageEditor(visible=False), mask_offsets[0], mask_offsets[1], mask_offsets[2], mask_offsets[3]
return None, None, gr.Slider(info=timeinfo)

layers = None
if maskimage is not None:
layers = maskimage["layers"]

if not fake_preview or len(roop.globals.INPUT_FACESETS) < 1:
return gr.Slider(info=timeinfo), gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), mask_offsets[0], mask_offsets[1], mask_offsets[2], mask_offsets[3]
return gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), gr.Slider(info=timeinfo)

roop.globals.face_swap_mode = translate_swap_mode(detection)
roop.globals.selected_enhancer = enhancer
Expand All @@ -425,8 +429,8 @@ def on_preview_frame_changed(frame_num, files, fake_preview, enhancer, detection
mask = layers[0] if layers is not None else None
current_frame = live_swap(current_frame, roop.globals.face_swap_mode, use_clip, clip_text, maskimage, SELECTED_INPUT_FACE_INDEX)
if current_frame is None:
return gr.Slider(info=timeinfo), gr.Image(visible=True), gr.ImageEditor(visible=False), mask_offsets[0], mask_offsets[1], mask_offsets[2], mask_offsets[3]
return gr.Slider(info=timeinfo), gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), mask_offsets[0], mask_offsets[1], mask_offsets[2], mask_offsets[3]
return gr.Image(visible=True), None, gr.Slider(info=timeinfo)
return gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), gr.Slider(info=timeinfo)

def on_toggle_masking(previewimage, mask):
global manual_masking
Expand Down Expand Up @@ -464,7 +468,7 @@ def on_preview_mask(frame_num, files, clip_text):
from roop.core import preview_mask
global is_processing

if is_processing:
if is_processing or files is None or selected_preview_index >= len(files) or clip_text is None or frame_num is None:
return None

filename = files[selected_preview_index].name
Expand Down
6 changes: 2 additions & 4 deletions ui/tabs/livecam_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ def start_cam(stream_to_obs, cam, reso, enhancer, blend_ratio):
from roop.virtualcam import start_virtual_cam
from roop.utilities import convert_to_gradio

ui.globals.ui_live_cam_active = True
start_virtual_cam(stream_to_obs, cam, reso)
roop.globals.selected_enhancer = enhancer
roop.globals.blend_ratio = blend_ratio

while ui.globals.ui_live_cam_active:
while True:
yield gr.Button(interactive=False), gr.Button(interactive=True), convert_to_gradio(ui.globals.ui_camera_frame)
return gr.Button(interactive=True), gr.Button(interactive=False), convert_to_gradio(ui.globals.ui_camera_frame)

def stop_swap():
from roop.virtualcam import stop_virtual_cam
Expand Down
1 change: 0 additions & 1 deletion ui/tabs/settings_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def settings_tab():
with gr.Column():
button_apply_restart = gr.Button("Restart Server", variant='primary')
with gr.Group():
settings_controls.append(gr.Checkbox(label='Start with active live cam', value=roop.globals.CFG.live_cam_start_active, elem_id='live_cam_start_active', interactive=True))
settings_controls.append(gr.Checkbox(label='Use OS temp folder', value=roop.globals.CFG.use_os_temp_folder, elem_id='use_os_temp_folder', interactive=True))
button_clean_temp = gr.Button("Clean temp folder")
button_apply_settings = gr.Button("Apply Settings")
Expand Down

0 comments on commit 64e2275

Please sign in to comment.