(
clips,
device=None,
usage="General",
dilate_radius=0,
*,
on_clip_start: Callable[[str, int], None] | None = None,
on_frame_complete: Callable[[int, int], None] | None = None,
)
| 304 | |
| 305 | |
| 306 | def run_birefnet( |
| 307 | clips, |
| 308 | device=None, |
| 309 | usage="General", |
| 310 | dilate_radius=0, |
| 311 | *, |
| 312 | on_clip_start: Callable[[str, int], None] | None = None, |
| 313 | on_frame_complete: Callable[[int, int], None] | None = None, |
| 314 | ): |
| 315 | clips_to_process = [c for c in clips if c.alpha_asset is None] |
| 316 | |
| 317 | if not clips_to_process: |
| 318 | logger.info("All clips have valid Alpha assets. No BiRefNet generation needed.") |
| 319 | return |
| 320 | |
| 321 | if device is None: |
| 322 | device = resolve_device() |
| 323 | |
| 324 | logger.info(f"Found {len(clips_to_process)} clips missing Alpha.") |
| 325 | |
| 326 | logger.info(f"Initializing BiRefNet ({usage}) on {device}...") |
| 327 | # Initialize the handler once |
| 328 | try: |
| 329 | handler = BiRefNetHandler(device=device, usage=usage) |
| 330 | except ImportError as e: |
| 331 | logger.error(f"BiRefNet Import Error: {e}") |
| 332 | return |
| 333 | except Exception as e: |
| 334 | logger.error(f"BiRefNet Initialization Error: {e}") |
| 335 | return |
| 336 | |
| 337 | try: |
| 338 | for clip in clips_to_process: |
| 339 | logger.info(f"Generating BiRefNet Alpha for: {clip.name}") |
| 340 | if on_clip_start: |
| 341 | on_clip_start(clip.name, clip.input_asset.frame_count) |
| 342 | |
| 343 | alpha_output_dir = os.path.join(clip.root_path, "AlphaHint") |
| 344 | os.makedirs(alpha_output_dir, exist_ok=True) |
| 345 | |
| 346 | try: |
| 347 | handler.process( |
| 348 | input_path=clip.input_asset.path, |
| 349 | alpha_output_dir=alpha_output_dir, |
| 350 | dilate_radius=dilate_radius, |
| 351 | on_frame_complete=on_frame_complete, |
| 352 | ) |
| 353 | logger.info(f"BiRefNet complete for {clip.name}") |
| 354 | except Exception as e: |
| 355 | logger.error(f"BiRefNet failed for {clip.name}: {e}") |
| 356 | import traceback |
| 357 | |
| 358 | traceback.print_exc() |
| 359 | |
| 360 | finally: |
| 361 | handler.cleanup() |
| 362 | |
| 363 |
no test coverage detected