MogensR commited on
Commit
a7c3f7d
·
1 Parent(s): 7c6d976

Update core/app.py

Browse files
Files changed (1) hide show
  1. core/app.py +59 -16
core/app.py CHANGED
@@ -125,30 +125,53 @@ def load_models(self, progress_callback: Optional[Callable] = None) -> str:
125
  progress_callback(0.0, f"Starting model loading on {self.device_manager.get_optimal_device()}")
126
 
127
  # Load models using the specialized loader
128
- sam2_predictor, matanyone_model = self.model_loader.load_all_models(
129
- progress_callback=progress_callback,
130
- cancel_event=self.cancel_event
131
- )
132
 
133
  if self.cancel_event.is_set():
134
  return "Model loading cancelled"
135
 
 
 
 
 
 
 
 
136
  # Initialize core processor with correct parameters
137
  self.core_processor = CoreVideoProcessor(
138
  config=self.config,
139
  models=self.model_loader
140
  )
141
 
142
- # Initialize two-stage processor if available
143
- if TWO_STAGE_AVAILABLE and sam2_predictor and matanyone_model:
144
- try:
145
- self.two_stage_processor = TwoStageProcessor(sam2_predictor, matanyone_model)
146
- logger.info("Two-stage processor initialized")
147
- except Exception as e:
148
- logger.warning(f"Two-stage processor init failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
149
 
150
  self.models_loaded = True
151
  message = self.model_loader.get_load_summary()
 
 
 
 
 
 
 
152
  logger.info(message)
153
  return message
154
 
@@ -192,12 +215,20 @@ def process_video(
192
 
193
  try:
194
  # Route to appropriate processing method
195
- if use_two_stage and TWO_STAGE_AVAILABLE and self.two_stage_processor:
 
 
 
 
 
 
 
196
  return self._process_two_stage(
197
  video_path, background_choice, custom_background_path,
198
  progress_callback, chroma_preset
199
  )
200
  else:
 
201
  return self._process_single_stage(
202
  video_path, background_choice, custom_background_path,
203
  progress_callback, preview_mask, preview_greenscreen
@@ -267,6 +298,9 @@ def _process_two_stage(
267
  ) -> Tuple[Optional[str], str]:
268
  """Process video using two-stage pipeline"""
269
 
 
 
 
270
  # Get video dimensions for background preparation
271
  import cv2
272
  cap = cv2.VideoCapture(video_path)
@@ -290,6 +324,7 @@ def _process_two_stage(
290
 
291
  chroma_settings = CHROMA_PRESETS.get(chroma_preset, CHROMA_PRESETS['standard'])
292
 
 
293
  result, message = self.two_stage_processor.process_full_pipeline(
294
  video_path,
295
  background,
@@ -302,10 +337,10 @@ def _process_two_stage(
302
  return None, message
303
 
304
  success_msg = (
305
- f"Two-stage success!\n"
306
  f"Background: {background_choice}\n"
307
- f"Preset: {chroma_preset}\n"
308
- f"Quality: Cinema-grade\n"
309
  f"Device: {self.device_manager.get_optimal_device()}"
310
  )
311
 
@@ -324,11 +359,19 @@ def get_status(self) -> Dict[str, Any]:
324
  # Add model-specific status if available
325
  if self.model_loader:
326
  base_status['model_loader_available'] = True
 
 
327
 
328
  # Add processing status if available
329
  if self.core_processor:
330
  base_status['core_processor_loaded'] = True
331
 
 
 
 
 
 
 
332
  # Add progress tracking if available
333
  if self.progress_tracker:
334
  base_status['progress'] = self.progress_tracker.get_all_progress()
@@ -388,7 +431,7 @@ def main():
388
  try:
389
  logger.info("Starting Video Background Replacement application")
390
  logger.info(f"Device: {processor.device_manager.get_optimal_device()}")
391
- logger.info(f"Two-stage available: {TWO_STAGE_AVAILABLE}")
392
  logger.info("Modular architecture loaded successfully")
393
 
394
  # Import and create UI
 
125
  progress_callback(0.0, f"Starting model loading on {self.device_manager.get_optimal_device()}")
126
 
127
  # Load models using the specialized loader
128
+ # Note: load_models returns a boolean, not the models themselves
129
+ success = self.model_loader.load_models()
 
 
130
 
131
  if self.cancel_event.is_set():
132
  return "Model loading cancelled"
133
 
134
+ if not success:
135
+ return "Model loading failed - check logs for details"
136
+
137
+ # Get the actual model instances using the getter methods
138
+ sam2_predictor = self.model_loader.get_sam2()
139
+ matanyone_model = self.model_loader.get_matanyone()
140
+
141
  # Initialize core processor with correct parameters
142
  self.core_processor = CoreVideoProcessor(
143
  config=self.config,
144
  models=self.model_loader
145
  )
146
 
147
+ # Initialize two-stage processor if available and models loaded
148
+ if TWO_STAGE_AVAILABLE:
149
+ if sam2_predictor is not None and matanyone_model is not None:
150
+ try:
151
+ self.two_stage_processor = TwoStageProcessor(
152
+ sam2_predictor=sam2_predictor,
153
+ matanyone_model=matanyone_model
154
+ )
155
+ logger.info("✅ Two-stage processor initialized with AI models")
156
+ except Exception as e:
157
+ logger.warning(f"Two-stage processor init failed: {e}")
158
+ self.two_stage_processor = None
159
+ else:
160
+ logger.warning("Two-stage processor not initialized - models not available")
161
+ if sam2_predictor is None:
162
+ logger.warning(" - SAM2 predictor is None")
163
+ if matanyone_model is None:
164
+ logger.warning(" - MatAnyone model is None")
165
 
166
  self.models_loaded = True
167
  message = self.model_loader.get_load_summary()
168
+
169
+ # Add two-stage status to message
170
+ if self.two_stage_processor is not None:
171
+ message += "\n✅ Two-stage processor ready"
172
+ else:
173
+ message += "\n⚠️ Two-stage processor not available"
174
+
175
  logger.info(message)
176
  return message
177
 
 
215
 
216
  try:
217
  # Route to appropriate processing method
218
+ if use_two_stage:
219
+ if not TWO_STAGE_AVAILABLE:
220
+ return None, "Two-stage processing not available - module not found"
221
+
222
+ if self.two_stage_processor is None:
223
+ return None, "Two-stage processor not initialized - models may not be loaded properly"
224
+
225
+ logger.info("Using two-stage processing pipeline with AI models")
226
  return self._process_two_stage(
227
  video_path, background_choice, custom_background_path,
228
  progress_callback, chroma_preset
229
  )
230
  else:
231
+ logger.info("Using single-stage processing pipeline")
232
  return self._process_single_stage(
233
  video_path, background_choice, custom_background_path,
234
  progress_callback, preview_mask, preview_greenscreen
 
298
  ) -> Tuple[Optional[str], str]:
299
  """Process video using two-stage pipeline"""
300
 
301
+ if self.two_stage_processor is None:
302
+ return None, "Two-stage processor not available"
303
+
304
  # Get video dimensions for background preparation
305
  import cv2
306
  cap = cv2.VideoCapture(video_path)
 
324
 
325
  chroma_settings = CHROMA_PRESETS.get(chroma_preset, CHROMA_PRESETS['standard'])
326
 
327
+ logger.info(f"Starting two-stage processing with chroma preset: {chroma_preset}")
328
  result, message = self.two_stage_processor.process_full_pipeline(
329
  video_path,
330
  background,
 
337
  return None, message
338
 
339
  success_msg = (
340
+ f"Two-stage processing completed!\n"
341
  f"Background: {background_choice}\n"
342
+ f"Chroma Preset: {chroma_preset}\n"
343
+ f"Quality: Cinema-grade with AI models\n"
344
  f"Device: {self.device_manager.get_optimal_device()}"
345
  )
346
 
 
359
  # Add model-specific status if available
360
  if self.model_loader:
361
  base_status['model_loader_available'] = True
362
+ base_status['sam2_loaded'] = self.model_loader.get_sam2() is not None
363
+ base_status['matanyone_loaded'] = self.model_loader.get_matanyone() is not None
364
 
365
  # Add processing status if available
366
  if self.core_processor:
367
  base_status['core_processor_loaded'] = True
368
 
369
+ # Add two-stage processor status
370
+ if self.two_stage_processor:
371
+ base_status['two_stage_processor_ready'] = True
372
+ else:
373
+ base_status['two_stage_processor_ready'] = False
374
+
375
  # Add progress tracking if available
376
  if self.progress_tracker:
377
  base_status['progress'] = self.progress_tracker.get_all_progress()
 
431
  try:
432
  logger.info("Starting Video Background Replacement application")
433
  logger.info(f"Device: {processor.device_manager.get_optimal_device()}")
434
+ logger.info(f"Two-stage module available: {TWO_STAGE_AVAILABLE}")
435
  logger.info("Modular architecture loaded successfully")
436
 
437
  # Import and create UI