Keypoints: Smart skellies
This commit is contained in:
@@ -56,8 +56,9 @@ from supervision.detection.utils import (
|
||||
polygon_to_xyxy,
|
||||
scale_boxes,
|
||||
)
|
||||
from supervision.keypoints.core import KeyPoints, YOLO_V8_SKELETON
|
||||
from supervision.keypoints.annotate import PointAnnotator, SkeletonAnnotator
|
||||
from supervision.keypoints.core import KeyPoints
|
||||
from supervision.keypoints.skeletons import KnownSkeletons, Skeleton
|
||||
from supervision.keypoints.annotate import KeyPointAnnotator, SkeletonAnnotator
|
||||
from supervision.draw.color import Color, ColorPalette
|
||||
from supervision.draw.utils import (
|
||||
calculate_optimal_line_thickness,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
@@ -6,7 +7,8 @@ import numpy as np
|
||||
from supervision.annotators.base import ImageType
|
||||
from supervision.annotators.utils import scene_to_annotator_img_type
|
||||
from supervision.draw.color import Color
|
||||
from supervision.keypoints.core import KeyPoints, Skeleton
|
||||
from supervision.keypoints.core import KeyPoints
|
||||
from supervision.keypoints.skeletons import Skeleton, KnownSkeletons
|
||||
|
||||
|
||||
class BaseKeyPointAnnotator(ABC):
|
||||
@@ -15,14 +17,18 @@ class BaseKeyPointAnnotator(ABC):
|
||||
pass
|
||||
|
||||
|
||||
class PointAnnotator(BaseKeyPointAnnotator):
|
||||
class KeyPointAnnotator(BaseKeyPointAnnotator):
|
||||
def __init__(
|
||||
self,
|
||||
color: Color = Color.GREEN,
|
||||
color: Color = Color.ROBOFLOW,
|
||||
radius: int = 4,
|
||||
) -> None:
|
||||
"""
|
||||
Most basic keypoint annotator.
|
||||
|
||||
Args:
|
||||
color (Color, optional): The color of the keypoint.
|
||||
radius (int, optional): The radius of the keypoint.
|
||||
"""
|
||||
self.color = color
|
||||
self.radius = radius
|
||||
@@ -50,15 +56,26 @@ class PointAnnotator(BaseKeyPointAnnotator):
|
||||
class SkeletonAnnotator(BaseKeyPointAnnotator):
|
||||
def __init__(
|
||||
self,
|
||||
color: Color = Color.GREEN,
|
||||
color: Color = Color.ROBOFLOW,
|
||||
thickness: int = 2,
|
||||
skeleton: Optional[Skeleton] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Draw the lines between points of the image.
|
||||
|
||||
Args:
|
||||
color (Color, optional): The color of the lines.
|
||||
thickness (int, optional): The thickness of the lines.
|
||||
skeleton (Skeleton, optional): The skeleton to draw.
|
||||
If set to `None`, will attempt to select automatically.
|
||||
"""
|
||||
self.color = color
|
||||
self.thickness = thickness
|
||||
self.skeleton = skeleton
|
||||
|
||||
@scene_to_annotator_img_type
|
||||
def annotate(
|
||||
self, scene: ImageType, keypoints: KeyPoints, skeleton: Skeleton
|
||||
self, scene: ImageType, keypoints: KeyPoints
|
||||
) -> ImageType:
|
||||
if len(keypoints) == 0:
|
||||
return scene
|
||||
@@ -67,6 +84,9 @@ class SkeletonAnnotator(BaseKeyPointAnnotator):
|
||||
"KeyPoints must have class_id to annotate a skeleton")
|
||||
|
||||
xy = keypoints.xy[0]
|
||||
skeleton = self.skeleton
|
||||
if not skeleton:
|
||||
skeleton = KnownSkeletons().get_skeleton(len(xy))
|
||||
|
||||
for class_a, class_b in skeleton.limbs:
|
||||
xy_a = xy[class_a - 1]
|
||||
|
||||
@@ -58,7 +58,8 @@ class KeyPoints:
|
||||
xy: npt.NDArray[np.float32]
|
||||
class_id: Optional[npt.NDArray[np.int_]] = None
|
||||
confidence: Optional[npt.NDArray[np.float32]] = None
|
||||
data: Dict[str, Union[npt.NDArray[Any], List]] = field(default_factory=dict)
|
||||
data: Dict[str, Union[npt.NDArray[Any], List]
|
||||
] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self):
|
||||
validate_keypoints_fields(
|
||||
@@ -138,7 +139,8 @@ class KeyPoints:
|
||||
|
||||
xy = ultralytics_results.keypoints.xy.cpu().numpy()
|
||||
class_id = ultralytics_results.boxes.cls.cpu().numpy().astype(int)
|
||||
class_names = np.array([ultralytics_results.names[i] for i in class_id])
|
||||
class_names = np.array([ultralytics_results.names[i]
|
||||
for i in class_id])
|
||||
|
||||
confidence = ultralytics_results.keypoints.conf.cpu().numpy()
|
||||
data = {CLASS_NAME_DATA_FIELD: class_names}
|
||||
@@ -239,39 +241,3 @@ class KeyPoints:
|
||||
```
|
||||
"""
|
||||
return cls(xy=np.empty((0, 0, 2), dtype=np.float32))
|
||||
|
||||
|
||||
class Skeleton:
|
||||
"""
|
||||
The `Skeleton` class connects keypoints to form a skeleton.
|
||||
|
||||
It provides utility methods to compute angles within itself, compare to skeletons, etc.
|
||||
"""
|
||||
|
||||
limbs: List[Tuple[int, int]]
|
||||
|
||||
def __init__(self, limbs: List[Tuple[int, int]]):
|
||||
self.limbs = limbs
|
||||
|
||||
|
||||
YOLO_V8_SKELETON = Skeleton(
|
||||
[
|
||||
(1, 2),
|
||||
(1, 3),
|
||||
(2, 3),
|
||||
(2, 4),
|
||||
(3, 5),
|
||||
(6, 12),
|
||||
(6, 7),
|
||||
(6, 8),
|
||||
(7, 13),
|
||||
(7, 9),
|
||||
(8, 10),
|
||||
(9, 11),
|
||||
(12, 13),
|
||||
(14, 12),
|
||||
(15, 13),
|
||||
(16, 14),
|
||||
(17, 15),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
|
||||
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
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(
|
||||
[
|
||||
(1, 2),
|
||||
(1, 3),
|
||||
(2, 3),
|
||||
(2, 4),
|
||||
(3, 5),
|
||||
(6, 12),
|
||||
(6, 7),
|
||||
(6, 8),
|
||||
(7, 13),
|
||||
(7, 9),
|
||||
(8, 10),
|
||||
(9, 11),
|
||||
(12, 13),
|
||||
(14, 12),
|
||||
(15, 13),
|
||||
(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)
|
||||
|
||||
|
||||
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. 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)
|
||||
Reference in New Issue
Block a user