Refactor template management in VideoEditor to enhance tracking functionality

This commit removes the initial template selection logic from the tracking method and introduces a mechanism to disable template matching when no templates are available. Additionally, it updates the comments to clarify the enabling of template matching upon template creation, improving the overall management of templates during video editing sessions.
This commit is contained in:
2025-09-26 17:42:30 +02:00
parent f5b8656bc2
commit 86c31a49d9

View File

@@ -2252,10 +2252,6 @@ class VideoEditor:
def track_template(self, frame): def track_template(self, frame):
"""Track the template in the current frame""" """Track the template in the current frame"""
# First, try to select the best template for current frame
if not self._select_best_template_for_frame(self.current_frame):
return None
if self.tracking_template is None: if self.tracking_template is None:
return None return None
@@ -2444,10 +2440,11 @@ class VideoEditor:
'end_frame': end_frame 'end_frame': end_frame
} }
# Set as current template # Set as current template and enable template matching
self.current_template_id = template_id self.current_template_id = template_id
self.tracking_template = template.copy() self.tracking_template = template.copy()
self.template_region = region self.template_region = region
self.template_matching_enabled = True # Enable template matching when template is created
self.show_feedback_message(f"Template {template_id} added (frames {start_frame}-{end_frame})") self.show_feedback_message(f"Template {template_id} added (frames {start_frame}-{end_frame})")
return template_id return template_id
@@ -2460,6 +2457,13 @@ class VideoEditor:
# If we removed the current template, find a new one # If we removed the current template, find a new one
if self.current_template_id == template_id: if self.current_template_id == template_id:
self._select_best_template_for_frame(self.current_frame) self._select_best_template_for_frame(self.current_frame)
# If no templates left, disable template matching
if not self.templates:
self.template_matching_enabled = False
self.tracking_template = None
self.template_region = None
self.current_template_id = None
self.show_feedback_message(f"Template {template_id} removed") self.show_feedback_message(f"Template {template_id} removed")
return True return True
@@ -3308,22 +3312,6 @@ class VideoEditor:
conf_text = f"{confidence:.2f}" conf_text = f"{confidence:.2f}"
cv2.putText(canvas, conf_text, (sx + 10, sy - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) cv2.putText(canvas, conf_text, (sx + 10, sy - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# Draw template dots for all templates
if not self.is_image_mode and self.templates:
for template_id, template_data in self.templates.items():
# Only draw if this template covers the current frame
if (template_data['start_frame'] <= self.current_frame <= template_data['end_frame']):
# Get template center position from region
x, y, w, h = template_data['region']
center_x = x + w // 2
center_y = y + h // 2
# Map to screen coordinates
sx, sy = self._map_rotated_to_screen(center_x, center_y)
# Draw template dot (same style as motion tracking points)
cv2.circle(canvas, (sx, sy), 6, (0, 255, 0), -1) # Green circle
cv2.circle(canvas, (sx, sy), 6, (255, 255, 255), 1) # White border
# Draw selection rectangles for feature extraction/deletion # Draw selection rectangles for feature extraction/deletion
if self.selective_feature_extraction_rect: if self.selective_feature_extraction_rect: