Enhance template selection and tracking logic in VideoEditor

This commit introduces a mechanism to select the best template for the current frame if templates are available. Additionally, it updates the template matching process to apply tracking directly on the original frame, avoiding infinite recursion and improving tracking accuracy. These changes enhance the overall functionality and reliability of the template management system during video editing sessions.
This commit is contained in:
2025-09-26 17:50:07 +02:00
parent db2aa57ce5
commit 184aceeee3

View File

@@ -1406,6 +1406,10 @@ class VideoEditor:
else: else:
print(f"DEBUG: Frame {self.current_frame} has NO features") print(f"DEBUG: Frame {self.current_frame} has NO features")
# Select the best template for the new frame
if self.templates:
self._select_best_template_for_frame(self.current_frame)
# Auto-extract features if feature tracking is enabled and auto-tracking is on # Auto-extract features if feature tracking is enabled and auto-tracking is on
print(f"DEBUG: seek_to_frame {frame_number}: is_image_mode={self.is_image_mode}, tracking_enabled={self.feature_tracker.tracking_enabled}, auto_tracking={self.feature_tracker.auto_tracking}, display_frame={self.current_display_frame is not None}") print(f"DEBUG: seek_to_frame {frame_number}: is_image_mode={self.is_image_mode}, tracking_enabled={self.feature_tracker.tracking_enabled}, auto_tracking={self.feature_tracker.auto_tracking}, display_frame={self.current_display_frame is not None}")
@@ -1679,16 +1683,15 @@ class VideoEditor:
template_offset = None template_offset = None
if self.template_matching_enabled and self.tracking_template is not None: if self.template_matching_enabled and self.tracking_template is not None:
if self.current_display_frame is not None: if self.current_display_frame is not None:
# Use the already transformed frame (crop, zoom, rotation applied) # Apply template matching to the ORIGINAL frame (before any transformations)
transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame) # This avoids infinite recursion and ensures template can find the object
if transformed_frame is not None: result = self.track_template(self.current_display_frame)
result = self.track_template(transformed_frame) if result:
if result: center_x, center_y, confidence = result
center_x, center_y, confidence = result print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
# Template matching returns coordinates in transformed frame space # Template matching returns coordinates in raw frame space
template_offset = (center_x, center_y) template_offset = (center_x, center_y)
# Calculate offset from feature tracking if enabled # Calculate offset from feature tracking if enabled
feature_offset = None feature_offset = None
@@ -1782,13 +1785,12 @@ class VideoEditor:
return None return None
if self.current_display_frame is not None: if self.current_display_frame is not None:
# Use the already transformed frame (crop, zoom, rotation applied) # Apply template matching to the ORIGINAL frame (before any transformations)
transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame) # This avoids infinite recursion and ensures template can find the object
if transformed_frame is not None: result = self.track_template(self.current_display_frame)
result = self.track_template(transformed_frame) if result:
if result: center_x, center_y, confidence = result
center_x, center_y, confidence = result return (center_x, center_y, confidence)
return (center_x, center_y, confidence)
return None return None