initialize method

Future<bool> initialize([
  1. SegmenterConfig? config
])

Initialize the processor.

This must be called before processing frames. Automatically creates the appropriate platform-specific segmenter.

If you have a custom legacySegmenter, set it before calling initialize.

Returns true if initialization was successful, false if it failed. Failures are handled gracefully - virtual backgrounds will be disabled.

Implementation

Future<bool> initialize([SegmenterConfig? config]) async {
  try {
    // Create platform-specific segmenter via conditional import factory
    _platformSegmenter = createSegmenter();

    // Initialize the platform segmenter
    await _platformSegmenter.initialize(config ?? const SegmenterConfig());

    // Also initialize legacy segmenter if provided
    if (legacySegmenter != null) {
      try {
        await legacySegmenter!.initialize();
      } catch (e) {
        debugPrint(
            'VirtualBackgroundProcessor: Legacy segmenter init failed: $e');
      }
    }

    _isInitialized = _platformSegmenter.isReady;
    debugPrint(
        'VirtualBackgroundProcessor: Initialized with ${_platformSegmenter.platformName} (ready=$_isInitialized)');
    return _isInitialized;
  } catch (e, stack) {
    debugPrint('⚠️ VirtualBackgroundProcessor: Initialization failed: $e');
    debugPrint('Stack: $stack');
    _isInitialized = false;
    return false;
  }
}