feature to mask
This commit is contained in:
@@ -771,10 +771,23 @@ class FeatureToMask(FlexExternalModulator):
|
||||
height = feature.height
|
||||
width = feature.width
|
||||
|
||||
# Get all normalized values at once
|
||||
# Get normalized data as numpy array
|
||||
normalized_data = feature.get_normalized_data()
|
||||
if normalized_data is None:
|
||||
# If no data available, use frame-by-frame normalization
|
||||
normalized_data = np.array([feature.get_value_at_frame(i) for i in range(feature.frame_count)])
|
||||
if len(normalized_data) > 0:
|
||||
min_val = np.min(normalized_data)
|
||||
max_val = np.max(normalized_data)
|
||||
if max_val > min_val:
|
||||
normalized_data = (normalized_data - min_val) / (max_val - min_val)
|
||||
else:
|
||||
normalized_data = np.zeros_like(normalized_data)
|
||||
|
||||
# Convert to torch tensor and reshape for broadcasting
|
||||
normalized_tensor = torch.from_numpy(normalized_data).float()
|
||||
|
||||
# Create masks for all frames at once by expanding the normalized data
|
||||
masks_out = normalized_data.unsqueeze(-1).unsqueeze(-1).expand(-1, height, width)
|
||||
masks_out = normalized_tensor.unsqueeze(-1).unsqueeze(-1).expand(-1, height, width)
|
||||
return (masks_out,)
|
||||
|
||||
|
||||
@@ -80,14 +80,23 @@ class FlexMaskBase(FlexBase, MaskBase):
|
||||
mask_strength=1.0, invert=False, subtract_original=0.0,
|
||||
grow_with_blur=0.0, feature_param=None, feature_mode="relative", **kwargs):
|
||||
"""Main entry point for the Flex system."""
|
||||
num_frames = masks.shape[0]
|
||||
if opt_feature is not None:
|
||||
num_frames = opt_feature.frame_count
|
||||
else:
|
||||
# Start with number of input frames
|
||||
num_frames = masks.shape[0]
|
||||
# Check all parameters for lists/arrays that might be longer
|
||||
for value in kwargs.values():
|
||||
if isinstance(value, (list, tuple, np.ndarray)):
|
||||
num_frames = max(num_frames, len(value))
|
||||
|
||||
original_masks = masks.clone()
|
||||
|
||||
self.start_progress(num_frames, desc=f"Applying {self.__class__.__name__}")
|
||||
|
||||
result = []
|
||||
for i in range(num_frames):
|
||||
mask = masks[i].numpy()
|
||||
mask = masks[i % masks.shape[0]].numpy()
|
||||
|
||||
# Get feature value
|
||||
feature_value = self.get_feature_value(i, opt_feature)
|
||||
@@ -118,11 +127,10 @@ class FlexMaskBase(FlexBase, MaskBase):
|
||||
**processed_kwargs
|
||||
)
|
||||
|
||||
|
||||
# Apply mask operations
|
||||
# Apply mask operations using modulo for original mask indexing
|
||||
frame_result = self.apply_mask_operation(
|
||||
processed_mask[np.newaxis, ...],
|
||||
original_masks[i:i+1],
|
||||
original_masks[i % masks.shape[0]:i % masks.shape[0]+1],
|
||||
processed_kwargs['mask_strength'],
|
||||
invert,
|
||||
processed_kwargs['subtract_original'],
|
||||
|
||||
Reference in New Issue
Block a user