Candle commited on
Commit
c343e7a
·
1 Parent(s): ebab963
Files changed (1) hide show
  1. loop_labeler_ui.py +71 -79
loop_labeler_ui.py CHANGED
@@ -3,10 +3,11 @@ import json
3
  from pathlib import Path
4
  from PIL import Image, ImageQt
5
  from PyQt5.QtWidgets import (
6
- QApplication, QWidget, QLabel, QPushButton, QSlider, QHBoxLayout, QVBoxLayout, QFileDialog, QListWidget, QSpinBox
 
7
  )
8
  from PyQt5.QtCore import Qt, QTimer
9
- from PyQt5.QtGui import QPixmap
10
 
11
  class LoopLabeler(QWidget):
12
  def __init__(self):
@@ -35,7 +36,6 @@ class LoopLabeler(QWidget):
35
  layout = QVBoxLayout()
36
  # File selector
37
  self.file_list = QListWidget()
38
- from PyQt5.QtGui import QColor, QBrush
39
  for i, f in enumerate(self.json_files):
40
  self.file_list.addItem(f.name)
41
  # Visual indicator for annotated files
@@ -55,37 +55,35 @@ class LoopLabeler(QWidget):
55
  self.preview_label = QLabel()
56
  self.preview_label.setFixedSize(320, 320)
57
  layout.addWidget(self.preview_label)
58
- # Sliders for start/end
59
- self.start_slider = QSlider(Qt.Orientation(1)) # Qt.Horizontal is 1
60
- self.end_slider = QSlider(Qt.Orientation(1))
61
 
62
- # Configure sliders for better behavior
63
- for slider in [self.start_slider, self.end_slider]:
64
- slider.setPageStep(1) # Smaller page step for smoother dragging
65
- slider.setTracking(True) # Enable continuous tracking during drag
66
- slider.setSingleStep(1) # Set smaller step size for keyboard navigation
 
 
 
67
 
68
- # Values to track current positions (initialized to avoid reference errors)
69
- self.current_start = 0
70
- self.current_end = 0
71
- self.is_dragging = False
 
 
 
 
72
 
73
- # Use sliderPressed/Released to detect dragging state
74
- self.start_slider.sliderPressed.connect(lambda: self.on_slider_drag_started('start'))
75
- self.start_slider.sliderReleased.connect(self.on_slider_drag_ended)
76
- self.end_slider.sliderPressed.connect(lambda: self.on_slider_drag_started('end'))
77
- self.end_slider.sliderReleased.connect(self.on_slider_drag_ended)
78
 
79
- # Connect value changed signals
80
- self.start_slider.valueChanged.connect(lambda v: self.on_slider_value_changed('start', v))
81
- self.end_slider.valueChanged.connect(lambda v: self.on_slider_value_changed('end', v))
82
-
83
- layout.addWidget(QLabel("Start index"))
84
- layout.addWidget(self.start_slider)
85
- layout.addWidget(QLabel("End index"))
86
- layout.addWidget(self.end_slider)
87
  # Loop checkbox
88
- from PyQt5.QtWidgets import QCheckBox
89
  self.loop_checkbox = QCheckBox("Is loop?")
90
  self.loop_checkbox.setChecked(True)
91
  layout.addWidget(self.loop_checkbox)
@@ -115,20 +113,20 @@ class LoopLabeler(QWidget):
115
  end_value = max(0, min(end_value, nframes-1 if nframes > 0 else 0))
116
 
117
  # Block signals temporarily to avoid triggering callbacks
118
- self.start_slider.blockSignals(True)
119
- self.end_slider.blockSignals(True)
120
 
121
  # Set the values
122
- self.end_slider.setValue(end_value)
123
- self.start_slider.setValue(start_value)
124
 
125
  # Update our tracking variables
126
  self.current_start = start_value
127
  self.current_end = end_value
128
 
129
  # Re-enable signals
130
- self.start_slider.blockSignals(False)
131
- self.end_slider.blockSignals(False)
132
 
133
  # Mark as loop
134
  self.loop_checkbox.setChecked(True)
@@ -211,15 +209,13 @@ class LoopLabeler(QWidget):
211
 
212
  def update_candidate(self):
213
  nframes = len(self.frames)
214
- # Set both sliders to cover full range from 0 to N
215
- self.start_slider.setMinimum(0)
216
- self.start_slider.setMaximum(nframes-1 if nframes > 0 else 0)
217
- self.end_slider.setMinimum(0)
218
- self.end_slider.setMaximum(nframes-1 if nframes > 0 else 0)
219
 
220
  # Block signals temporarily to avoid triggering callbacks multiple times
221
- self.start_slider.blockSignals(True)
222
- self.end_slider.blockSignals(True)
223
 
224
  try:
225
  # Check if there are loop candidates and if the current index is valid
@@ -233,9 +229,9 @@ class LoopLabeler(QWidget):
233
  start = max(0, min(start, nframes-1 if nframes > 0 else 0))
234
  end = max(0, min(end, nframes-1 if nframes > 0 else 0))
235
 
236
- # Set sliders and update our tracking variables
237
- self.start_slider.setValue(start)
238
- self.end_slider.setValue(end)
239
  self.current_start = start
240
  self.current_end = end
241
  else:
@@ -243,15 +239,15 @@ class LoopLabeler(QWidget):
243
  start = 0
244
  default_end = min(10, nframes-1) if nframes > 1 else 0
245
 
246
- # Set sliders and update our tracking variables
247
- self.start_slider.setValue(start)
248
- self.end_slider.setValue(default_end)
249
  self.current_start = start
250
  self.current_end = default_end
251
  finally:
252
  # Always re-enable signals
253
- self.start_slider.blockSignals(False)
254
- self.end_slider.blockSignals(False)
255
 
256
  # Reset animation
257
  self.preview_idx = 0
@@ -260,39 +256,21 @@ class LoopLabeler(QWidget):
260
  if not self.timer.isActive():
261
  self.timer.start(40)
262
 
263
- def on_slider_drag_started(self, slider_type):
264
- """Called when user starts dragging a slider"""
265
- self.is_dragging = True
266
- self.dragging_slider = slider_type
267
- # Store initial values at the start of drag
268
- self.drag_start_value = self.start_slider.value()
269
- self.drag_end_value = self.end_slider.value()
270
-
271
- def on_slider_drag_ended(self):
272
- """Called when user releases a slider after dragging"""
273
- self.is_dragging = False
274
- # No need to update again, the valueChanged events already did it
275
 
276
- # Reset preview index when drag is complete for a smooth preview
277
  self.preview_idx = 0
278
 
279
  # Make sure preview is running
280
  if not self.timer.isActive():
281
  self.timer.start(40)
282
-
283
- def on_slider_value_changed(self, slider_type, value):
284
- """Called when slider value changes (drag, programmatic, etc.)"""
285
- # Update our cached values
286
- if slider_type == 'start':
287
- self.current_start = value
288
- else: # 'end'
289
- self.current_end = value
290
-
291
- # Update the preview - but don't reset animation during continuous drag
292
- if not self.timer.isActive():
293
- self.timer.start(40)
294
 
295
  def update_preview(self):
 
296
  start = self.current_start
297
  end = self.current_end
298
 
@@ -310,8 +288,10 @@ class LoopLabeler(QWidget):
310
  show_progress = False
311
  else:
312
  # Normal case: show animation between start and end
313
- frame_idx = start + (self.preview_idx % (end-start+1))
314
- if frame_idx > max_idx:
 
 
315
  frame_idx = max_idx
316
  frame = self.frames[frame_idx]
317
  show_progress = True
@@ -329,12 +309,25 @@ class LoopLabeler(QWidget):
329
  import cv2
330
  frame_resized = frame.resize((320,320))
331
  arr = np.array(frame_resized)
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  # Draw progress indicator line at bottom
333
  indicator_height = 2
334
 
335
  # Only show progress bar when we have a valid range
336
  if show_progress:
337
- total = end - start
338
  pos = self.preview_idx % total
339
  bar_w = int(320 * (pos / max(total-1, 1)))
340
  else:
@@ -347,7 +340,6 @@ class LoopLabeler(QWidget):
347
  arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
348
  h, w, ch = arr.shape
349
  bytes_per_line = ch * w
350
- from PyQt5.QtGui import QImage
351
  qimg = QImage(arr.data, w, h, bytes_per_line, QImage.Format_BGR888)
352
  pixmap = QPixmap.fromImage(qimg)
353
  self.preview_label.setPixmap(pixmap)
 
3
  from pathlib import Path
4
  from PIL import Image, ImageQt
5
  from PyQt5.QtWidgets import (
6
+ QApplication, QWidget, QLabel, QPushButton, QSlider, QHBoxLayout, QVBoxLayout, QFileDialog, QListWidget, QSpinBox,
7
+ QCheckBox
8
  )
9
  from PyQt5.QtCore import Qt, QTimer
10
+ from PyQt5.QtGui import QPixmap, QColor, QBrush, QImage
11
 
12
  class LoopLabeler(QWidget):
13
  def __init__(self):
 
36
  layout = QVBoxLayout()
37
  # File selector
38
  self.file_list = QListWidget()
 
39
  for i, f in enumerate(self.json_files):
40
  self.file_list.addItem(f.name)
41
  # Visual indicator for annotated files
 
55
  self.preview_label = QLabel()
56
  self.preview_label.setFixedSize(320, 320)
57
  layout.addWidget(self.preview_label)
58
+ # Custom number input fields instead of sliders
59
+ # Using QSpinBox already imported at the top of the file
 
60
 
61
+ # Create layout for start controls with label and spinbox
62
+ start_layout = QHBoxLayout()
63
+ start_layout.addWidget(QLabel("Start index"))
64
+ self.start_spin = QSpinBox()
65
+ self.start_spin.setMinimum(0)
66
+ self.start_spin.setMaximum(9999) # Will be updated with actual frame count
67
+ start_layout.addWidget(self.start_spin)
68
+ layout.addLayout(start_layout)
69
 
70
+ # Create layout for end controls with label and spinbox
71
+ end_layout = QHBoxLayout()
72
+ end_layout.addWidget(QLabel("End index"))
73
+ self.end_spin = QSpinBox()
74
+ self.end_spin.setMinimum(0)
75
+ self.end_spin.setMaximum(9999) # Will be updated with actual frame count
76
+ end_layout.addWidget(self.end_spin)
77
+ layout.addLayout(end_layout)
78
 
79
+ # Connect spinbox value changed signals
80
+ self.start_spin.valueChanged.connect(self.on_range_changed)
81
+ self.end_spin.valueChanged.connect(self.on_range_changed)
 
 
82
 
83
+ # Keep track of current values
84
+ self.current_start = 0
85
+ self.current_end = 0
 
 
 
 
 
86
  # Loop checkbox
 
87
  self.loop_checkbox = QCheckBox("Is loop?")
88
  self.loop_checkbox.setChecked(True)
89
  layout.addWidget(self.loop_checkbox)
 
113
  end_value = max(0, min(end_value, nframes-1 if nframes > 0 else 0))
114
 
115
  # Block signals temporarily to avoid triggering callbacks
116
+ self.start_spin.blockSignals(True)
117
+ self.end_spin.blockSignals(True)
118
 
119
  # Set the values
120
+ self.end_spin.setValue(end_value)
121
+ self.start_spin.setValue(start_value)
122
 
123
  # Update our tracking variables
124
  self.current_start = start_value
125
  self.current_end = end_value
126
 
127
  # Re-enable signals
128
+ self.start_spin.blockSignals(False)
129
+ self.end_spin.blockSignals(False)
130
 
131
  # Mark as loop
132
  self.loop_checkbox.setChecked(True)
 
209
 
210
  def update_candidate(self):
211
  nframes = len(self.frames)
212
+ # Set both spin boxes to cover full range from 0 to N
213
+ self.start_spin.setMaximum(nframes-1 if nframes > 0 else 0)
214
+ self.end_spin.setMaximum(nframes-1 if nframes > 0 else 0)
 
 
215
 
216
  # Block signals temporarily to avoid triggering callbacks multiple times
217
+ self.start_spin.blockSignals(True)
218
+ self.end_spin.blockSignals(True)
219
 
220
  try:
221
  # Check if there are loop candidates and if the current index is valid
 
229
  start = max(0, min(start, nframes-1 if nframes > 0 else 0))
230
  end = max(0, min(end, nframes-1 if nframes > 0 else 0))
231
 
232
+ # Set spin box values and update our tracking variables
233
+ self.start_spin.setValue(start)
234
+ self.end_spin.setValue(end)
235
  self.current_start = start
236
  self.current_end = end
237
  else:
 
239
  start = 0
240
  default_end = min(10, nframes-1) if nframes > 1 else 0
241
 
242
+ # Set spin box values and update our tracking variables
243
+ self.start_spin.setValue(start)
244
+ self.end_spin.setValue(default_end)
245
  self.current_start = start
246
  self.current_end = default_end
247
  finally:
248
  # Always re-enable signals
249
+ self.start_spin.blockSignals(False)
250
+ self.end_spin.blockSignals(False)
251
 
252
  # Reset animation
253
  self.preview_idx = 0
 
256
  if not self.timer.isActive():
257
  self.timer.start(40)
258
 
259
+ def on_range_changed(self):
260
+ """Called when either spinbox value changes"""
261
+ # Update our cached values
262
+ self.current_start = self.start_spin.value()
263
+ self.current_end = self.end_spin.value()
 
 
 
 
 
 
 
264
 
265
+ # Reset animation index to give a smooth preview after changing range
266
  self.preview_idx = 0
267
 
268
  # Make sure preview is running
269
  if not self.timer.isActive():
270
  self.timer.start(40)
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
  def update_preview(self):
273
+ # Get current values directly from spinboxes to ensure latest values
274
  start = self.current_start
275
  end = self.current_end
276
 
 
288
  show_progress = False
289
  else:
290
  # Normal case: show animation between start and end
291
+ # The +1 is because we want to include both start and end frames
292
+ range_size = end - start + 1
293
+ frame_idx = start + (self.preview_idx % range_size)
294
+ if frame_idx > max_idx: # Safety check
295
  frame_idx = max_idx
296
  frame = self.frames[frame_idx]
297
  show_progress = True
 
309
  import cv2
310
  frame_resized = frame.resize((320,320))
311
  arr = np.array(frame_resized)
312
+
313
+ # Draw current frame number overlay
314
+ if self.frames and len(self.frames) > 0:
315
+ cv2.putText(
316
+ arr,
317
+ f"Frame: {frame_idx}",
318
+ (10, 30), # Position (x, y)
319
+ cv2.FONT_HERSHEY_SIMPLEX, # Font
320
+ 0.7, # Scale
321
+ (255, 255, 255), # Color (white)
322
+ 2 # Thickness
323
+ )
324
+
325
  # Draw progress indicator line at bottom
326
  indicator_height = 2
327
 
328
  # Only show progress bar when we have a valid range
329
  if show_progress:
330
+ total = end - start + 1 # +1 to include both start and end frames
331
  pos = self.preview_idx % total
332
  bar_w = int(320 * (pos / max(total-1, 1)))
333
  else:
 
340
  arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
341
  h, w, ch = arr.shape
342
  bytes_per_line = ch * w
 
343
  qimg = QImage(arr.data, w, h, bytes_per_line, QImage.Format_BGR888)
344
  pixmap = QPixmap.fromImage(qimg)
345
  self.preview_label.setPixmap(pixmap)