From 048e8ef033706aceead43cf3348b3279e7dea332 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 26 Sep 2025 14:31:35 +0200 Subject: [PATCH] Refactor template management in VideoEditor to improve tracking functionality This commit removes the direct storage of the tracking template and introduces a method to recreate the template from the specified region when needed. It enhances the template tracking logic by ensuring that the template is dynamically generated based on the current frame and region, improving accuracy and usability. Debug messages have been added to assist in tracking the template recreation process. --- croppa/main.py | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index ef8ae80..0cd425c 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -931,7 +931,6 @@ class VideoEditor: 'tracking_points': {str(k): v for k, v in self.tracking_points.items()}, 'feature_tracker': self.feature_tracker.get_state_dict(), 'template_matching_enabled': self.template_matching_enabled, - 'tracking_template': self.tracking_template, 'template_region': self.template_region } @@ -1023,10 +1022,10 @@ class VideoEditor: # Load template matching state if 'template_matching_enabled' in state: self.template_matching_enabled = state['template_matching_enabled'] - if 'tracking_template' in state and state['tracking_template'] is not None: - self.tracking_template = state['tracking_template'] - if 'template_region' in state: + if 'template_region' in state and state['template_region'] is not None: self.template_region = state['template_region'] + # Recreate template from region when needed + self.tracking_template = None # Will be recreated on first use # Validate cut markers against current video length if self.cut_start_frame is not None and self.cut_start_frame >= self.total_frames: @@ -2146,7 +2145,11 @@ class VideoEditor: def track_template(self, frame): """Track the template in the current frame""" if self.tracking_template is None: - return None + # Try to recreate template from saved region + if self.template_region is not None: + self._recreate_template_from_region(frame) + if self.tracking_template is None: + return None try: # Convert to grayscale @@ -2178,6 +2181,37 @@ class VideoEditor: print(f"Error in template tracking: {e}") return None + def _recreate_template_from_region(self, frame): + """Recreate template from saved region coordinates""" + try: + if self.template_region is None: + return False + + x, y, w, h = self.template_region + print(f"DEBUG: Recreating template from region ({x}, {y}, {w}, {h})") + + # Ensure region is within frame bounds + if (x >= 0 and y >= 0 and + x + w <= frame.shape[1] and + y + h <= frame.shape[0]): + + # Extract template from frame + template = frame[y:y+h, x:x+w] + if template.size > 0: + self.tracking_template = template.copy() + print(f"DEBUG: Template recreated with size {template.shape}") + return True + else: + print("DEBUG: Template region too small") + return False + else: + print("DEBUG: Template region outside frame bounds") + return False + + except Exception as e: + print(f"Error recreating template: {e}") + return False + def _set_template_from_region(self, screen_rect): """Set template from selected region""" x, y, w, h = screen_rect