Candle commited on
Commit ·
54dde4f
1
Parent(s): d6d7b2c
slider stuff
Browse files- loop_labeler_ui.py +277 -62
loop_labeler_ui.py
CHANGED
|
@@ -15,7 +15,7 @@ class LoopLabeler(QWidget):
|
|
| 15 |
self.resize(900, 600)
|
| 16 |
self.data_dir = Path("data/loops")
|
| 17 |
self.shots_dir = Path("data/shots")
|
| 18 |
-
self.json_files = sorted(self.data_dir.glob("*.json"))
|
| 19 |
self.current_json = None
|
| 20 |
self.frames = []
|
| 21 |
self.loop_candidates = []
|
|
@@ -24,15 +24,21 @@ class LoopLabeler(QWidget):
|
|
| 24 |
self.timer.timeout.connect(self.update_preview)
|
| 25 |
self.preview_idx = 0
|
| 26 |
self.init_ui()
|
| 27 |
-
if
|
| 28 |
-
|
| 29 |
|
| 30 |
def init_ui(self):
|
| 31 |
layout = QVBoxLayout()
|
| 32 |
# File selector
|
| 33 |
self.file_list = QListWidget()
|
| 34 |
-
|
|
|
|
| 35 |
self.file_list.addItem(f.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
self.file_list.currentRowChanged.connect(self.on_file_selected)
|
| 37 |
layout.addWidget(self.file_list)
|
| 38 |
# Loop candidate selector
|
|
@@ -48,6 +54,16 @@ class LoopLabeler(QWidget):
|
|
| 48 |
# Sliders for start/end
|
| 49 |
self.start_slider = QSlider(Qt.Orientation(1)) # Qt.Horizontal is 1
|
| 50 |
self.end_slider = QSlider(Qt.Orientation(1))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
self.start_slider.valueChanged.connect(self.on_slider_changed)
|
| 52 |
self.end_slider.valueChanged.connect(self.on_slider_changed)
|
| 53 |
layout.addWidget(QLabel("Start index"))
|
|
@@ -69,30 +85,102 @@ class LoopLabeler(QWidget):
|
|
| 69 |
layout.addWidget(self.save_btn)
|
| 70 |
self.setLayout(layout)
|
| 71 |
def revert_to_detected(self):
|
| 72 |
-
|
| 73 |
-
self.
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def load_json(self, json_path):
|
| 78 |
self.current_json = json_path
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
self.current_candidate = 0
|
| 83 |
self.candidate_spin.setValue(0)
|
|
|
|
| 84 |
# Find corresponding webp
|
| 85 |
stem = json_path.name.split(".")[0]
|
| 86 |
webp_path = self.shots_dir / f"{stem}.webp"
|
| 87 |
self.frames = []
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
self.update_candidate()
|
| 97 |
|
| 98 |
def on_file_selected(self, idx):
|
|
@@ -100,23 +188,62 @@ class LoopLabeler(QWidget):
|
|
| 100 |
self.load_json(self.json_files[idx])
|
| 101 |
|
| 102 |
def on_candidate_changed(self, idx):
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
def update_candidate(self):
|
| 107 |
-
candidate = self.loop_candidates[self.current_candidate]
|
| 108 |
-
start = candidate["start"]
|
| 109 |
-
end = candidate["end"]
|
| 110 |
nframes = len(self.frames)
|
| 111 |
# Set both sliders to cover full range from 0 to N
|
| 112 |
self.start_slider.setMinimum(0)
|
| 113 |
self.start_slider.setMaximum(nframes-1 if nframes > 0 else 0)
|
| 114 |
self.end_slider.setMinimum(0)
|
| 115 |
self.end_slider.setMaximum(nframes-1 if nframes > 0 else 0)
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
self.preview_idx = 0
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
def on_slider_changed(self):
|
| 122 |
start = self.start_slider.value()
|
|
@@ -126,14 +253,33 @@ class LoopLabeler(QWidget):
|
|
| 126 |
if end <= start:
|
| 127 |
# For preview purposes, we'll adjust temporarily in the update_preview method
|
| 128 |
pass
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
def update_preview(self):
|
| 132 |
start = self.start_slider.value()
|
| 133 |
end = self.end_slider.value()
|
| 134 |
|
| 135 |
# Handle case where end ≤ start by showing a single frame or appropriate range
|
| 136 |
-
if self.frames:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
if end <= start:
|
| 138 |
# Show just the start frame when end <= start
|
| 139 |
frame_idx = start
|
|
@@ -144,51 +290,120 @@ class LoopLabeler(QWidget):
|
|
| 144 |
frame_idx = start + (self.preview_idx % (end-start))
|
| 145 |
frame = self.frames[frame_idx]
|
| 146 |
show_progress = True
|
| 147 |
-
|
|
|
|
| 148 |
import numpy as np
|
|
|
|
| 149 |
import cv2
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# Convert RGB to BGR for OpenCV, then to QImage
|
| 167 |
-
arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
|
| 168 |
-
h, w, ch = arr.shape
|
| 169 |
-
bytes_per_line = ch * w
|
| 170 |
-
from PyQt5.QtGui import QImage
|
| 171 |
-
qimg = QImage(arr.data, w, h, bytes_per_line, QImage.Format_BGR888)
|
| 172 |
-
pixmap = QPixmap.fromImage(qimg)
|
| 173 |
-
self.preview_label.setPixmap(pixmap)
|
| 174 |
-
self.preview_idx += 1
|
| 175 |
|
| 176 |
def save_annotation(self):
|
| 177 |
-
candidate = self.loop_candidates[self.current_candidate]
|
| 178 |
start = self.start_slider.value()
|
| 179 |
end = self.end_slider.value()
|
| 180 |
is_loop = self.loop_checkbox.isChecked()
|
|
|
|
| 181 |
# Save to new file (or overwrite)
|
| 182 |
if self.current_json is not None:
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
else:
|
| 191 |
print("No JSON file loaded. Cannot save annotation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|
| 194 |
app = QApplication(sys.argv)
|
|
|
|
| 15 |
self.resize(900, 600)
|
| 16 |
self.data_dir = Path("data/loops")
|
| 17 |
self.shots_dir = Path("data/shots")
|
| 18 |
+
self.json_files = sorted(self.data_dir.glob("*.loop.json"))
|
| 19 |
self.current_json = None
|
| 20 |
self.frames = []
|
| 21 |
self.loop_candidates = []
|
|
|
|
| 24 |
self.timer.timeout.connect(self.update_preview)
|
| 25 |
self.preview_idx = 0
|
| 26 |
self.init_ui()
|
| 27 |
+
# Load the first unannotated file if available
|
| 28 |
+
self.load_first_unannotated()
|
| 29 |
|
| 30 |
def init_ui(self):
|
| 31 |
layout = QVBoxLayout()
|
| 32 |
# File selector
|
| 33 |
self.file_list = QListWidget()
|
| 34 |
+
from PyQt5.QtGui import QColor, QBrush
|
| 35 |
+
for i, f in enumerate(self.json_files):
|
| 36 |
self.file_list.addItem(f.name)
|
| 37 |
+
# Visual indicator for annotated files
|
| 38 |
+
if self.is_annotated(f):
|
| 39 |
+
item = self.file_list.item(i)
|
| 40 |
+
if item: # Ensure item exists
|
| 41 |
+
item.setForeground(QBrush(QColor(0, 120, 0))) # Dark green text for annotated
|
| 42 |
self.file_list.currentRowChanged.connect(self.on_file_selected)
|
| 43 |
layout.addWidget(self.file_list)
|
| 44 |
# Loop candidate selector
|
|
|
|
| 54 |
# Sliders for start/end
|
| 55 |
self.start_slider = QSlider(Qt.Orientation(1)) # Qt.Horizontal is 1
|
| 56 |
self.end_slider = QSlider(Qt.Orientation(1))
|
| 57 |
+
|
| 58 |
+
# Configure sliders to prevent jumpy behavior
|
| 59 |
+
for slider in [self.start_slider, self.end_slider]:
|
| 60 |
+
slider.setPageStep(1) # Smaller page step for smoother dragging
|
| 61 |
+
slider.setTracking(True) # Enable continuous tracking during drag
|
| 62 |
+
|
| 63 |
+
# Initialize tracking variables for smoother dragging
|
| 64 |
+
self.last_start = 0
|
| 65 |
+
self.last_end = 0
|
| 66 |
+
|
| 67 |
self.start_slider.valueChanged.connect(self.on_slider_changed)
|
| 68 |
self.end_slider.valueChanged.connect(self.on_slider_changed)
|
| 69 |
layout.addWidget(QLabel("Start index"))
|
|
|
|
| 85 |
layout.addWidget(self.save_btn)
|
| 86 |
self.setLayout(layout)
|
| 87 |
def revert_to_detected(self):
|
| 88 |
+
# Check if we have valid candidates and index
|
| 89 |
+
if self.loop_candidates and 0 <= self.current_candidate < len(self.loop_candidates):
|
| 90 |
+
candidate = self.loop_candidates[self.current_candidate]
|
| 91 |
+
|
| 92 |
+
# Check if candidate has the required fields
|
| 93 |
+
if "start" in candidate and "end" in candidate:
|
| 94 |
+
# Get values from candidate
|
| 95 |
+
start_value = int(candidate["start"])
|
| 96 |
+
end_value = int(candidate["end"])
|
| 97 |
+
|
| 98 |
+
# Ensure values are within valid range
|
| 99 |
+
nframes = len(self.frames)
|
| 100 |
+
start_value = max(0, min(start_value, nframes-1 if nframes > 0 else 0))
|
| 101 |
+
end_value = max(0, min(end_value, nframes-1 if nframes > 0 else 0))
|
| 102 |
+
|
| 103 |
+
# Block signals temporarily to avoid triggering callbacks
|
| 104 |
+
self.start_slider.blockSignals(True)
|
| 105 |
+
self.end_slider.blockSignals(True)
|
| 106 |
+
|
| 107 |
+
# Set the values
|
| 108 |
+
self.end_slider.setValue(end_value)
|
| 109 |
+
self.start_slider.setValue(start_value)
|
| 110 |
+
|
| 111 |
+
# Re-enable signals
|
| 112 |
+
self.start_slider.blockSignals(False)
|
| 113 |
+
self.end_slider.blockSignals(False)
|
| 114 |
+
|
| 115 |
+
# Update the last known values to match reverted positions
|
| 116 |
+
self.last_start = start_value
|
| 117 |
+
self.last_end = end_value
|
| 118 |
+
|
| 119 |
+
# Mark as loop
|
| 120 |
+
self.loop_checkbox.setChecked(True)
|
| 121 |
+
|
| 122 |
+
# Reset preview
|
| 123 |
+
self.preview_idx = 0
|
| 124 |
+
|
| 125 |
+
# Manually trigger the slider changed callback to ensure UI updates properly
|
| 126 |
+
self.on_slider_changed()
|
| 127 |
+
else:
|
| 128 |
+
print("Cannot revert: candidate missing start/end fields")
|
| 129 |
+
else:
|
| 130 |
+
print("Cannot revert: no valid loop candidate")
|
| 131 |
+
# Set default values
|
| 132 |
+
nframes = len(self.frames)
|
| 133 |
+
self.start_slider.setValue(0)
|
| 134 |
+
default_end = min(10, nframes-1) if nframes > 1 else 0
|
| 135 |
+
self.end_slider.setValue(default_end)
|
| 136 |
+
# Force update
|
| 137 |
+
self.start_slider.update()
|
| 138 |
+
self.end_slider.update()
|
| 139 |
|
| 140 |
def load_json(self, json_path):
|
| 141 |
self.current_json = json_path
|
| 142 |
+
try:
|
| 143 |
+
with open(json_path) as f:
|
| 144 |
+
self.loop_candidates = json.load(f)
|
| 145 |
+
|
| 146 |
+
# Validate loop_candidates is a list
|
| 147 |
+
if not isinstance(self.loop_candidates, list):
|
| 148 |
+
print(f"Warning: {json_path} does not contain a list of loop candidates")
|
| 149 |
+
self.loop_candidates = []
|
| 150 |
+
|
| 151 |
+
except (json.JSONDecodeError, FileNotFoundError, PermissionError) as e:
|
| 152 |
+
print(f"Error loading JSON file {json_path}: {e}")
|
| 153 |
+
self.loop_candidates = []
|
| 154 |
+
|
| 155 |
+
# Update candidate spinner
|
| 156 |
+
max_candidate = max(0, len(self.loop_candidates)-1)
|
| 157 |
+
self.candidate_spin.setMaximum(max_candidate)
|
| 158 |
self.current_candidate = 0
|
| 159 |
self.candidate_spin.setValue(0)
|
| 160 |
+
|
| 161 |
# Find corresponding webp
|
| 162 |
stem = json_path.name.split(".")[0]
|
| 163 |
webp_path = self.shots_dir / f"{stem}.webp"
|
| 164 |
self.frames = []
|
| 165 |
+
|
| 166 |
+
try:
|
| 167 |
+
if webp_path.exists():
|
| 168 |
+
with Image.open(webp_path) as im:
|
| 169 |
+
try:
|
| 170 |
+
while True:
|
| 171 |
+
self.frames.append(im.convert("RGB"))
|
| 172 |
+
im.seek(im.tell() + 1)
|
| 173 |
+
except EOFError:
|
| 174 |
+
pass
|
| 175 |
+
else:
|
| 176 |
+
print(f"Warning: WebP file not found: {webp_path}")
|
| 177 |
+
except Exception as e:
|
| 178 |
+
print(f"Error loading WebP file {webp_path}: {e}")
|
| 179 |
+
|
| 180 |
+
# If no frames were loaded, ensure there's at least an empty frame
|
| 181 |
+
if not self.frames:
|
| 182 |
+
print(f"No frames found in {webp_path}")
|
| 183 |
+
|
| 184 |
self.update_candidate()
|
| 185 |
|
| 186 |
def on_file_selected(self, idx):
|
|
|
|
| 188 |
self.load_json(self.json_files[idx])
|
| 189 |
|
| 190 |
def on_candidate_changed(self, idx):
|
| 191 |
+
if 0 <= idx < len(self.loop_candidates):
|
| 192 |
+
self.current_candidate = idx
|
| 193 |
+
self.update_candidate()
|
| 194 |
+
else:
|
| 195 |
+
self.current_candidate = 0
|
| 196 |
|
| 197 |
def update_candidate(self):
|
|
|
|
|
|
|
|
|
|
| 198 |
nframes = len(self.frames)
|
| 199 |
# Set both sliders to cover full range from 0 to N
|
| 200 |
self.start_slider.setMinimum(0)
|
| 201 |
self.start_slider.setMaximum(nframes-1 if nframes > 0 else 0)
|
| 202 |
self.end_slider.setMinimum(0)
|
| 203 |
self.end_slider.setMaximum(nframes-1 if nframes > 0 else 0)
|
| 204 |
+
|
| 205 |
+
# Block signals temporarily to avoid triggering callbacks multiple times
|
| 206 |
+
self.start_slider.blockSignals(True)
|
| 207 |
+
self.end_slider.blockSignals(True)
|
| 208 |
+
|
| 209 |
+
try:
|
| 210 |
+
# Check if there are loop candidates and if the current index is valid
|
| 211 |
+
if self.loop_candidates and self.current_candidate < len(self.loop_candidates):
|
| 212 |
+
candidate = self.loop_candidates[self.current_candidate]
|
| 213 |
+
# Check if candidate has the required keys
|
| 214 |
+
if "start" in candidate and "end" in candidate:
|
| 215 |
+
start = int(candidate["start"])
|
| 216 |
+
end = int(candidate["end"])
|
| 217 |
+
# Make sure start/end are within valid range
|
| 218 |
+
start = max(0, min(start, nframes-1 if nframes > 0 else 0))
|
| 219 |
+
end = max(0, min(end, nframes-1 if nframes > 0 else 0))
|
| 220 |
+
|
| 221 |
+
self.start_slider.setValue(start)
|
| 222 |
+
self.end_slider.setValue(end)
|
| 223 |
+
else:
|
| 224 |
+
# Default values if no candidates or invalid index
|
| 225 |
+
self.start_slider.setValue(0)
|
| 226 |
+
default_end = min(10, nframes-1) if nframes > 1 else 0
|
| 227 |
+
self.end_slider.setValue(default_end)
|
| 228 |
+
finally:
|
| 229 |
+
# Always re-enable signals
|
| 230 |
+
self.start_slider.blockSignals(False)
|
| 231 |
+
self.end_slider.blockSignals(False)
|
| 232 |
+
|
| 233 |
+
# Force UI update
|
| 234 |
+
self.start_slider.update()
|
| 235 |
+
self.end_slider.update()
|
| 236 |
+
|
| 237 |
+
# Reset animation
|
| 238 |
self.preview_idx = 0
|
| 239 |
+
|
| 240 |
+
# Update the last known values to match current slider positions
|
| 241 |
+
self.last_start = self.start_slider.value()
|
| 242 |
+
self.last_end = self.end_slider.value()
|
| 243 |
+
|
| 244 |
+
# Make sure preview animation is running
|
| 245 |
+
if not self.timer.isActive():
|
| 246 |
+
self.timer.start(40)
|
| 247 |
|
| 248 |
def on_slider_changed(self):
|
| 249 |
start = self.start_slider.value()
|
|
|
|
| 253 |
if end <= start:
|
| 254 |
# For preview purposes, we'll adjust temporarily in the update_preview method
|
| 255 |
pass
|
| 256 |
+
|
| 257 |
+
# Only reset animation index when the range actually changes,
|
| 258 |
+
# not during continuous dragging of a single slider
|
| 259 |
+
if not hasattr(self, 'last_start') or not hasattr(self, 'last_end') or \
|
| 260 |
+
self.last_start != start or self.last_end != end:
|
| 261 |
+
self.preview_idx = 0
|
| 262 |
+
self.last_start = start
|
| 263 |
+
self.last_end = end
|
| 264 |
+
|
| 265 |
+
# Don't update the sliders directly during value change events
|
| 266 |
+
# as this can cause jumping behavior
|
| 267 |
+
|
| 268 |
+
# Make sure preview gets updated immediately
|
| 269 |
+
if not self.timer.isActive():
|
| 270 |
+
self.timer.start(40)
|
| 271 |
|
| 272 |
def update_preview(self):
|
| 273 |
start = self.start_slider.value()
|
| 274 |
end = self.end_slider.value()
|
| 275 |
|
| 276 |
# Handle case where end ≤ start by showing a single frame or appropriate range
|
| 277 |
+
if self.frames and len(self.frames) > 0:
|
| 278 |
+
# Make sure start and end are within valid ranges
|
| 279 |
+
max_idx = len(self.frames) - 1
|
| 280 |
+
start = max(0, min(start, max_idx))
|
| 281 |
+
end = max(0, min(end, max_idx))
|
| 282 |
+
|
| 283 |
if end <= start:
|
| 284 |
# Show just the start frame when end <= start
|
| 285 |
frame_idx = start
|
|
|
|
| 290 |
frame_idx = start + (self.preview_idx % (end-start))
|
| 291 |
frame = self.frames[frame_idx]
|
| 292 |
show_progress = True
|
| 293 |
+
else:
|
| 294 |
+
# Display an empty/blank preview if no frames
|
| 295 |
import numpy as np
|
| 296 |
+
blank_frame = np.ones((320, 320, 3), dtype=np.uint8) * 200 # Light gray
|
| 297 |
import cv2
|
| 298 |
+
from PIL import Image
|
| 299 |
+
frame = Image.fromarray(cv2.cvtColor(blank_frame, cv2.COLOR_BGR2RGB))
|
| 300 |
+
show_progress = False
|
| 301 |
+
|
| 302 |
+
# Process and display the frame (shared code for both cases)
|
| 303 |
+
import numpy as np
|
| 304 |
+
import cv2
|
| 305 |
+
frame_resized = frame.resize((320,320))
|
| 306 |
+
arr = np.array(frame_resized)
|
| 307 |
+
# Draw progress indicator line at bottom
|
| 308 |
+
indicator_height = 2
|
| 309 |
+
|
| 310 |
+
# Only show progress bar when we have a valid range
|
| 311 |
+
if show_progress:
|
| 312 |
+
total = end - start
|
| 313 |
+
pos = self.preview_idx % total
|
| 314 |
+
bar_w = int(320 * (pos / max(total-1, 1)))
|
| 315 |
+
else:
|
| 316 |
+
# Just show a full bar for single frame
|
| 317 |
+
bar_w = 320
|
| 318 |
|
| 319 |
+
arr[-indicator_height:, :320] = [220, 220, 220] # light gray background
|
| 320 |
+
arr[-indicator_height:, :bar_w] = [100, 100, 255] # red progress
|
| 321 |
+
# Convert RGB to BGR for OpenCV, then to QImage
|
| 322 |
+
arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
|
| 323 |
+
h, w, ch = arr.shape
|
| 324 |
+
bytes_per_line = ch * w
|
| 325 |
+
from PyQt5.QtGui import QImage
|
| 326 |
+
qimg = QImage(arr.data, w, h, bytes_per_line, QImage.Format_BGR888)
|
| 327 |
+
pixmap = QPixmap.fromImage(qimg)
|
| 328 |
+
self.preview_label.setPixmap(pixmap)
|
| 329 |
+
self.preview_idx += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
def save_annotation(self):
|
|
|
|
| 332 |
start = self.start_slider.value()
|
| 333 |
end = self.end_slider.value()
|
| 334 |
is_loop = self.loop_checkbox.isChecked()
|
| 335 |
+
|
| 336 |
# Save to new file (or overwrite)
|
| 337 |
if self.current_json is not None:
|
| 338 |
+
try:
|
| 339 |
+
out_path = self.current_json.with_suffix(".hf.json")
|
| 340 |
+
hf_data = {
|
| 341 |
+
"loop": bool(is_loop),
|
| 342 |
+
"best_cut": [start, end]
|
| 343 |
+
}
|
| 344 |
+
with open(out_path, "w") as f:
|
| 345 |
+
json.dump(hf_data, f, indent=2)
|
| 346 |
+
print(f"Annotation saved to {out_path}")
|
| 347 |
+
|
| 348 |
+
# After saving, load the next unannotated file
|
| 349 |
+
self.load_next_unannotated()
|
| 350 |
+
except Exception as e:
|
| 351 |
+
print(f"Error saving annotation: {e}")
|
| 352 |
else:
|
| 353 |
print("No JSON file loaded. Cannot save annotation.")
|
| 354 |
+
|
| 355 |
+
def is_annotated(self, json_file):
|
| 356 |
+
"""Check if a JSON file has a corresponding human feedback (.hf.json) file"""
|
| 357 |
+
hf_file = json_file.with_suffix(".hf.json")
|
| 358 |
+
return hf_file.exists()
|
| 359 |
+
|
| 360 |
+
def find_first_unannotated(self):
|
| 361 |
+
"""Find the index of the first unannotated JSON file"""
|
| 362 |
+
for idx, json_file in enumerate(self.json_files):
|
| 363 |
+
if not self.is_annotated(json_file):
|
| 364 |
+
return idx
|
| 365 |
+
# If all files are annotated, return 0
|
| 366 |
+
return 0 if self.json_files else None
|
| 367 |
+
|
| 368 |
+
def find_next_unannotated(self, current_idx):
|
| 369 |
+
"""Find the index of the next unannotated JSON file after current_idx"""
|
| 370 |
+
if current_idx is None:
|
| 371 |
+
return self.find_first_unannotated()
|
| 372 |
+
|
| 373 |
+
# Start from the next file
|
| 374 |
+
for idx in range(current_idx + 1, len(self.json_files)):
|
| 375 |
+
if not self.is_annotated(self.json_files[idx]):
|
| 376 |
+
return idx
|
| 377 |
+
|
| 378 |
+
# If no more unannotated files after current, wrap around to the beginning
|
| 379 |
+
for idx in range(0, current_idx + 1):
|
| 380 |
+
if not self.is_annotated(self.json_files[idx]):
|
| 381 |
+
return idx
|
| 382 |
+
|
| 383 |
+
# If all files are annotated, return the current index
|
| 384 |
+
return current_idx
|
| 385 |
+
|
| 386 |
+
def load_first_unannotated(self):
|
| 387 |
+
"""Load the first unannotated JSON file in the list"""
|
| 388 |
+
if not self.json_files:
|
| 389 |
+
return
|
| 390 |
+
|
| 391 |
+
idx = self.find_first_unannotated()
|
| 392 |
+
if idx is not None:
|
| 393 |
+
self.file_list.setCurrentRow(idx)
|
| 394 |
+
self.load_json(self.json_files[idx])
|
| 395 |
+
|
| 396 |
+
def load_next_unannotated(self):
|
| 397 |
+
"""Load the next unannotated JSON file after the current one"""
|
| 398 |
+
if not self.json_files or self.current_json is None:
|
| 399 |
+
return
|
| 400 |
+
|
| 401 |
+
current_idx = self.json_files.index(self.current_json)
|
| 402 |
+
next_idx = self.find_next_unannotated(current_idx)
|
| 403 |
+
|
| 404 |
+
if next_idx is not None and next_idx != current_idx:
|
| 405 |
+
self.file_list.setCurrentRow(next_idx)
|
| 406 |
+
self.load_json(self.json_files[next_idx])
|
| 407 |
|
| 408 |
if __name__ == "__main__":
|
| 409 |
app = QApplication(sys.argv)
|