Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🚀 Keypoint support for detectron2 #1310

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: ✨ Keypoints for detectron2 added
Signed-off-by: Onuralp SEZER <[email protected]>
  • Loading branch information
onuralpszr committed Jun 26, 2024
commit 0bb3839d2922bea18eb56316ec3b0487add14f1b
44 changes: 44 additions & 0 deletions supervision/keypoint/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,50 @@ def from_yolo_nas(cls, yolo_nas_results) -> KeyPoints:
data=data,
)

@classmethod
def from_detectron2(cls, detectron2_results) -> KeyPoints:
"""
Create a `sv.KeyPoints` object from the
[Detectron2](https://github.com/facebookresearch/detectron2) inference result.

Args:
detectron2_results: The output of a
Detectron2 model containing instances with prediction data.

Returns:
A `sv.KeyPoints` object containing the keypoint coordinates, class IDs,
and class names, and confidences of each keypoint.

Example:
```python
import cv2
import supervision as sv
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg


image = cv2.imread(<SOURCE_IMAGE_PATH>)
cfg = get_cfg()
cfg.merge_from_file(<CONFIG_PATH>)
cfg.MODEL.WEIGHTS = <WEIGHTS_PATH>
predictor = DefaultPredictor(cfg)

result = predictor(image)
keypoints = sv.Keypoints.from_detectron2(result)
```
"""

return cls(
xy=detectron2_results["instances"].pred_keypoints.cpu().numpy()[:, :, :2],
confidence=detectron2_results["instances"]
.pred_keypoints.cpu()
.numpy()[:, :, 2:],
class_id=detectron2_results["instances"]
.pred_classes.cpu()
.numpy()
.astype(int),
)

def __getitem__(
self, index: Union[int, slice, List[int], np.ndarray, str]
) -> Union[KeyPoints, List, np.ndarray, None]:
Expand Down