From 184aceeee39b10d74ca42fc8ae21c5ef6552302f Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 26 Sep 2025 17:50:07 +0200 Subject: [PATCH] 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. --- croppa/main.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index 6c4a3f3..1c44e1b 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -1405,6 +1405,10 @@ class VideoEditor: print(f"DEBUG: Frame {self.current_frame} has {feature_count} features") else: 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 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 if self.template_matching_enabled and self.tracking_template is not None: if self.current_display_frame is not None: - # Use the already transformed frame (crop, zoom, rotation applied) - transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame) - if transformed_frame is not None: - result = self.track_template(transformed_frame) - if result: - center_x, center_y, confidence = result - print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}") - - # Template matching returns coordinates in transformed frame space - template_offset = (center_x, center_y) + # Apply template matching to the ORIGINAL frame (before any transformations) + # This avoids infinite recursion and ensures template can find the object + result = self.track_template(self.current_display_frame) + if result: + center_x, center_y, confidence = result + print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}") + + # Template matching returns coordinates in raw frame space + template_offset = (center_x, center_y) # Calculate offset from feature tracking if enabled feature_offset = None @@ -1782,13 +1785,12 @@ class VideoEditor: return None if self.current_display_frame is not None: - # Use the already transformed frame (crop, zoom, rotation applied) - transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame) - if transformed_frame is not None: - result = self.track_template(transformed_frame) - if result: - center_x, center_y, confidence = result - return (center_x, center_y, confidence) + # Apply template matching to the ORIGINAL frame (before any transformations) + # This avoids infinite recursion and ensures template can find the object + result = self.track_template(self.current_display_frame) + if result: + center_x, center_y, confidence = result + return (center_x, center_y, confidence) return None