[skeleton] fix slider enabling, set scene rect to min-max extent of data

This commit is contained in:
Jan Grewe 2025-02-06 10:44:36 +01:00
parent 6c75d24f97
commit 2f40879624

View File

@ -50,6 +50,10 @@ class SkeletonWidget(QWidget):
super().__init__(parent)
self._skeletons = []
self._current_skeleton = None
self._maxx = 0.
self._minx = 0.
self._maxy = 0.
self._miny = 0.
self._slider = QSlider(Qt.Orientation.Horizontal)
self._slider.sliderMoved.connect(self.on_sliderMoved)
self._scene = QGraphicsScene()
@ -65,7 +69,8 @@ class SkeletonWidget(QWidget):
self.setLayout(lyt)
self._view.fitInView(self._scene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
self._scene.changed.connect(lambda: self._view.fitInView(self._scene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio))
self._scene.changed.connect(lambda: self._view.fitInView(self._scene.sceneRect(),
Qt.AspectRatioMode.KeepAspectRatio))
def clear(self):
for i in range(len(self._skeletons)):
@ -84,28 +89,48 @@ class SkeletonWidget(QWidget):
self._scene.addItem(self._current_skeleton)
def update(self):
logging.debug("SkeletonWidget: update")
if len(self._skeletons) > 0:
self._scene.addItem(self._skeletons[-1])
self._current_skeleton = self._skeletons[-1]
self._slider.setMaximum(len(self._skeletons))
self._slider.setMinimum(0)
self._slider.setValue(len(self._skeletons))
if len(self._skeletons) == 0:
self._slider.setEnabled(False)
self._slider.setEnabled(len(self._skeletons) > 0)
self._scene.setSceneRect(self._minx, self._miny, self._maxx - self._minx, self._maxy - self._miny)
self._view.fitInView(self._scene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
def addSkeleton(self, coords, detection_id, brush, update=True):
def check_extent(x, y, w, h):
if len(self._skeletons) == 0:
self._minx = x
self._maxx = x + w
self._miny = y
self._maxy = y + h
else:
if x < self._minx:
self._minx = x
if y > self._maxy:
self._maxy = y
if x + w > self._maxx:
self._maxx = x + w
if y + h > self._maxy:
self._maxy = y + h
boxx = np.min(coords[:,0])
boxy = np.min(coords[:,1])
boxw = np.max(coords[:, 0]) - boxx
boxh = np.max(coords[:, 1]) - boxy
check_extent(boxx, boxy, boxw, boxh)
item = Skeleton(boxx, boxy, boxw, boxh, coords, brush)
item.setData(0, detection_id)
self._skeletons.append(item)
if update:
self.update()
def addSkeletons(self, coordinates, detection_ids, brush):
num_detections = coordinates.shape[0]
def addSkeletons(self, coordinates:np.ndarray, detection_ids:np.ndarray, brush:QBrush):
num_detections = 0 if coordinates is None else coordinates.shape[0]
logging.debug("SkeletonWidget: add %i Skeletons", num_detections)
for i in range(num_detections):
self.addSkeleton(coordinates[i,:,:], detection_ids[i], brush=brush, update=False)
self.update()