From c1c01e86caa8fb93fe39bdb5b481e69a98e94f90 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 26 Sep 2025 17:52:09 +0200 Subject: [PATCH] Optimize template matching in VideoEditor to utilize cropped regions for improved performance This commit modifies the template matching logic in the VideoEditor to use only the cropped region of the frame when available, significantly enhancing the speed of template tracking. If no crop is applied, the original frame is used for tracking. This change improves the efficiency of the template matching process while maintaining accuracy in locating objects during video editing sessions. --- croppa/main.py | 57 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index 1c44e1b..6a919d3 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -1683,15 +1683,27 @@ 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: - # 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) + # Use only the cropped region for much faster template matching + if self.crop_rect: + crop_x, crop_y, crop_w, crop_h = self.crop_rect + # Extract only the cropped region from raw frame + cropped_frame = self.current_display_frame[crop_y:crop_y+crop_h, crop_x:crop_x+crop_w] + if cropped_frame is not None and cropped_frame.size > 0: + # Track template in cropped frame (much faster!) + result = self.track_template(cropped_frame) + if result: + center_x, center_y, confidence = result + # Map from cropped frame coordinates to raw frame coordinates + # Add crop offset back + raw_x = center_x + crop_x + raw_y = center_y + crop_y + template_offset = (raw_x, raw_y) + else: + # No crop - use full frame + result = self.track_template(self.current_display_frame) + if result: + center_x, center_y, confidence = result + template_offset = (center_x, center_y) # Calculate offset from feature tracking if enabled feature_offset = None @@ -1785,12 +1797,27 @@ class VideoEditor: return None if self.current_display_frame is not None: - # 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) + # Use only the cropped region for much faster template matching + if self.crop_rect: + crop_x, crop_y, crop_w, crop_h = self.crop_rect + # Extract only the cropped region from raw frame + cropped_frame = self.current_display_frame[crop_y:crop_y+crop_h, crop_x:crop_x+crop_w] + if cropped_frame is not None and cropped_frame.size > 0: + # Track template in cropped frame (much faster!) + result = self.track_template(cropped_frame) + if result: + center_x, center_y, confidence = result + # Map from cropped frame coordinates to raw frame coordinates + # Add crop offset back + raw_x = center_x + crop_x + raw_y = center_y + crop_y + return (raw_x, raw_y, confidence) + else: + # No crop - use full frame + result = self.track_template(self.current_display_frame) + if result: + center_x, center_y, confidence = result + return (center_x, center_y, confidence) return None