jhj0517 commited on
Commit
06525bf
β€’
1 Parent(s): 62faa17

Add preview filter feature

Browse files
Files changed (1) hide show
  1. app.py +65 -24
app.py CHANGED
@@ -9,8 +9,9 @@ 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:
@@ -21,6 +22,10 @@ class App:
21
  self.sam_inf = SamInference()
22
  self.image_modes = [AUTOMATIC_MODE, BOX_PROMPT_MODE]
23
  self.default_mode = BOX_PROMPT_MODE
 
 
 
 
24
  default_param_config_path = os.path.join(SAM2_CONFIGS_DIR, "default_hparams.yaml")
25
  with open(default_param_config_path, 'r') as file:
26
  self.hparams = yaml.safe_load(file)
@@ -54,14 +59,25 @@ class App:
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
@@ -69,8 +85,7 @@ class App:
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):
@@ -121,17 +136,34 @@ class App:
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")
@@ -141,15 +173,24 @@ class App:
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
 
 
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, PIXELIZE_FILTER, COLOR_FILTER, DEFAULT_COLOR,
13
+ DEFAULT_PIXEL_SIZE)
14
+ from modules.video_utils import extract_frames, get_frames_from_dir, clean_image_files
15
 
16
 
17
  class App:
 
22
  self.sam_inf = SamInference()
23
  self.image_modes = [AUTOMATIC_MODE, BOX_PROMPT_MODE]
24
  self.default_mode = BOX_PROMPT_MODE
25
+ self.filter_modes = [PIXELIZE_FILTER, COLOR_FILTER]
26
+ self.default_filter = PIXELIZE_FILTER
27
+ self.default_color = DEFAULT_COLOR
28
+ self.default_pixel_size = DEFAULT_PIXEL_SIZE
29
  default_param_config_path = os.path.join(SAM2_CONFIGS_DIR, "default_hparams.yaml")
30
  with open(default_param_config_path, 'r') as file:
31
  self.hparams = yaml.safe_load(file)
 
59
  gr.Accordion(visible=mode == AUTOMATIC_MODE),
60
  ]
61
 
62
+ @staticmethod
63
+ def on_filter_mode_change(mode: str):
64
+ return [
65
+ gr.ColorPicker(visible=mode == COLOR_FILTER),
66
+ gr.Number(visible=mode == PIXELIZE_FILTER)
67
+ ]
68
+
69
+ def on_video_model_change(self,
70
+ model_type: str,
71
+ vid_input: str):
72
  output_temp_dir = TEMP_DIR
73
+ clean_image_files(output_temp_dir)
74
  extract_frames(vid_input=vid_input, output_temp_dir=output_temp_dir)
75
  frames = get_frames_from_dir(vid_dir=output_temp_dir)
76
+ initial_frame, max_frame_index = frames[0], (len(frames)-1)
77
+ self.sam_inf.init_video_inference_state(vid_input=output_temp_dir, model_type=model_type)
78
  return [
79
+ ImagePrompter(label="Prompt image with Box & Point", value=initial_frame),
80
+ gr.Slider(label="Frame Index", value=0, interactive=True, step=1, minimum=0, maximum=max_frame_index)
81
  ]
82
 
83
  @staticmethod
 
85
  temp_dir = TEMP_DIR
86
  frames = get_frames_from_dir(vid_dir=temp_dir)
87
  selected_frame = frames[frame_idx]
88
+ return ImagePrompter(label=f"Prompt image with Box & Point", value=selected_frame)
 
89
 
90
  @staticmethod
91
  def on_prompt_change(prompt: Dict):
 
136
  inputs=[dd_input_modes],
137
  outputs=[img_input, img_input_prompter, acc_mask_hparams])
138
 
139
+ with gr.TabItem("Pixelize Filter"):
140
+ with gr.Column():
141
+ file_vid_input = gr.File(label="Input Video here", file_types=['.mp4', '.avi', '.mov', '.wmv',
142
+ '.flv', '.webm', '.mkv', '.mpeg',
143
+ '.mpg', '.m4v', '.3gp', '.ts',
144
+ '.vob'])
145
+ with gr.Row(equal_height=True):
146
+ with gr.Column(scale=9):
147
+ with gr.Row():
148
+ vid_frame_prompter = ImagePrompter(label="Prompt image with Box & Point", type='pil',
149
+ interactive=True, scale=5)
150
+ img_preview = gr.Image(label="Preview", interactive=False, scale=5)
151
+
152
+ sld_frame_selector = gr.Slider(label="Frame Index", interactive=False)
153
+
154
+ with gr.Column(scale=1):
155
+ dd_models = gr.Dropdown(label="Model", value=DEFAULT_MODEL_TYPE,
156
+ choices=self.sam_inf.available_models)
157
+ dd_filter_mode = gr.Dropdown(label="Filter Modes", interactive=True,
158
+ value=self.default_filter,
159
+ choices=self.filter_modes)
160
+ cp_color_picker = gr.ColorPicker(label="Solid Color", interactive=True,
161
+ visible=self.default_filter == COLOR_FILTER,
162
+ value=self.default_color)
163
+ nb_pixel_size = gr.Number(label="Pixel Size", interactive=True, minimum=1,
164
+ visible=self.default_filter == PIXELIZE_FILTER,
165
+ value=self.default_pixel_size)
166
+ btn_generate_preview = gr.Button("GENERATE PREVIEW")
167
 
168
  with gr.Row():
169
  btn_generate = gr.Button("GENERATE", variant="primary")
 
173
  output_file = gr.File(label="Generated psd file", scale=9)
174
  btn_open_folder = gr.Button("πŸ“\nOpen PSD folder", scale=1)
175
 
176
+ file_vid_input.change(fn=self.on_video_model_change,
177
+ inputs=[dd_models, file_vid_input],
178
+ outputs=[vid_frame_prompter, sld_frame_selector])
179
+ dd_models.change(fn=self.on_video_model_change,
180
+ inputs=[dd_models, file_vid_input],
181
  outputs=[vid_frame_prompter, sld_frame_selector])
182
  sld_frame_selector.change(fn=self.on_frame_change,
183
  inputs=[sld_frame_selector],
184
  outputs=[vid_frame_prompter],)
185
+ dd_filter_mode.change(fn=self.on_filter_mode_change,
186
+ inputs=[dd_filter_mode],
187
+ outputs=[cp_color_picker,
188
+ nb_pixel_size])
189
+
190
+ preview_params = [vid_frame_prompter, dd_filter_mode, sld_frame_selector, nb_pixel_size, cp_color_picker]
191
+ btn_generate_preview.click(fn=self.sam_inf.add_filter_to_preview,
192
+ inputs=preview_params,
193
+ outputs=[img_preview])
194
 
195
  self.demo.queue().launch(inbrowser=True)
196