msdhon393 commited on
Commit
2bc7c30
·
verified ·
1 Parent(s): b00ac90

Upload 5 files

Browse files
ui/tabs/extras_tab.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import roop.utilities as util
4
+ import roop.util_ffmpeg as ffmpeg
5
+ import roop.globals
6
+
7
+ def extras_tab():
8
+ with gr.Tab("🎉 Extras"):
9
+ with gr.Row():
10
+ files_to_process = gr.Files(label='File(s) to process', file_count="multiple", file_types=["image", "video"])
11
+ # with gr.Row(variant='panel'):
12
+ # with gr.Accordion(label="Post process", open=False):
13
+ # with gr.Column():
14
+ # selected_post_enhancer = gr.Dropdown(["None", "Codeformer", "GFPGAN"], value="None", label="Select post-processing")
15
+ # with gr.Column():
16
+ # gr.Button("Start").click(fn=lambda: gr.Info('Not yet implemented...'))
17
+ with gr.Row(variant='panel'):
18
+ with gr.Accordion(label="Video/GIF", open=False):
19
+ with gr.Row(variant='panel'):
20
+ with gr.Column():
21
+ gr.Markdown("""
22
+ # Poor man's video editor
23
+ Re-encoding uses your configuration from the Settings Tab.
24
+ """)
25
+ with gr.Column():
26
+ cut_start_time = gr.Slider(0, 1000000, value=0, label="Start Frame", step=1.0, interactive=True)
27
+ with gr.Column():
28
+ cut_end_time = gr.Slider(1, 1000000, value=1, label="End Frame", step=1.0, interactive=True)
29
+ with gr.Column():
30
+ extras_chk_encode = gr.Checkbox(label='Re-encode videos (necessary for videos with different codecs)', value=False)
31
+ start_cut_video = gr.Button("Cut video")
32
+ start_extract_frames = gr.Button("Extract frames")
33
+ start_join_videos = gr.Button("Join videos")
34
+
35
+ with gr.Row(variant='panel'):
36
+ with gr.Column():
37
+ gr.Markdown("""
38
+ # Create video/gif from images
39
+ """)
40
+ with gr.Column():
41
+ extras_fps = gr.Slider(minimum=0, maximum=120, value=30, label="Video FPS", step=1.0, interactive=True)
42
+ extras_images_folder = gr.Textbox(show_label=False, placeholder="/content/", interactive=True)
43
+ with gr.Column():
44
+ extras_chk_creategif = gr.Checkbox(label='Create GIF from video', value=False)
45
+ extras_create_video=gr.Button("Create")
46
+ with gr.Row():
47
+ gr.Button("👀 Open Output Folder", size='sm').click(fn=lambda: util.open_folder(roop.globals.output_path))
48
+ with gr.Row():
49
+ extra_files_output = gr.Files(label='Resulting output files', file_count="multiple")
50
+
51
+ start_cut_video.click(fn=on_cut_video, inputs=[files_to_process, cut_start_time, cut_end_time, extras_chk_encode], outputs=[extra_files_output])
52
+ start_extract_frames.click(fn=on_extras_extract_frames, inputs=[files_to_process], outputs=[extra_files_output])
53
+ start_join_videos.click(fn=on_join_videos, inputs=[files_to_process, extras_chk_encode], outputs=[extra_files_output])
54
+ extras_create_video.click(fn=on_extras_create_video, inputs=[extras_images_folder, extras_fps, extras_chk_creategif], outputs=[extra_files_output])
55
+
56
+
57
+ def on_cut_video(files, cut_start_frame, cut_end_frame, reencode):
58
+ if files is None:
59
+ return None
60
+
61
+ resultfiles = []
62
+ for tf in files:
63
+ f = tf.name
64
+ destfile = util.get_destfilename_from_path(f, roop.globals.output_path, '_cut')
65
+ ffmpeg.cut_video(f, destfile, cut_start_frame, cut_end_frame, reencode)
66
+ if os.path.isfile(destfile):
67
+ resultfiles.append(destfile)
68
+ else:
69
+ gr.Error('Cutting video failed!')
70
+ return resultfiles
71
+
72
+
73
+ def on_join_videos(files, chk_encode):
74
+ if files is None:
75
+ return None
76
+
77
+ filenames = []
78
+ for f in files:
79
+ filenames.append(f.name)
80
+ destfile = util.get_destfilename_from_path(filenames[0], roop.globals.output_path, '_join')
81
+ sorted_filenames = util.sort_filenames_ignore_path(filenames)
82
+ ffmpeg.join_videos(sorted_filenames, destfile, not chk_encode)
83
+ resultfiles = []
84
+ if os.path.isfile(destfile):
85
+ resultfiles.append(destfile)
86
+ else:
87
+ gr.Error('Joining videos failed!')
88
+ return resultfiles
89
+
90
+
91
+
92
+ def on_extras_create_video(images_path,fps, create_gif):
93
+ util.sort_rename_frames(os.path.dirname(images_path))
94
+ destfilename = os.path.join(roop.globals.output_path, "img2video." + roop.globals.CFG.output_video_format)
95
+ ffmpeg.create_video('', destfilename, fps, images_path)
96
+ resultfiles = []
97
+ if os.path.isfile(destfilename):
98
+ resultfiles.append(destfilename)
99
+ else:
100
+ return None
101
+ if create_gif:
102
+ gifname = util.get_destfilename_from_path(destfilename, './output', '.gif')
103
+ ffmpeg.create_gif_from_video(destfilename, gifname)
104
+ if os.path.isfile(destfilename):
105
+ resultfiles.append(gifname)
106
+ return resultfiles
107
+
108
+
109
+ def on_extras_extract_frames(files):
110
+ if files is None:
111
+ return None
112
+
113
+ resultfiles = []
114
+ for tf in files:
115
+ f = tf.name
116
+ resfolder = ffmpeg.extract_frames(f)
117
+ for file in os.listdir(resfolder):
118
+ outfile = os.path.join(resfolder, file)
119
+ if os.path.isfile(outfile):
120
+ resultfiles.append(outfile)
121
+ return resultfiles
122
+
ui/tabs/facemgr_tab.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import cv2
4
+ import gradio as gr
5
+ import roop.utilities as util
6
+ import roop.globals
7
+ from roop.face_util import extract_face_images
8
+ from roop.capturer import get_video_frame, get_video_frame_total
9
+ from typing import List, Tuple, Optional
10
+ from roop.typing import Frame, Face, FaceSet
11
+
12
+ selected_face_index = -1
13
+ thumbs = []
14
+ images = []
15
+
16
+
17
+ def facemgr_tab() -> None:
18
+ with gr.Tab("👨‍👩‍👧‍👦 Face Management"):
19
+ with gr.Row():
20
+ gr.Markdown("""
21
+ # Create blending facesets
22
+ Add multiple reference images into a faceset file.
23
+ """)
24
+ with gr.Row():
25
+ videoimagefst = gr.Image(label="Cut face from video frame", height=576, interactive=False, visible=True)
26
+ with gr.Row():
27
+ frame_num_fst = gr.Slider(1, 1, value=1, label="Frame Number", info='0:00:00', step=1.0, interactive=False)
28
+ fb_cutfromframe = gr.Button("Use faces from this frame", variant='secondary', interactive=False)
29
+ with gr.Row():
30
+ fb_facesetfile = gr.Files(label='Faceset', file_count='single', file_types=['.fsz'], interactive=True)
31
+ fb_files = gr.Files(label='Input Files', file_count="multiple", file_types=["image", "video"], interactive=True)
32
+ with gr.Row():
33
+ with gr.Column():
34
+ gr.Button("👀 Open Output Folder", size='sm').click(fn=lambda: util.open_folder(roop.globals.output_path))
35
+ with gr.Column():
36
+ gr.Markdown(' ')
37
+ with gr.Row():
38
+ faces = gr.Gallery(label="Faces in this Faceset", allow_preview=True, preview=True, height=128, object_fit="scale-down")
39
+ with gr.Row():
40
+ fb_remove = gr.Button("Remove selected", variant='secondary')
41
+ fb_update = gr.Button("Create/Update Faceset file", variant='primary')
42
+ fb_clear = gr.Button("Clear all", variant='stop')
43
+
44
+ fb_facesetfile.change(fn=on_faceset_changed, inputs=[fb_facesetfile], outputs=[faces])
45
+ fb_files.change(fn=on_fb_files_changed, inputs=[fb_files], outputs=[faces, videoimagefst, frame_num_fst, fb_cutfromframe])
46
+ fb_update.click(fn=on_update_clicked, outputs=[fb_facesetfile])
47
+ fb_remove.click(fn=on_remove_clicked, outputs=[faces])
48
+ fb_clear.click(fn=on_clear_clicked, outputs=[faces, fb_files, fb_facesetfile])
49
+ fb_cutfromframe.click(fn=on_cutfromframe_clicked, inputs=[fb_files, frame_num_fst], outputs=[faces])
50
+ frame_num_fst.release(fn=on_frame_num_fst_changed, inputs=[fb_files, frame_num_fst], outputs=[videoimagefst])
51
+ faces.select(fn=on_face_selected)
52
+
53
+
54
+ def on_faceset_changed(faceset, progress=gr.Progress()) -> List[Frame]:
55
+ global thumbs, images
56
+
57
+ if faceset is None:
58
+ return thumbs
59
+
60
+ thumbs.clear()
61
+ filename = faceset.name
62
+
63
+ if filename.lower().endswith('fsz'):
64
+ progress(0, desc="Retrieving faces from Faceset File", )
65
+ unzipfolder = os.path.join(os.environ["TEMP"], 'faceset')
66
+ if os.path.isdir(unzipfolder):
67
+ shutil.rmtree(unzipfolder)
68
+ util.mkdir_with_umask(unzipfolder)
69
+ util.unzip(filename, unzipfolder)
70
+ for file in os.listdir(unzipfolder):
71
+ if file.endswith(".png"):
72
+ SELECTION_FACES_DATA = extract_face_images(os.path.join(unzipfolder,file), (False, 0), 0.5)
73
+ if len(SELECTION_FACES_DATA) < 1:
74
+ gr.Warning(f"No face detected in {file}!")
75
+ for f in SELECTION_FACES_DATA:
76
+ image = f[1]
77
+ images.append(image)
78
+ thumbs.append(util.convert_to_gradio(image))
79
+
80
+ return thumbs
81
+
82
+
83
+ def on_fb_files_changed(inputfiles, progress=gr.Progress()) -> Tuple[List[Frame], Optional[gr.Image], Optional[gr.Slider], Optional[gr.Button]]:
84
+ global thumbs, images, total_frames, current_video_fps
85
+
86
+ if inputfiles is None or len(inputfiles) < 1:
87
+ return thumbs, None, None, None
88
+
89
+ progress(0, desc="Retrieving faces from images", )
90
+ slider = None
91
+ video_image = None
92
+ cut_button = None
93
+ for f in inputfiles:
94
+ source_path = f.name
95
+ if util.has_image_extension(source_path):
96
+ slider = gr.Slider(interactive=False)
97
+ video_image = gr.Image(interactive=False)
98
+ cut_button = gr.Button(interactive=False)
99
+ roop.globals.source_path = source_path
100
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.source_path, (False, 0), 0.5)
101
+ for f in SELECTION_FACES_DATA:
102
+ image = f[1]
103
+ images.append(image)
104
+ thumbs.append(util.convert_to_gradio(image))
105
+ elif util.is_video(source_path) or source_path.lower().endswith('gif'):
106
+ total_frames = get_video_frame_total(source_path)
107
+ current_video_fps = util.detect_fps(source_path)
108
+ cut_button = gr.Button(interactive=True)
109
+ video_image, slider = display_video_frame(source_path, 1, total_frames)
110
+
111
+ return thumbs, video_image, slider, cut_button
112
+
113
+
114
+ def display_video_frame(filename: str, frame_num: int, total: int=0) -> Tuple[gr.Image, gr.Slider]:
115
+ global current_video_fps
116
+
117
+ current_frame = get_video_frame(filename, frame_num)
118
+ if current_video_fps == 0:
119
+ current_video_fps = 1
120
+ secs = (frame_num - 1) / current_video_fps
121
+ minutes = secs / 60
122
+ secs = secs % 60
123
+ hours = minutes / 60
124
+ minutes = minutes % 60
125
+ milliseconds = (secs - int(secs)) * 1000
126
+ timeinfo = f"{int(hours):0>2}:{int(minutes):0>2}:{int(secs):0>2}.{int(milliseconds):0>3}"
127
+ if total > 0:
128
+ return gr.Image(value=util.convert_to_gradio(current_frame), interactive=True), gr.Slider(info=timeinfo, minimum=1, maximum=total, interactive=True)
129
+ return gr.Image(value=util.convert_to_gradio(current_frame), interactive=True), gr.Slider(info=timeinfo, interactive=True)
130
+
131
+
132
+ def on_face_selected(evt: gr.SelectData) -> None:
133
+ global selected_face_index
134
+
135
+ if evt is not None:
136
+ selected_face_index = evt.index
137
+
138
+ def on_frame_num_fst_changed(inputfiles: List[gr.Files], frame_num: int) -> Frame:
139
+ filename = inputfiles[0].name
140
+ video_image, _ = display_video_frame(filename, frame_num, 0)
141
+ return video_image
142
+
143
+
144
+ def on_cutfromframe_clicked(inputfiles: List[gr.Files], frame_num: int) -> List[Frame]:
145
+ global thumbs
146
+
147
+ filename = inputfiles[0].name
148
+ SELECTION_FACES_DATA = extract_face_images(filename, (True, frame_num), 0.5)
149
+ for f in SELECTION_FACES_DATA:
150
+ image = f[1]
151
+ images.append(image)
152
+ thumbs.append(util.convert_to_gradio(image))
153
+ return thumbs
154
+
155
+
156
+ def on_remove_clicked() -> List[Frame]:
157
+ global thumbs, images, selected_face_index
158
+
159
+ if len(thumbs) > selected_face_index:
160
+ f = thumbs.pop(selected_face_index)
161
+ del f
162
+ f = images.pop(selected_face_index)
163
+ del f
164
+ return thumbs
165
+
166
+ def on_clear_clicked() -> Tuple[List[Frame], None, None]:
167
+ global thumbs, images
168
+
169
+ thumbs.clear()
170
+ images.clear()
171
+ return thumbs, None, None
172
+
173
+
174
+ def on_update_clicked() -> Optional[str]:
175
+ if len(images) < 1:
176
+ gr.Warning(f"No faces to create faceset from!")
177
+ return None
178
+
179
+ imgnames = []
180
+ for index,img in enumerate(images):
181
+ filename = os.path.join(roop.globals.output_path, f'{index}.png')
182
+ cv2.imwrite(filename, img)
183
+ imgnames.append(filename)
184
+
185
+ finalzip = os.path.join(roop.globals.output_path, 'faceset.fsz')
186
+ util.zip(imgnames, finalzip)
187
+ return finalzip
ui/tabs/faceswap_tab.py ADDED
@@ -0,0 +1,717 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import pathlib
4
+ import gradio as gr
5
+ import roop.utilities as util
6
+ import roop.globals
7
+ import ui.globals
8
+ from roop.face_util import extract_face_images, create_blank_image
9
+ from roop.capturer import get_video_frame, get_video_frame_total, get_image_frame
10
+ from roop.ProcessEntry import ProcessEntry
11
+ from roop.ProcessOptions import ProcessOptions
12
+ from roop.FaceSet import FaceSet
13
+
14
+ last_image = None
15
+
16
+
17
+ IS_INPUT = True
18
+ SELECTED_FACE_INDEX = 0
19
+
20
+ SELECTED_INPUT_FACE_INDEX = 0
21
+ SELECTED_TARGET_FACE_INDEX = 0
22
+
23
+ input_faces = None
24
+ target_faces = None
25
+ face_selection = None
26
+ previewimage = None
27
+
28
+ selected_preview_index = 0
29
+
30
+ is_processing = False
31
+
32
+ list_files_process : list[ProcessEntry] = []
33
+ no_face_choices = ["Use untouched original frame","Retry rotated", "Skip Frame"]
34
+
35
+ current_video_fps = 50
36
+
37
+ manual_masking = False
38
+
39
+
40
+ def faceswap_tab():
41
+ global no_face_choices, previewimage
42
+
43
+ with gr.Tab("🎭 Face Swap"):
44
+ with gr.Row(variant='panel'):
45
+ with gr.Column(scale=2):
46
+ with gr.Row():
47
+ with gr.Column(min_width=160):
48
+ input_faces = gr.Gallery(label="Input faces", allow_preview=False, preview=False, height=128, object_fit="scale-down", columns=8)
49
+ with gr.Accordion(label="Advanced Masking", open=False):
50
+ chk_showmaskoffsets = gr.Checkbox(label="Show mask overlay in preview", value=False, interactive=True)
51
+ mask_top = gr.Slider(0, 1.0, value=0, label="Offset Face Top", step=0.01, interactive=True)
52
+ mask_bottom = gr.Slider(0, 1.0, value=0, label="Offset Face Bottom", step=0.01, interactive=True)
53
+ mask_left = gr.Slider(0, 1.0, value=0, label="Offset Face Left", step=0.01, interactive=True)
54
+ mask_right = gr.Slider(0, 1.0, value=0, label="Offset Face Right", step=0.01, interactive=True)
55
+ mask_erosion = gr.Slider(1.0, 3.0, value=1.0, label="Erosion Iterations", step=1.00, interactive=True)
56
+ mask_blur = gr.Slider(10.0, 50.0, value=20.0, label="Blur size", step=1.00, interactive=True)
57
+ bt_toggle_masking = gr.Button("Toggle manual masking", variant='secondary', size='sm')
58
+ selected_mask_engine = gr.Dropdown(["None", "Clip2Seg", "DFL XSeg"], value="None", label="Face masking engine")
59
+ clip_text = gr.Textbox(label="List of objects to mask and restore back on fake face", value="cup,hands,hair,banana", interactive=False)
60
+ bt_preview_mask = gr.Button("👥 Show Mask Preview", variant='secondary')
61
+ bt_remove_selected_input_face = gr.Button("❌ Remove selected", size='sm')
62
+ bt_clear_input_faces = gr.Button("💥 Clear all", variant='stop', size='sm')
63
+ with gr.Column(min_width=160):
64
+ target_faces = gr.Gallery(label="Target faces", allow_preview=False, preview=False, height=128, object_fit="scale-down", columns=8)
65
+ bt_remove_selected_target_face = gr.Button("❌ Remove selected", size='sm')
66
+ bt_add_local = gr.Button('Add local files from', size='sm')
67
+ local_folder = gr.Textbox(show_label=False, placeholder="/content/", interactive=True)
68
+ with gr.Row(variant='panel'):
69
+ bt_srcfiles = gr.Files(label='Source File(s)', file_count="multiple", file_types=["image", ".fsz"], elem_id='filelist', height=233)
70
+ bt_destfiles = gr.Files(label='Target File(s)', file_count="multiple", file_types=["image", "video"], elem_id='filelist', height=233)
71
+ with gr.Row(variant='panel'):
72
+ gr.Markdown('')
73
+ 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)
74
+
75
+ with gr.Column(scale=2):
76
+ previewimage = gr.Image(label="Preview Image", height=576, interactive=False, visible=True)
77
+ maskimage = gr.ImageEditor(label="Manual mask Image", sources=["clipboard"], transforms="", type="numpy",
78
+ brush=gr.Brush(color_mode="fixed", colors=["rgba(255, 255, 255, 1"]), interactive=True, visible=False)
79
+ with gr.Row(variant='panel'):
80
+ fake_preview = gr.Checkbox(label="Face swap frames", value=False)
81
+ bt_refresh_preview = gr.Button("🔄 Refresh", variant='secondary', size='sm')
82
+ bt_use_face_from_preview = gr.Button("Use Face from this Frame", variant='primary', size='sm')
83
+ with gr.Row():
84
+ preview_frame_num = gr.Slider(1, 1, value=1, label="Frame Number", info='0:00:00', step=1.0, interactive=True)
85
+ with gr.Row():
86
+ text_frame_clip = gr.Markdown('Processing frame range [0 - 0]')
87
+ set_frame_start = gr.Button("⬅ Set as Start", size='sm')
88
+ set_frame_end = gr.Button("➡ Set as End", size='sm')
89
+ with gr.Row(visible=False) as dynamic_face_selection:
90
+ with gr.Column(scale=2):
91
+ face_selection = gr.Gallery(label="Detected faces", allow_preview=False, preview=False, height=256, object_fit="cover", columns=8)
92
+ with gr.Column():
93
+ bt_faceselect = gr.Button("☑ Use selected face", size='sm')
94
+ bt_cancelfaceselect = gr.Button("Done", size='sm')
95
+ with gr.Column():
96
+ gr.Markdown(' ')
97
+
98
+ with gr.Row(variant='panel'):
99
+ with gr.Column(scale=1):
100
+ selected_face_detection = gr.Dropdown(["First found", "All female", "All male", "All faces", "Selected face"], value="First found", label="Specify face selection for swapping")
101
+ with gr.Column(scale=1):
102
+ ui.globals.ui_selected_enhancer = gr.Dropdown(["None", "Codeformer", "DMDNet", "GFPGAN", "GPEN", "Restoreformer++"], value="None", label="Select post-processing")
103
+
104
+ with gr.Row(variant='panel'):
105
+ with gr.Column(scale=1):
106
+ max_face_distance = gr.Slider(0.01, 1.0, value=0.65, label="Max Face Similarity Threshold", info="0.0 = identical 1.0 = no similarity")
107
+ with gr.Column(scale=1):
108
+ num_swap_steps = gr.Slider(1, 5, value=1, step=1.0, label="Number of swapping steps", info="More steps can increase likeness")
109
+ with gr.Column(scale=2):
110
+ ui.globals.ui_blend_ratio = gr.Slider(0.0, 1.0, value=0.65, label="Original/Enhanced image blend ratio", info="Only used with active post-processing")
111
+
112
+
113
+ with gr.Row(variant='panel'):
114
+ with gr.Column(scale=1):
115
+ video_swapping_method = gr.Dropdown(["Extract Frames to media","In-Memory processing"], value="In-Memory processing", label="Select video processing method", interactive=True)
116
+ no_face_action = gr.Dropdown(choices=no_face_choices, value=no_face_choices[0], label="Action on no face detected", interactive=True)
117
+ vr_mode = gr.Checkbox(label="VR Mode", value=False)
118
+ with gr.Column(scale=1):
119
+ with gr.Group():
120
+ autorotate = gr.Checkbox(label="Auto rotate horizontal Faces", value=True)
121
+ roop.globals.skip_audio = gr.Checkbox(label="Skip audio", value=False)
122
+ roop.globals.keep_frames = gr.Checkbox(label="Keep Frames (relevant only when extracting frames)", value=False)
123
+ roop.globals.wait_after_extraction = gr.Checkbox(label="Wait for user key press before creating video ", value=False)
124
+
125
+
126
+
127
+ with gr.Row(variant='panel'):
128
+ with gr.Column():
129
+ bt_start = gr.Button("▶ Start", variant='primary')
130
+ gr.Button("👀 Open Output Folder", size='sm').click(fn=lambda: util.open_folder(roop.globals.output_path))
131
+ with gr.Column():
132
+ bt_stop = gr.Button("⏹ Stop", variant='secondary', interactive=False)
133
+ with gr.Column(scale=2):
134
+ gr.Markdown(' ')
135
+ with gr.Row(variant='panel'):
136
+ with gr.Column():
137
+ resultfiles = gr.Files(label='Processed File(s)', interactive=False)
138
+ with gr.Column():
139
+ resultimage = gr.Image(type='filepath', label='Final Image', interactive=False )
140
+ resultvideo = gr.Video(label='Final Video', interactive=False, visible=False)
141
+
142
+ previewinputs = [preview_frame_num, bt_destfiles, fake_preview, ui.globals.ui_selected_enhancer, selected_face_detection,
143
+ max_face_distance, ui.globals.ui_blend_ratio, selected_mask_engine, clip_text, no_face_action, vr_mode, autorotate, maskimage, chk_showmaskoffsets, num_swap_steps]
144
+ previewoutputs = [previewimage, maskimage, preview_frame_num]
145
+ input_faces.select(on_select_input_face, None, None).then(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
146
+ bt_remove_selected_input_face.click(fn=remove_selected_input_face, outputs=[input_faces])
147
+ bt_srcfiles.change(fn=on_srcfile_changed, show_progress='full', inputs=bt_srcfiles, outputs=[dynamic_face_selection, face_selection, input_faces])
148
+
149
+ mask_top.release(fn=on_mask_top_changed, inputs=[mask_top], show_progress='hidden')
150
+ mask_bottom.release(fn=on_mask_bottom_changed, inputs=[mask_bottom], show_progress='hidden')
151
+ mask_left.release(fn=on_mask_left_changed, inputs=[mask_left], show_progress='hidden')
152
+ mask_right.release(fn=on_mask_right_changed, inputs=[mask_right], show_progress='hidden')
153
+ mask_erosion.release(fn=on_mask_erosion_changed, inputs=[mask_erosion], show_progress='hidden')
154
+ mask_blur.release(fn=on_mask_blur_changed, inputs=[mask_blur], show_progress='hidden')
155
+ selected_mask_engine.change(fn=on_mask_engine_changed, inputs=[selected_mask_engine], outputs=[clip_text], show_progress='hidden')
156
+
157
+
158
+ target_faces.select(on_select_target_face, None, None)
159
+ bt_remove_selected_target_face.click(fn=remove_selected_target_face, outputs=[target_faces])
160
+
161
+ forced_fps.change(fn=on_fps_changed, inputs=[forced_fps], show_progress='hidden')
162
+ 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')
163
+ 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')
164
+ bt_destfiles.clear(fn=on_clear_destfiles, outputs=[target_faces, selected_face_detection])
165
+ resultfiles.select(fn=on_resultfiles_selected, inputs=[resultfiles], outputs=[resultimage, resultvideo])
166
+
167
+ face_selection.select(on_select_face, None, None)
168
+ bt_faceselect.click(fn=on_selected_face, outputs=[input_faces, target_faces, selected_face_detection])
169
+ bt_cancelfaceselect.click(fn=on_end_face_selection, outputs=[dynamic_face_selection, face_selection])
170
+
171
+ bt_clear_input_faces.click(fn=on_clear_input_faces, outputs=[input_faces])
172
+
173
+
174
+ bt_add_local.click(fn=on_add_local_folder, inputs=[local_folder], outputs=[bt_destfiles])
175
+ bt_preview_mask.click(fn=on_preview_mask, inputs=[preview_frame_num, bt_destfiles, clip_text, selected_mask_engine], outputs=[previewimage])
176
+
177
+ start_event = bt_start.click(fn=start_swap,
178
+ inputs=[ui.globals.ui_selected_enhancer, selected_face_detection, roop.globals.keep_frames, roop.globals.wait_after_extraction,
179
+ 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, num_swap_steps, maskimage],
180
+ outputs=[bt_start, bt_stop, resultfiles], show_progress='full')
181
+ after_swap_event = start_event.then(fn=on_resultfiles_finished, inputs=[resultfiles], outputs=[resultimage, resultvideo])
182
+
183
+ bt_stop.click(fn=stop_swap, cancels=[start_event, after_swap_event], outputs=[bt_start, bt_stop], queue=False)
184
+
185
+ bt_refresh_preview.click(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
186
+ bt_toggle_masking.click(fn=on_toggle_masking, inputs=[previewimage, maskimage], outputs=[previewimage, maskimage])
187
+ fake_preview.change(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
188
+ preview_frame_num.release(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden', )
189
+ 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])
190
+ set_frame_start.click(fn=on_set_frame, inputs=[set_frame_start, preview_frame_num], outputs=[text_frame_clip])
191
+ set_frame_end.click(fn=on_set_frame, inputs=[set_frame_end, preview_frame_num], outputs=[text_frame_clip])
192
+
193
+
194
+
195
+ def on_mask_top_changed(mask_offset):
196
+ set_mask_offset(0, mask_offset)
197
+
198
+ def on_mask_bottom_changed(mask_offset):
199
+ set_mask_offset(1, mask_offset)
200
+
201
+ def on_mask_left_changed(mask_offset):
202
+ set_mask_offset(2, mask_offset)
203
+
204
+ def on_mask_right_changed(mask_offset):
205
+ set_mask_offset(3, mask_offset)
206
+
207
+ def on_mask_erosion_changed(mask_offset):
208
+ set_mask_offset(4, mask_offset)
209
+ def on_mask_blur_changed(mask_offset):
210
+ set_mask_offset(5, mask_offset)
211
+
212
+
213
+ def set_mask_offset(index, mask_offset):
214
+ global SELECTED_INPUT_FACE_INDEX
215
+
216
+ if len(roop.globals.INPUT_FACESETS) > SELECTED_INPUT_FACE_INDEX:
217
+ offs = roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets
218
+ offs[index] = mask_offset
219
+ if offs[0] + offs[1] > 0.99:
220
+ offs[0] = 0.99
221
+ offs[1] = 0.0
222
+ if offs[2] + offs[3] > 0.99:
223
+ offs[2] = 0.99
224
+ offs[3] = 0.0
225
+ roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets = offs
226
+
227
+ def on_mask_engine_changed(mask_engine):
228
+ if mask_engine == "Clip2Seg":
229
+ return gr.Textbox(interactive=True)
230
+ return gr.Textbox(interactive=False)
231
+
232
+
233
+
234
+ def on_add_local_folder(folder):
235
+ files = util.get_local_files_from_folder(folder)
236
+ if files is None:
237
+ gr.Warning("Empty folder or folder not found!")
238
+ return files
239
+
240
+
241
+ def on_srcfile_changed(srcfiles, progress=gr.Progress()):
242
+ global SELECTION_FACES_DATA, IS_INPUT, input_faces, face_selection, last_image
243
+
244
+ IS_INPUT = True
245
+
246
+ if srcfiles is None or len(srcfiles) < 1:
247
+ return gr.Column(visible=False), None, ui.globals.ui_input_thumbs
248
+
249
+ thumbs = []
250
+ for f in srcfiles:
251
+ source_path = f.name
252
+ if source_path.lower().endswith('fsz'):
253
+ progress(0, desc="Retrieving faces from Faceset File")
254
+ unzipfolder = os.path.join(os.environ["TEMP"], 'faceset')
255
+ if os.path.isdir(unzipfolder):
256
+ files = os.listdir(unzipfolder)
257
+ for file in files:
258
+ os.remove(os.path.join(unzipfolder, file))
259
+ else:
260
+ os.makedirs(unzipfolder)
261
+ util.mkdir_with_umask(unzipfolder)
262
+ util.unzip(source_path, unzipfolder)
263
+ is_first = True
264
+ face_set = FaceSet()
265
+ for file in os.listdir(unzipfolder):
266
+ if file.endswith(".png"):
267
+ filename = os.path.join(unzipfolder,file)
268
+ progress(0, desc="Extracting faceset")
269
+ SELECTION_FACES_DATA = extract_face_images(filename, (False, 0))
270
+ for f in SELECTION_FACES_DATA:
271
+ face = f[0]
272
+ face.mask_offsets = (0,0,0,0,1,20)
273
+ face_set.faces.append(face)
274
+ if is_first:
275
+ image = util.convert_to_gradio(f[1])
276
+ ui.globals.ui_input_thumbs.append(image)
277
+ is_first = False
278
+ face_set.ref_images.append(get_image_frame(filename))
279
+ if len(face_set.faces) > 0:
280
+ if len(face_set.faces) > 1:
281
+ face_set.AverageEmbeddings()
282
+ roop.globals.INPUT_FACESETS.append(face_set)
283
+
284
+ elif util.has_image_extension(source_path):
285
+ progress(0, desc="Retrieving faces from image")
286
+ roop.globals.source_path = source_path
287
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.source_path, (False, 0))
288
+ progress(0.5, desc="Retrieving faces from image")
289
+ for f in SELECTION_FACES_DATA:
290
+ face_set = FaceSet()
291
+ face = f[0]
292
+ face.mask_offsets = (0,0,0,0,1,20)
293
+ face_set.faces.append(face)
294
+ image = util.convert_to_gradio(f[1])
295
+ ui.globals.ui_input_thumbs.append(image)
296
+ roop.globals.INPUT_FACESETS.append(face_set)
297
+
298
+ progress(1.0)
299
+
300
+ # old style with selecting input faces commented out
301
+ # if len(thumbs) < 1:
302
+ # return gr.Column(visible=False), None, ui.globals.ui_input_thumbs
303
+ # return gr.Column(visible=True), thumbs, gr.Gallery(visible=True)
304
+
305
+ return gr.Column(visible=False), None, ui.globals.ui_input_thumbs
306
+
307
+
308
+ def on_select_input_face(evt: gr.SelectData):
309
+ global SELECTED_INPUT_FACE_INDEX
310
+
311
+ SELECTED_INPUT_FACE_INDEX = evt.index
312
+
313
+
314
+ def remove_selected_input_face():
315
+ global SELECTED_INPUT_FACE_INDEX
316
+
317
+ if len(roop.globals.INPUT_FACESETS) > SELECTED_INPUT_FACE_INDEX:
318
+ f = roop.globals.INPUT_FACESETS.pop(SELECTED_INPUT_FACE_INDEX)
319
+ del f
320
+ if len(ui.globals.ui_input_thumbs) > SELECTED_INPUT_FACE_INDEX:
321
+ f = ui.globals.ui_input_thumbs.pop(SELECTED_INPUT_FACE_INDEX)
322
+ del f
323
+
324
+ return ui.globals.ui_input_thumbs
325
+
326
+ def on_select_target_face(evt: gr.SelectData):
327
+ global SELECTED_TARGET_FACE_INDEX
328
+
329
+ SELECTED_TARGET_FACE_INDEX = evt.index
330
+
331
+ def remove_selected_target_face():
332
+ if len(roop.globals.TARGET_FACES) > SELECTED_TARGET_FACE_INDEX:
333
+ f = roop.globals.TARGET_FACES.pop(SELECTED_TARGET_FACE_INDEX)
334
+ del f
335
+ if len(ui.globals.ui_target_thumbs) > SELECTED_TARGET_FACE_INDEX:
336
+ f = ui.globals.ui_target_thumbs.pop(SELECTED_TARGET_FACE_INDEX)
337
+ del f
338
+ return ui.globals.ui_target_thumbs
339
+
340
+
341
+
342
+
343
+
344
+ def on_use_face_from_selected(files, frame_num):
345
+ global IS_INPUT, SELECTION_FACES_DATA
346
+
347
+ IS_INPUT = False
348
+ thumbs = []
349
+
350
+ roop.globals.target_path = files[selected_preview_index].name
351
+ if util.is_image(roop.globals.target_path) and not roop.globals.target_path.lower().endswith(('gif')):
352
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.target_path, (False, 0))
353
+ if len(SELECTION_FACES_DATA) > 0:
354
+ for f in SELECTION_FACES_DATA:
355
+ image = util.convert_to_gradio(f[1])
356
+ thumbs.append(image)
357
+ else:
358
+ gr.Info('No faces detected!')
359
+ roop.globals.target_path = None
360
+
361
+ elif util.is_video(roop.globals.target_path) or roop.globals.target_path.lower().endswith(('gif')):
362
+ selected_frame = frame_num
363
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.target_path, (True, selected_frame))
364
+ if len(SELECTION_FACES_DATA) > 0:
365
+ for f in SELECTION_FACES_DATA:
366
+ image = util.convert_to_gradio(f[1])
367
+ thumbs.append(image)
368
+ else:
369
+ gr.Info('No faces detected!')
370
+ roop.globals.target_path = None
371
+
372
+ if len(thumbs) == 1:
373
+ roop.globals.TARGET_FACES.append(SELECTION_FACES_DATA[0][0])
374
+ ui.globals.ui_target_thumbs.append(thumbs[0])
375
+ return gr.Row(visible=False), None, ui.globals.ui_target_thumbs, gr.Dropdown(value='Selected face')
376
+
377
+ return gr.Row(visible=True), thumbs, gr.Gallery(visible=True), gr.Dropdown(visible=True)
378
+
379
+
380
+
381
+ def on_select_face(evt: gr.SelectData): # SelectData is a subclass of EventData
382
+ global SELECTED_FACE_INDEX
383
+ SELECTED_FACE_INDEX = evt.index
384
+
385
+
386
+ def on_selected_face():
387
+ global IS_INPUT, SELECTED_FACE_INDEX, SELECTION_FACES_DATA
388
+
389
+ fd = SELECTION_FACES_DATA[SELECTED_FACE_INDEX]
390
+ image = util.convert_to_gradio(fd[1])
391
+ if IS_INPUT:
392
+ face_set = FaceSet()
393
+ fd[0].mask_offsets = (0,0,0,0,1,20)
394
+ face_set.faces.append(fd[0])
395
+ roop.globals.INPUT_FACESETS.append(face_set)
396
+ ui.globals.ui_input_thumbs.append(image)
397
+ return ui.globals.ui_input_thumbs, gr.Gallery(visible=True), gr.Dropdown(visible=True)
398
+ else:
399
+ roop.globals.TARGET_FACES.append(fd[0])
400
+ ui.globals.ui_target_thumbs.append(image)
401
+ return gr.Gallery(visible=True), ui.globals.ui_target_thumbs, gr.Dropdown(value='Selected face')
402
+
403
+ # bt_faceselect.click(fn=on_selected_face, outputs=[dynamic_face_selection, face_selection, input_faces, target_faces])
404
+
405
+ def on_end_face_selection():
406
+ return gr.Column(visible=False), None
407
+
408
+
409
+ def on_preview_frame_changed(frame_num, files, fake_preview, enhancer, detection, face_distance, blend_ratio,
410
+ selected_mask_engine, clip_text, no_face_action, vr_mode, auto_rotate, maskimage, show_face_area, num_steps):
411
+ global SELECTED_INPUT_FACE_INDEX, manual_masking, current_video_fps
412
+
413
+ from roop.core import live_swap, get_processing_plugins
414
+
415
+ manual_masking = False
416
+ mask_offsets = (0,0,0,0)
417
+ if len(roop.globals.INPUT_FACESETS) > SELECTED_INPUT_FACE_INDEX:
418
+ if not hasattr(roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0], 'mask_offsets'):
419
+ roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets = mask_offsets
420
+ mask_offsets = roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets
421
+
422
+ timeinfo = '0:00:00'
423
+ if files is None or selected_preview_index >= len(files) or frame_num is None:
424
+ return None,None, gr.Slider(info=timeinfo)
425
+
426
+ filename = files[selected_preview_index].name
427
+ if util.is_video(filename) or filename.lower().endswith('gif'):
428
+ current_frame = get_video_frame(filename, frame_num)
429
+ if current_video_fps == 0:
430
+ current_video_fps = 1
431
+ secs = (frame_num - 1) / current_video_fps
432
+ minutes = secs / 60
433
+ secs = secs % 60
434
+ hours = minutes / 60
435
+ minutes = minutes % 60
436
+ milliseconds = (secs - int(secs)) * 1000
437
+ timeinfo = f"{int(hours):0>2}:{int(minutes):0>2}:{int(secs):0>2}.{int(milliseconds):0>3}"
438
+ else:
439
+ current_frame = get_image_frame(filename)
440
+ if current_frame is None:
441
+ return None, None, gr.Slider(info=timeinfo)
442
+
443
+ layers = None
444
+ if maskimage is not None:
445
+ layers = maskimage["layers"]
446
+
447
+ if not fake_preview or len(roop.globals.INPUT_FACESETS) < 1:
448
+ return gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), gr.Slider(info=timeinfo)
449
+
450
+ roop.globals.face_swap_mode = translate_swap_mode(detection)
451
+ roop.globals.selected_enhancer = enhancer
452
+ roop.globals.distance_threshold = face_distance
453
+ roop.globals.blend_ratio = blend_ratio
454
+ roop.globals.no_face_action = index_of_no_face_action(no_face_action)
455
+ roop.globals.vr_mode = vr_mode
456
+ roop.globals.autorotate_faces = auto_rotate
457
+
458
+ mask_engine = map_mask_engine(selected_mask_engine, clip_text)
459
+
460
+ roop.globals.execution_threads = roop.globals.CFG.max_threads
461
+ mask = layers[0] if layers is not None else None
462
+ face_index = SELECTED_INPUT_FACE_INDEX
463
+ if len(roop.globals.INPUT_FACESETS) <= face_index:
464
+ face_index = 0
465
+
466
+ options = ProcessOptions(get_processing_plugins(mask_engine), roop.globals.distance_threshold, roop.globals.blend_ratio,
467
+ roop.globals.face_swap_mode, face_index, clip_text, maskimage, num_steps, show_face_area)
468
+
469
+ current_frame = live_swap(current_frame, options)
470
+ if current_frame is None:
471
+ return gr.Image(visible=True), None, gr.Slider(info=timeinfo)
472
+ return gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), gr.Slider(info=timeinfo)
473
+
474
+ def map_mask_engine(selected_mask_engine, clip_text):
475
+ if selected_mask_engine == "Clip2Seg":
476
+ mask_engine = "mask_clip2seg"
477
+ if clip_text is None or len(clip_text) < 1:
478
+ mask_engine = None
479
+ elif selected_mask_engine == "DFL XSeg":
480
+ mask_engine = "mask_xseg"
481
+ else:
482
+ mask_engine = None
483
+ return mask_engine
484
+
485
+
486
+
487
+ def on_toggle_masking(previewimage, mask):
488
+ global manual_masking
489
+
490
+ manual_masking = not manual_masking
491
+ if manual_masking:
492
+ layers = mask["layers"]
493
+ if len(layers) == 1:
494
+ layers = [create_blank_image(previewimage.shape[1],previewimage.shape[0])]
495
+ return gr.Image(visible=False), gr.ImageEditor(value={"background": previewimage, "layers": layers, "composite": None}, visible=True)
496
+ return gr.Image(visible=True), gr.ImageEditor(visible=False)
497
+
498
+ def gen_processing_text(start, end):
499
+ return f'Processing frame range [{start} - {end}]'
500
+
501
+ def on_set_frame(sender:str, frame_num):
502
+ global selected_preview_index, list_files_process
503
+
504
+ idx = selected_preview_index
505
+ if list_files_process[idx].endframe == 0:
506
+ return gen_processing_text(0,0)
507
+
508
+ start = list_files_process[idx].startframe
509
+ end = list_files_process[idx].endframe
510
+ if sender.lower().endswith('start'):
511
+ list_files_process[idx].startframe = min(frame_num, end)
512
+ else:
513
+ list_files_process[idx].endframe = max(frame_num, start)
514
+
515
+ return gen_processing_text(list_files_process[idx].startframe,list_files_process[idx].endframe)
516
+
517
+
518
+
519
+ def on_preview_mask(frame_num, files, clip_text, mask_engine):
520
+ from roop.core import live_swap, get_processing_plugins
521
+ global is_processing
522
+
523
+ if is_processing or files is None or selected_preview_index >= len(files) or clip_text is None or frame_num is None:
524
+ return None
525
+
526
+ filename = files[selected_preview_index].name
527
+ if util.is_video(filename) or filename.lower().endswith('gif'):
528
+ current_frame = get_video_frame(filename, frame_num
529
+ )
530
+ else:
531
+ current_frame = get_image_frame(filename)
532
+ if current_frame is None or mask_engine is None:
533
+ return None
534
+ if mask_engine == "Clip2Seg":
535
+ mask_engine = "mask_clip2seg"
536
+ if clip_text is None or len(clip_text) < 1:
537
+ mask_engine = None
538
+ elif mask_engine == "DFL XSeg":
539
+ mask_engine = "mask_xseg"
540
+ options = ProcessOptions(get_processing_plugins(mask_engine), roop.globals.distance_threshold, roop.globals.blend_ratio,
541
+ "all", 0, clip_text, None, 0, False, True)
542
+
543
+ current_frame = live_swap(current_frame, options)
544
+ return util.convert_to_gradio(current_frame)
545
+
546
+
547
+
548
+ def on_clear_input_faces():
549
+ ui.globals.ui_input_thumbs.clear()
550
+ roop.globals.INPUT_FACESETS.clear()
551
+ return ui.globals.ui_input_thumbs
552
+
553
+ def on_clear_destfiles():
554
+ roop.globals.TARGET_FACES.clear()
555
+ ui.globals.ui_target_thumbs.clear()
556
+ return ui.globals.ui_target_thumbs, gr.Dropdown(value="First found")
557
+
558
+
559
+ def index_of_no_face_action(dropdown_text):
560
+ global no_face_choices
561
+
562
+ return no_face_choices.index(dropdown_text)
563
+
564
+ def translate_swap_mode(dropdown_text):
565
+ if dropdown_text == "Selected face":
566
+ return "selected"
567
+ elif dropdown_text == "First found":
568
+ return "first"
569
+ elif dropdown_text == "All female":
570
+ return "all_female"
571
+ elif dropdown_text == "All male":
572
+ return "all_male"
573
+
574
+ return "all"
575
+
576
+
577
+
578
+ def start_swap( enhancer, detection, keep_frames, wait_after_extraction, skip_audio, face_distance, blend_ratio,
579
+ selected_mask_engine, clip_text, processing_method, no_face_action, vr_mode, autorotate, num_swap_steps, imagemask, progress=gr.Progress()):
580
+ from ui.main import prepare_environment
581
+ from roop.core import batch_process
582
+ global is_processing, list_files_process
583
+
584
+ if list_files_process is None or len(list_files_process) <= 0:
585
+ return gr.Button(variant="primary"), None, None
586
+
587
+ if roop.globals.CFG.clear_output:
588
+ shutil.rmtree(roop.globals.output_path)
589
+
590
+ if not util.is_installed("ffmpeg"):
591
+ msg = "ffmpeg is not installed! No video processing possible."
592
+ gr.Warning(msg)
593
+
594
+ prepare_environment()
595
+
596
+ roop.globals.selected_enhancer = enhancer
597
+ roop.globals.target_path = None
598
+ roop.globals.distance_threshold = face_distance
599
+ roop.globals.blend_ratio = blend_ratio
600
+ roop.globals.keep_frames = keep_frames
601
+ roop.globals.wait_after_extraction = wait_after_extraction
602
+ roop.globals.skip_audio = skip_audio
603
+ roop.globals.face_swap_mode = translate_swap_mode(detection)
604
+ roop.globals.no_face_action = index_of_no_face_action(no_face_action)
605
+ roop.globals.vr_mode = vr_mode
606
+ roop.globals.autorotate_faces = autorotate
607
+ mask_engine = map_mask_engine(selected_mask_engine, clip_text)
608
+
609
+ if roop.globals.face_swap_mode == 'selected':
610
+ if len(roop.globals.TARGET_FACES) < 1:
611
+ gr.Error('No Target Face selected!')
612
+ return gr.Button(variant="primary"), None, None
613
+
614
+ is_processing = True
615
+ yield gr.Button(variant="secondary", interactive=False), gr.Button(variant="primary", interactive=True), None
616
+ roop.globals.execution_threads = roop.globals.CFG.max_threads
617
+ roop.globals.video_encoder = roop.globals.CFG.output_video_codec
618
+ roop.globals.video_quality = roop.globals.CFG.video_quality
619
+ roop.globals.max_memory = roop.globals.CFG.memory_limit if roop.globals.CFG.memory_limit > 0 else None
620
+
621
+ batch_process(list_files_process, mask_engine, clip_text, processing_method == "In-Memory processing", imagemask, num_swap_steps, progress, SELECTED_INPUT_FACE_INDEX)
622
+ is_processing = False
623
+ outdir = pathlib.Path(roop.globals.output_path)
624
+ outfiles = [str(item) for item in outdir.rglob("*") if item.is_file()]
625
+ if len(outfiles) > 0:
626
+ yield gr.Button(variant="primary", interactive=True),gr.Button(variant="secondary", interactive=False),gr.Files(value=outfiles)
627
+ else:
628
+ yield gr.Button(variant="primary", interactive=True),gr.Button(variant="secondary", interactive=False),None
629
+
630
+
631
+ def stop_swap():
632
+ roop.globals.processing = False
633
+ gr.Info('Aborting processing - please wait for the remaining threads to be stopped')
634
+ return gr.Button(variant="primary", interactive=True),gr.Button(variant="secondary", interactive=False),None
635
+
636
+
637
+ def on_fps_changed(fps):
638
+ global selected_preview_index, list_files_process
639
+
640
+ if len(list_files_process) < 1 or list_files_process[selected_preview_index].endframe < 1:
641
+ return
642
+ list_files_process[selected_preview_index].fps = fps
643
+
644
+
645
+ def on_destfiles_changed(destfiles):
646
+ global selected_preview_index, list_files_process, current_video_fps
647
+
648
+ if destfiles is None or len(destfiles) < 1:
649
+ list_files_process.clear()
650
+ return gr.Slider(value=1, maximum=1, info='0:00:00'), ''
651
+
652
+ for f in destfiles:
653
+ list_files_process.append(ProcessEntry(f.name, 0,0, 0))
654
+
655
+ selected_preview_index = 0
656
+ idx = selected_preview_index
657
+
658
+ filename = list_files_process[idx].filename
659
+
660
+ if util.is_video(filename) or filename.lower().endswith('gif'):
661
+ total_frames = get_video_frame_total(filename)
662
+ current_video_fps = util.detect_fps(filename)
663
+ else:
664
+ total_frames = 1
665
+ list_files_process[idx].endframe = total_frames
666
+ if total_frames > 1:
667
+ return gr.Slider(value=1, maximum=total_frames, info='0:00:00'), gen_processing_text(list_files_process[idx].startframe,list_files_process[idx].endframe)
668
+ return gr.Slider(value=1, maximum=total_frames, info='0:00:00'), ''
669
+
670
+
671
+
672
+
673
+ def on_destfiles_selected(evt: gr.SelectData):
674
+ global selected_preview_index, list_files_process, current_video_fps
675
+
676
+ if evt is not None:
677
+ selected_preview_index = evt.index
678
+ idx = selected_preview_index
679
+ filename = list_files_process[idx].filename
680
+ fps = list_files_process[idx].fps
681
+ if util.is_video(filename) or filename.lower().endswith('gif'):
682
+ total_frames = get_video_frame_total(filename)
683
+ current_video_fps = util.detect_fps(filename)
684
+ if list_files_process[idx].endframe == 0:
685
+ list_files_process[idx].endframe = total_frames
686
+ else:
687
+ total_frames = 1
688
+
689
+ if total_frames > 1:
690
+ return gr.Slider(value=list_files_process[idx].startframe, maximum=total_frames, info='0:00:00'), gen_processing_text(list_files_process[idx].startframe,list_files_process[idx].endframe), fps
691
+ return gr.Slider(value=1, maximum=total_frames, info='0:00:00'), gen_processing_text(0,0), fps
692
+
693
+
694
+
695
+ def on_resultfiles_selected(evt: gr.SelectData, files):
696
+ selected_index = evt.index
697
+ filename = files[selected_index].name
698
+ return display_output(filename)
699
+
700
+ def on_resultfiles_finished(files):
701
+ selected_index = 0
702
+ if files is None or len(files) < 1:
703
+ return None, None
704
+
705
+ filename = files[selected_index].name
706
+ return display_output(filename)
707
+
708
+
709
+ def display_output(filename):
710
+ if util.is_video(filename) and roop.globals.CFG.output_show_video:
711
+ return gr.Image(visible=False), gr.Video(visible=True, value=filename)
712
+ else:
713
+ if util.is_video(filename) or filename.lower().endswith('gif'):
714
+ current_frame = get_video_frame(filename)
715
+ else:
716
+ current_frame = get_image_frame(filename)
717
+ return gr.Image(visible=True, value=util.convert_to_gradio(current_frame)), gr.Video(visible=False)
ui/tabs/livecam_tab.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import roop.globals
3
+ import ui.globals
4
+
5
+
6
+ camera_frame = None
7
+
8
+ def livecam_tab():
9
+ with gr.Tab("🎥 Live Cam"):
10
+ with gr.Row(variant='panel'):
11
+ gr.Markdown("""
12
+ This feature will allow you to use your physical webcam and apply the selected faces to the stream.
13
+ You can also forward the stream to a virtual camera, which can be used in video calls or streaming software.<br />
14
+ Supported are: v4l2loopback (linux), OBS Virtual Camera (macOS/Windows) and unitycapture (Windows).<br />
15
+ **Please note:** to change the face or any other settings you need to stop and restart a running live cam.
16
+ """)
17
+
18
+ with gr.Row(variant='panel'):
19
+ with gr.Column():
20
+ bt_start = gr.Button("▶ Start", variant='primary')
21
+ with gr.Column():
22
+ bt_stop = gr.Button("⏹ Stop", variant='secondary', interactive=False)
23
+ with gr.Column():
24
+ camera_num = gr.Slider(0, 2, value=0, label="Camera Number", step=1.0, interactive=True)
25
+ cb_obs = gr.Checkbox(label="Forward stream to virtual camera", interactive=True)
26
+ with gr.Column():
27
+ dd_reso = gr.Dropdown(choices=["640x480","1280x720", "1920x1080"], value="1280x720", label="Fake Camera Resolution", interactive=True)
28
+
29
+ with gr.Row():
30
+ fake_cam_image = gr.Image(label='Fake Camera Output', interactive=False)
31
+
32
+ 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],outputs=[bt_start, bt_stop,fake_cam_image])
33
+ bt_stop.click(fn=stop_swap, cancels=[start_event], outputs=[bt_start, bt_stop], queue=False)
34
+
35
+
36
+ def start_cam(stream_to_obs, cam, reso, enhancer, blend_ratio):
37
+ from roop.virtualcam import start_virtual_cam
38
+ from roop.utilities import convert_to_gradio
39
+
40
+ start_virtual_cam(stream_to_obs, cam, reso)
41
+ roop.globals.selected_enhancer = enhancer
42
+ roop.globals.blend_ratio = blend_ratio
43
+ while True:
44
+ yield gr.Button(interactive=False), gr.Button(interactive=True), convert_to_gradio(ui.globals.ui_camera_frame)
45
+
46
+
47
+ def stop_swap():
48
+ from roop.virtualcam import stop_virtual_cam
49
+ stop_virtual_cam()
50
+ return gr.Button(interactive=True), gr.Button(interactive=False)
51
+
52
+
53
+
54
+
ui/tabs/settings_tab.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ import os
3
+ import gradio as gr
4
+ import roop.globals
5
+ import ui.globals
6
+
7
+ available_themes = ["Default", "gradio/glass", "gradio/monochrome", "gradio/seafoam", "gradio/soft", "gstaff/xkcd", "freddyaboulton/dracula_revamped", "ysharma/steampunk"]
8
+ image_formats = ['jpg','png', 'webp']
9
+ video_formats = ['avi','mkv', 'mp4', 'webm']
10
+ video_codecs = ['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc']
11
+ providerlist = None
12
+
13
+ settings_controls = []
14
+
15
+ def settings_tab():
16
+ from roop.core import suggest_execution_providers
17
+ global providerlist
18
+
19
+ providerlist = suggest_execution_providers()
20
+ with gr.Tab("⚙ Settings"):
21
+ with gr.Row():
22
+ with gr.Column():
23
+ themes = gr.Dropdown(available_themes, label="Theme", info="Change needs complete restart", value=roop.globals.CFG.selected_theme)
24
+ with gr.Column():
25
+ settings_controls.append(gr.Checkbox(label="Public Server", value=roop.globals.CFG.server_share, elem_id='server_share', interactive=True))
26
+ settings_controls.append(gr.Checkbox(label='Clear output folder before each run', value=roop.globals.CFG.clear_output, elem_id='clear_output', interactive=True))
27
+ output_template = gr.Textbox(label="Filename Output Template", info="(file extension is added automatically)", lines=1, placeholder='{file}_{time}', value=roop.globals.CFG.output_template)
28
+ with gr.Column():
29
+ input_server_name = gr.Textbox(label="Server Name", lines=1, info="Leave blank to run locally", value=roop.globals.CFG.server_name)
30
+ with gr.Column():
31
+ input_server_port = gr.Number(label="Server Port", precision=0, info="Leave at 0 to use default", value=roop.globals.CFG.server_port)
32
+ with gr.Row():
33
+ with gr.Column():
34
+ settings_controls.append(gr.Dropdown(providerlist, label="Provider", value=roop.globals.CFG.provider, elem_id='provider', interactive=True))
35
+ chk_det_size = gr.Checkbox(label="Use default Det-Size", value=True, elem_id='default_det_size', interactive=True)
36
+ settings_controls.append(gr.Checkbox(label="Force CPU for Face Analyser", value=roop.globals.CFG.force_cpu, elem_id='force_cpu', interactive=True))
37
+ max_threads = gr.Slider(1, 32, value=roop.globals.CFG.max_threads, label="Max. Number of Threads", info='default: 3', step=1.0, interactive=True)
38
+ with gr.Column():
39
+ memory_limit = gr.Slider(0, 128, value=roop.globals.CFG.memory_limit, label="Max. Memory to use (Gb)", info='0 meaning no limit', step=1.0, interactive=True)
40
+ settings_controls.append(gr.Dropdown(image_formats, label="Image Output Format", info='default: png', value=roop.globals.CFG.output_image_format, elem_id='output_image_format', interactive=True))
41
+ with gr.Column():
42
+ settings_controls.append(gr.Dropdown(video_codecs, label="Video Codec", info='default: libx264', value=roop.globals.CFG.output_video_codec, elem_id='output_video_codec', interactive=True))
43
+ settings_controls.append(gr.Dropdown(video_formats, label="Video Output Format", info='default: mp4', value=roop.globals.CFG.output_video_format, elem_id='output_video_format', interactive=True))
44
+ video_quality = gr.Slider(0, 100, value=roop.globals.CFG.video_quality, label="Video Quality (crf)", info='default: 14', step=1.0, interactive=True)
45
+ with gr.Column():
46
+ with gr.Group():
47
+ 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))
48
+ settings_controls.append(gr.Checkbox(label='Show video in browser (re-encodes output)', value=roop.globals.CFG.output_show_video, elem_id='output_show_video', interactive=True))
49
+ button_apply_restart = gr.Button("Restart Server", variant='primary')
50
+ button_clean_temp = gr.Button("Clean temp folder")
51
+ button_apply_settings = gr.Button("Apply Settings")
52
+
53
+ chk_det_size.select(fn=on_option_changed)
54
+
55
+ # Settings
56
+ for s in settings_controls:
57
+ s.select(fn=on_settings_changed)
58
+ max_threads.input(fn=lambda a,b='max_threads':on_settings_changed_misc(a,b), inputs=[max_threads])
59
+ memory_limit.input(fn=lambda a,b='memory_limit':on_settings_changed_misc(a,b), inputs=[memory_limit])
60
+ video_quality.input(fn=lambda a,b='video_quality':on_settings_changed_misc(a,b), inputs=[video_quality])
61
+
62
+ # button_clean_temp.click(fn=clean_temp, outputs=[bt_srcfiles, input_faces, target_faces, bt_destfiles])
63
+ button_clean_temp.click(fn=clean_temp)
64
+ button_apply_settings.click(apply_settings, inputs=[themes, input_server_name, input_server_port, output_template])
65
+ button_apply_restart.click(restart)
66
+
67
+
68
+ def on_option_changed(evt: gr.SelectData):
69
+ attribname = evt.target.elem_id
70
+ if isinstance(evt.target, gr.Checkbox):
71
+ if hasattr(roop.globals, attribname):
72
+ setattr(roop.globals, attribname, evt.selected)
73
+ return
74
+ elif isinstance(evt.target, gr.Dropdown):
75
+ if hasattr(roop.globals, attribname):
76
+ setattr(roop.globals, attribname, evt.value)
77
+ return
78
+ raise gr.Error(f'Unhandled Setting for {evt.target}')
79
+
80
+
81
+ def on_settings_changed_misc(new_val, attribname):
82
+ if hasattr(roop.globals.CFG, attribname):
83
+ setattr(roop.globals.CFG, attribname, new_val)
84
+ else:
85
+ print("Didn't find attrib!")
86
+
87
+
88
+
89
+ def on_settings_changed(evt: gr.SelectData):
90
+ attribname = evt.target.elem_id
91
+ if isinstance(evt.target, gr.Checkbox):
92
+ if hasattr(roop.globals.CFG, attribname):
93
+ setattr(roop.globals.CFG, attribname, evt.selected)
94
+ return
95
+ elif isinstance(evt.target, gr.Dropdown):
96
+ if hasattr(roop.globals.CFG, attribname):
97
+ setattr(roop.globals.CFG, attribname, evt.value)
98
+ return
99
+
100
+ raise gr.Error(f'Unhandled Setting for {evt.target}')
101
+
102
+ def clean_temp():
103
+ from ui.main import prepare_environment
104
+
105
+ if not roop.globals.CFG.use_os_temp_folder:
106
+ shutil.rmtree(os.environ["TEMP"])
107
+ prepare_environment()
108
+
109
+ ui.globals.ui_input_thumbs.clear()
110
+ roop.globals.INPUT_FACESETS.clear()
111
+ roop.globals.TARGET_FACES.clear()
112
+ ui.globals.ui_target_thumbs = []
113
+ gr.Info('Temp Files removed')
114
+ return None,None,None,None
115
+
116
+
117
+ def apply_settings(themes, input_server_name, input_server_port, output_template):
118
+ from ui.main import show_msg
119
+
120
+ roop.globals.CFG.selected_theme = themes
121
+ roop.globals.CFG.server_name = input_server_name
122
+ roop.globals.CFG.server_port = input_server_port
123
+ roop.globals.CFG.output_template = output_template
124
+ roop.globals.CFG.save()
125
+ show_msg('Settings saved')
126
+
127
+
128
+ def restart():
129
+ ui.globals.ui_restart_server = True