Candle commited on
Commit ·
0d35102
1
Parent(s): 04f0973
stuff
Browse filesFiles changed (3)
- docs/video_loop_segment_detection.mdAdded+43
- loop_labeler_ui.pyAdded+148
- requirements.txt+2-1
12345678910111213141516171819202122232425262728293031323334353637383940414243# Video Loop Segment Detection
## Problem Definition
Given a video (or animation), the goal is to automatically detect loopable segments, defined by start and end frame indices, such that the segment can be played repeatedly with minimal visible discontinuity at the loop point.
### Inputs- Video file or sequence of frames- (Optional) Preprocessing: downscaling, grayscale conversion, etc.
### Outputs- List of candidate loop segments: each with start index, end index, and a score indicating loop quality
## Approach
### 1. Similarity-Based Loop Detection- For each possible segment (start, end): - Compute similarity between the start and end frames (e.g., MSE, cosine similarity, SSIM) - Optionally, use motion energy or perceptual features to adjust similarity - Apply a length penalty to discourage very short or very long loops - Rank candidates by a combined score
### 2. Human Feedback- Present detected loop segments to a human annotator- Allow manual review, correction, and annotation of loop quality- Use feedback to refine detection or train supervised models
## Evaluation- Quantitative: similarity score, motion energy, length penalty- Qualitative: human annotation (good/bad, corrected indices)
## Related Work- "Seamless Video Looping" (Liao et al., SIGGRAPH 2013): Patch-based optimization for seamless loops- Deep features (CNN, ViT, CLIP) can be used for perceptual similarity, but no SOTA end-to-end loop detector exists as of 2025
## Future Directions- Use deep video features for similarity- Train supervised models with human-annotated loop segments- Explore generative models for loop synthesis
## References- Liao, S., et al. "Seamless Video Looping." SIGGRAPH 2013.- Other recent works on video similarity, perceptual metrics, and generative video models123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148import sysimport jsonfrom pathlib import Pathfrom PIL import Image, ImageQtfrom PyQt5.QtWidgets import ( QApplication, QWidget, QLabel, QPushButton, QSlider, QHBoxLayout, QVBoxLayout, QFileDialog, QListWidget, QSpinBox)from PyQt5.QtCore import Qt, QTimerfrom PyQt5.QtGui import QPixmap
class LoopLabeler(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Video Loop Segment Labeler") self.resize(900, 600) self.data_dir = Path("data/loops") self.shots_dir = Path("data/shots") self.json_files = sorted(self.data_dir.glob("*.json")) self.current_json = None self.frames = [] self.loop_candidates = [] self.current_candidate = 0 self.timer = QTimer() self.timer.timeout.connect(self.update_preview) self.preview_idx = 0 self.init_ui() if self.json_files: self.load_json(self.json_files[0])
def init_ui(self): layout = QVBoxLayout() # File selector self.file_list = QListWidget() for f in self.json_files: self.file_list.addItem(f.name) self.file_list.currentRowChanged.connect(self.on_file_selected) layout.addWidget(self.file_list) # Loop candidate selector self.candidate_spin = QSpinBox() self.candidate_spin.setMinimum(0) self.candidate_spin.valueChanged.connect(self.on_candidate_changed) layout.addWidget(QLabel("Loop candidate index:")) layout.addWidget(self.candidate_spin) # Preview self.preview_label = QLabel() self.preview_label.setFixedSize(320, 320) layout.addWidget(self.preview_label) # Sliders for start/end self.start_slider = QSlider(Qt.Orientation(1)) # Qt.Horizontal is 1 self.end_slider = QSlider(Qt.Orientation(1)) self.start_slider.valueChanged.connect(self.on_slider_changed) self.end_slider.valueChanged.connect(self.on_slider_changed) layout.addWidget(QLabel("Start index")) layout.addWidget(self.start_slider) layout.addWidget(QLabel("End index")) layout.addWidget(self.end_slider) # Save button self.save_btn = QPushButton("Save annotation") self.save_btn.clicked.connect(self.save_annotation) layout.addWidget(self.save_btn) self.setLayout(layout)
def load_json(self, json_path): self.current_json = json_path with open(json_path) as f: self.loop_candidates = json.load(f) self.candidate_spin.setMaximum(len(self.loop_candidates)-1) self.current_candidate = 0 self.candidate_spin.setValue(0) # Find corresponding webp stem = json_path.name.split(".")[0] webp_path = self.shots_dir / f"{stem}.webp" self.frames = [] if webp_path.exists(): with Image.open(webp_path) as im: try: while True: self.frames.append(im.convert("RGB")) im.seek(im.tell() + 1) except EOFError: pass self.update_candidate()
def on_file_selected(self, idx): if idx >= 0 and idx < len(self.json_files): self.load_json(self.json_files[idx])
def on_candidate_changed(self, idx): self.current_candidate = idx self.update_candidate()
def update_candidate(self): candidate = self.loop_candidates[self.current_candidate] start = candidate["start"] end = candidate["end"] self.start_slider.setMinimum(0) self.start_slider.setMaximum(len(self.frames)-2) self.end_slider.setMinimum(1) self.end_slider.setMaximum(len(self.frames)-1) self.start_slider.setValue(start) self.end_slider.setValue(end) self.preview_idx = 0 self.timer.start(40)
def on_slider_changed(self): start = self.start_slider.value() end = self.end_slider.value() if start >= end: end = start + 1 self.end_slider.setValue(end) self.preview_idx = 0
def update_preview(self): start = self.start_slider.value() end = self.end_slider.value() if self.frames and end > start: frame = self.frames[start + (self.preview_idx % (end-start))] import numpy as np import cv2 frame_resized = frame.resize((320,320)) arr = np.array(frame_resized) # Convert RGB to BGR for OpenCV, then to QImage arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR) h, w, ch = arr.shape bytes_per_line = ch * w from PyQt5.QtGui import QImage qimg = QImage(arr.data, w, h, bytes_per_line, QImage.Format_BGR888) pixmap = QPixmap.fromImage(qimg) self.preview_label.setPixmap(pixmap) self.preview_idx += 1
def save_annotation(self): candidate = self.loop_candidates[self.current_candidate] candidate["start"] = self.start_slider.value() candidate["end"] = self.end_slider.value() # Save to new file (or overwrite) if self.current_json is not None: out_path = self.current_json.with_suffix(".hf.json") with open(out_path, "w") as f: json.dump(self.loop_candidates, f, indent=2) else: print("No JSON file loaded. Cannot save annotation.")
if __name__ == "__main__": app = QApplication(sys.argv) win = LoopLabeler() win.show() sys.exit(app.exec_())3 unmodified lines45673 unmodified linesmatplotlibstreamlitopencv-pythonscikit-image3 unmodified lines456783 unmodified linesmatplotlibstreamlitopencv-pythonscikit-imagepyqt5
docs/video_loop_segment_detection.mdAdded+43
loop_labeler_ui.pyAdded+148
requirements.txt+2-1