Spaces:
Paused
Paused
jhj0517 commited on
Commit ·
a6933f9
1
Parent(s): 80b3718
Add initial UI for mosaic filter
Browse files
app.py
CHANGED
|
@@ -1,20 +1,22 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_image_prompter import ImagePrompter
|
|
|
|
| 3 |
from typing import List, Dict, Optional, Union
|
| 4 |
import os
|
| 5 |
import yaml
|
| 6 |
|
| 7 |
from modules.sam_inference import SamInference
|
| 8 |
from modules.model_downloader import DEFAULT_MODEL_TYPE
|
| 9 |
-
from modules.paths import (OUTPUT_DIR, OUTPUT_PSD_DIR, SAM2_CONFIGS_DIR)
|
| 10 |
from modules.utils import open_folder
|
| 11 |
from modules.constants import (AUTOMATIC_MODE, BOX_PROMPT_MODE)
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
class App:
|
| 15 |
def __init__(self,
|
| 16 |
args=None):
|
| 17 |
-
self.
|
| 18 |
self.args = args
|
| 19 |
self.sam_inf = SamInference()
|
| 20 |
self.image_modes = [AUTOMATIC_MODE, BOX_PROMPT_MODE]
|
|
@@ -52,51 +54,106 @@ class App:
|
|
| 52 |
gr.Accordion(visible=mode == AUTOMATIC_MODE),
|
| 53 |
]
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
with gr.Column(scale=5):
|
| 66 |
-
dd_input_modes = gr.Dropdown(label="Image Input Mode", value=self.default_mode,
|
| 67 |
-
choices=self.image_modes)
|
| 68 |
-
dd_models = gr.Dropdown(label="Model", value=DEFAULT_MODEL_TYPE,
|
| 69 |
-
choices=self.sam_inf.available_models)
|
| 70 |
-
|
| 71 |
-
with gr.Accordion("Mask Parameters", open=False, visible=self.default_mode == AUTOMATIC_MODE) as acc_mask_hparams:
|
| 72 |
-
mask_hparams_component = self.mask_parameters(_mask_hparams)
|
| 73 |
-
|
| 74 |
-
cb_multimask_output = gr.Checkbox(label="multimask_output", value=_mask_hparams["multimask_output"])
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
btn_open_folder.click(fn=lambda: open_folder(os.path.join(OUTPUT_PSD_DIR)),
|
| 92 |
-
inputs=None, outputs=None)
|
| 93 |
-
dd_input_modes.change(fn=self.on_mode_change,
|
| 94 |
-
inputs=[dd_input_modes],
|
| 95 |
-
outputs=[img_input, img_input_prompter, acc_mask_hparams])
|
| 96 |
|
| 97 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_image_prompter import ImagePrompter
|
| 3 |
+
from gradio_image_prompter.image_prompter import PromptData
|
| 4 |
from typing import List, Dict, Optional, Union
|
| 5 |
import os
|
| 6 |
import yaml
|
| 7 |
|
| 8 |
from modules.sam_inference import SamInference
|
| 9 |
from modules.model_downloader import DEFAULT_MODEL_TYPE
|
| 10 |
+
from modules.paths import (OUTPUT_DIR, OUTPUT_PSD_DIR, SAM2_CONFIGS_DIR, TEMP_DIR)
|
| 11 |
from modules.utils import open_folder
|
| 12 |
from modules.constants import (AUTOMATIC_MODE, BOX_PROMPT_MODE)
|
| 13 |
+
from modules.video_utils import extract_frames, get_frames_from_dir
|
| 14 |
|
| 15 |
|
| 16 |
class App:
|
| 17 |
def __init__(self,
|
| 18 |
args=None):
|
| 19 |
+
self.demo = gr.Blocks()
|
| 20 |
self.args = args
|
| 21 |
self.sam_inf = SamInference()
|
| 22 |
self.image_modes = [AUTOMATIC_MODE, BOX_PROMPT_MODE]
|
|
|
|
| 54 |
gr.Accordion(visible=mode == AUTOMATIC_MODE),
|
| 55 |
]
|
| 56 |
|
| 57 |
+
def on_video_upload(self, vid_input: str):
|
| 58 |
+
output_temp_dir = TEMP_DIR
|
| 59 |
+
extract_frames(vid_input=vid_input, output_temp_dir=output_temp_dir)
|
| 60 |
+
frames = get_frames_from_dir(vid_dir=output_temp_dir)
|
| 61 |
+
# self.sam_inf.init_video_inference_state(output_temp_dir)
|
| 62 |
+
return [
|
| 63 |
+
ImagePrompter(label="Prompt image with Box & Point", value=frames[0]),
|
| 64 |
+
gr.Slider(label="Frame Indexes", value=0, interactive=True, step=1, minimum=0, maximum=(len(frames)-1))
|
| 65 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
@staticmethod
|
| 68 |
+
def on_frame_change(frame_idx: int):
|
| 69 |
+
temp_dir = TEMP_DIR
|
| 70 |
+
frames = get_frames_from_dir(vid_dir=temp_dir)
|
| 71 |
+
selected_frame = frames[frame_idx]
|
| 72 |
+
return ImagePrompter(elem_id="vid-prompter-index", label=f"Prompt image with Box & Point #{frame_idx}",
|
| 73 |
+
value=selected_frame)
|
| 74 |
|
| 75 |
+
@staticmethod
|
| 76 |
+
def on_prompt_change(prompt: Dict):
|
| 77 |
+
image, points = prompt["image"], prompt["points"]
|
| 78 |
+
return gr.Image(label="Preview", value=image)
|
| 79 |
|
| 80 |
+
def launch(self):
|
| 81 |
+
_mask_hparams = self.hparams["mask_hparams"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
with self.demo:
|
| 84 |
+
with gr.Tabs():
|
| 85 |
+
with gr.TabItem("Layer Divider"):
|
| 86 |
+
with gr.Row():
|
| 87 |
+
with gr.Column(scale=5):
|
| 88 |
+
img_input = gr.Image(label="Input image here", visible=self.default_mode == AUTOMATIC_MODE)
|
| 89 |
+
img_input_prompter = ImagePrompter(label="Prompt image with Box & Point", type='pil',
|
| 90 |
+
visible=self.default_mode == BOX_PROMPT_MODE)
|
| 91 |
+
|
| 92 |
+
with gr.Column(scale=5):
|
| 93 |
+
dd_input_modes = gr.Dropdown(label="Image Input Mode", value=self.default_mode,
|
| 94 |
+
choices=self.image_modes)
|
| 95 |
+
dd_models = gr.Dropdown(label="Model", value=DEFAULT_MODEL_TYPE,
|
| 96 |
+
choices=self.sam_inf.available_models)
|
| 97 |
+
|
| 98 |
+
with gr.Accordion("Mask Parameters", open=False, visible=self.default_mode == AUTOMATIC_MODE) as acc_mask_hparams:
|
| 99 |
+
mask_hparams_component = self.mask_parameters(_mask_hparams)
|
| 100 |
+
|
| 101 |
+
cb_multimask_output = gr.Checkbox(label="multimask_output", value=_mask_hparams["multimask_output"])
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
btn_generate = gr.Button("GENERATE", variant="primary")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
gallery_output = gr.Gallery(label="Output images will be shown here")
|
| 107 |
+
with gr.Column():
|
| 108 |
+
output_file = gr.File(label="Generated psd file", scale=9)
|
| 109 |
+
btn_open_folder = gr.Button("📁\nOpen PSD folder", scale=1)
|
| 110 |
+
|
| 111 |
+
sources = [img_input, img_input_prompter, dd_input_modes]
|
| 112 |
+
model_params = [dd_models]
|
| 113 |
+
mask_hparams = mask_hparams_component + [cb_multimask_output]
|
| 114 |
+
input_params = sources + model_params + mask_hparams
|
| 115 |
+
|
| 116 |
+
btn_generate.click(fn=self.sam_inf.divide_layer,
|
| 117 |
+
inputs=input_params, outputs=[gallery_output, output_file])
|
| 118 |
+
btn_open_folder.click(fn=lambda: open_folder(os.path.join(OUTPUT_PSD_DIR)),
|
| 119 |
+
inputs=None, outputs=None)
|
| 120 |
+
dd_input_modes.change(fn=self.on_mode_change,
|
| 121 |
+
inputs=[dd_input_modes],
|
| 122 |
+
outputs=[img_input, img_input_prompter, acc_mask_hparams])
|
| 123 |
+
|
| 124 |
+
with gr.TabItem("Mosaic Filter"):
|
| 125 |
+
with gr.Row(equal_height=True):
|
| 126 |
+
with gr.Column(scale=2):
|
| 127 |
+
vid_input = gr.Video(label="Input Video here", scale=3)
|
| 128 |
+
with gr.Column(scale=8):
|
| 129 |
+
with gr.Row():
|
| 130 |
+
vid_frame_prompter = ImagePrompter(elem_id="vid-prompter",
|
| 131 |
+
label="Prompt image with Box & Point ",
|
| 132 |
+
interactive=True, scale=5)
|
| 133 |
+
img_preview = gr.Image(label="Preview", interactive=False, scale=5)
|
| 134 |
+
sld_frame_selector = gr.Slider(label="Frame Index", interactive=False)
|
| 135 |
+
|
| 136 |
+
with gr.Row():
|
| 137 |
+
btn_generate = gr.Button("GENERATE", variant="primary")
|
| 138 |
+
with gr.Row():
|
| 139 |
+
gallery_output = gr.Gallery(label="Output images will be shown here")
|
| 140 |
+
with gr.Column():
|
| 141 |
+
output_file = gr.File(label="Generated psd file", scale=9)
|
| 142 |
+
btn_open_folder = gr.Button("📁\nOpen PSD folder", scale=1)
|
| 143 |
+
|
| 144 |
+
vid_input.change(fn=self.on_video_upload,
|
| 145 |
+
inputs=[vid_input],
|
| 146 |
+
outputs=[vid_frame_prompter, sld_frame_selector])
|
| 147 |
+
sld_frame_selector.change(fn=self.on_frame_change,
|
| 148 |
+
inputs=[sld_frame_selector],
|
| 149 |
+
outputs=[vid_frame_prompter],)
|
| 150 |
+
vid_frame_prompter.change(fn=self.on_prompt_change,
|
| 151 |
+
inputs=[vid_frame_prompter],
|
| 152 |
+
outputs=[img_preview])
|
| 153 |
+
|
| 154 |
+
self.demo.queue().launch(inbrowser=True)
|
| 155 |
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
+
demo = App()
|
| 159 |
+
demo.launch()
|