test simplified skeleton API

This commit is contained in:
SkalskiP
2024-04-24 00:43:46 +02:00
parent a2dbbcce63
commit 102c5adc38
3 changed files with 21 additions and 79 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ from supervision.geometry.core import Point, Position, Rect
from supervision.geometry.utils import get_polygon_center
from supervision.keypoint.annotate import SkeletonAnnotator, VertexAnnotator
from supervision.keypoint.core import KeyPoints
from supervision.keypoint.skeletons import KnownSkeletons, Skeleton
from supervision.keypoint.skeletons import Skeleton
from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision
from supervision.tracker.byte_tracker.core import ByteTrack
from supervision.utils.conversion import cv2_to_pillow, pillow_to_cv2
+5 -5
View File
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Optional
from typing import Optional, List, Tuple
import cv2
import numpy as np
@@ -7,7 +7,7 @@ import numpy as np
from supervision.annotators.base import ImageType
from supervision.draw.color import Color
from supervision.keypoint.core import KeyPoints
from supervision.keypoint.skeletons import KnownSkeletons, Skeleton
from supervision.keypoint.skeletons import resolve_skeleton_by_vertex_count
from supervision.utils.conversion import convert_for_annotation_method
@@ -57,7 +57,7 @@ class SkeletonAnnotator(BaseKeyPointAnnotator):
self,
color: Color = Color.ROBOFLOW,
thickness: int = 2,
skeleton: Optional[Skeleton] = None,
skeleton: Optional[List[Tuple[int, int]]] = None,
) -> None:
"""
Draw the lines between points of the image.
@@ -83,9 +83,9 @@ class SkeletonAnnotator(BaseKeyPointAnnotator):
for xy in xy_all:
skeleton = self.skeleton
if not skeleton:
skeleton = KnownSkeletons().get_skeleton(len(xy))
skeleton = resolve_skeleton_by_vertex_count(len(xy))
for class_a, class_b in skeleton.limbs:
for class_a, class_b in skeleton:
xy_a = xy[class_a - 1]
xy_b = xy[class_b - 1]
missing_a = np.allclose(xy_a, 0)
+15 -73
View File
@@ -1,24 +1,9 @@
from typing import Dict, List, Tuple
from enum import Enum
from typing import Optional, Tuple, List
class Skeleton:
"""
The `Skeleton` class connects keypoints to form a skeleton.
Args:
limbs: (List[Tuple[int, int]]): List of (`class_id_1`,
`class_id_2`) representing connected points of the skeleton.
"""
limbs: List[Tuple[int, int]]
def __init__(self, limbs: List[Tuple[int, int]]):
self.limbs = limbs
NO_SKELETON = Skeleton([])
YOLO_V8_SKELETON = Skeleton(
[
class Skeleton(Enum):
COCO = [
(1, 2),
(1, 3),
(2, 3),
@@ -37,61 +22,18 @@ YOLO_V8_SKELETON = Skeleton(
(16, 14),
(17, 15),
]
)
def _make_sequential_skeleton(limb_count: int) -> Skeleton:
"""
Create a skeleton where neighboring keypoints are connected sequentially.
Args:
limb_count (int): The number of limbs to connect.
Returns:
Skeleton: The skeleton connecting all keypoints.
"""
limbs = []
for i in range(0, limb_count - 1):
limbs.append((i, i + 1))
limbs.append((0, limb_count - 1))
return Skeleton(limbs)
def resolve_skeleton_by_vertex_count(count: int) -> Optional[List[Tuple[int, int]]]:
for skeleton in Skeleton:
unique_vertices = set(vertex for edge in skeleton.value for vertex in edge)
if len(unique_vertices) == count:
return skeleton.value
return None
class KnownSkeletons:
"""
Helps automatically determine which skeleton to use.
The `KnownSkeletons` class contains a collection of predefined skeletons
for various keypoint models, to be distinguished by limb counts.
Example:
# Suppose you have a model returning 15 keypoints.
skeleton = KnownSkeletons().get_skeleton(15)
"""
_instance = None
_skeletons: Dict[int, Skeleton] = {}
def __new__(cls):
if cls._instance is None:
cls._instance = super(KnownSkeletons, cls).__new__(cls)
return cls._instance
def add_skeleton(self, skeleton: Skeleton) -> None:
len_limbs = len(skeleton.limbs)
if len_limbs in self._skeletons:
raise ValueError(f"A skeleton with {len_limbs} limbs already exists")
self._skeletons[len_limbs] = skeleton
def get_skeleton(self, limb_count: int) -> Skeleton:
if limb_count not in self._skeletons:
print(
f"Warning: No skeleton found with {limb_count} limbs. "
f"Will create one now."
)
self.add_skeleton(_make_sequential_skeleton(limb_count))
return self._skeletons[limb_count]
KnownSkeletons().add_skeleton(NO_SKELETON)
KnownSkeletons().add_skeleton(YOLO_V8_SKELETON)
def resolve_skeleton_by_edge_count(count: int) -> Optional[List[Tuple[int, int]]]:
for skeleton in Skeleton:
if len(skeleton.value) == count:
return skeleton.value
return None