Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ def INPUT_TYPES(cls):
"INT",
{"default": 5, "min": 1, "max": 50, "step": 1, "tooltip": "Maximum number of objects to detect"},
),
"category_allowlist": (
"STRING",
{
"default": "",
"multiline": False,
"tooltip": "Comma-separated list of categories to detect (e.g., 'person,cat'). Empty means all categories.",
},
),
"category_denylist": (
"STRING",
{
"default": "",
"multiline": False,
"tooltip": "Comma-separated list of categories to exclude. Empty means not to exclude any categories.",
},
),
}
)

Expand All @@ -64,6 +80,8 @@ def detect(
model_info: dict,
min_confidence: float,
max_results: int,
category_allowlist: str,
category_denylist: str,
running_mode: str,
delegate: str,
):
Expand All @@ -75,9 +93,19 @@ def detect(
# Initialize or update detector
detector = self.initialize_or_update_detector(model_path)

# Parse the allow / deny list string into a list, or None if empty
allowed_categories = [cat.strip() for cat in category_allowlist.split(',') if cat.strip()] or None
denied_categories = [cat.strip() for cat in category_denylist.split(',') if cat.strip()] or None

# Perform detection with all parameters
batch_results = detector.detect(
image, score_threshold=min_confidence, max_results=max_results, running_mode=running_mode, delegate=delegate
image,
score_threshold=min_confidence,
max_results=max_results,
category_allowlist=allowed_categories,
category_denylist=denied_categories,
running_mode=running_mode,
delegate=delegate
)

return (batch_results,)
Expand Down
8 changes: 7 additions & 1 deletion src/mediapipe_vision/object_detection/detector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List
from typing import Any, List, Optional

import mediapipe as mp
import numpy as np
Expand Down Expand Up @@ -40,6 +40,8 @@ def _create_detector_options(self, base_options: python.BaseOptions,
running_mode=mode_enum,
score_threshold=kwargs.get('score_threshold', 0.5),
max_results=kwargs.get('max_results', 5),
category_allowlist=kwargs.get('category_allowlist', None),
category_denylist=kwargs.get('category_denylist', None),
)

def _create_detector_instance(self, options: vision.ObjectDetectorOptions) -> vision.ObjectDetector:
Expand Down Expand Up @@ -89,6 +91,8 @@ def detect(
image: torch.Tensor,
score_threshold: float = 0.5,
max_results: int = 5,
category_allowlist: Optional[List[str]] = None,
category_denylist: Optional[List[str]] = None,
running_mode: str = "video",
delegate: str = "cpu",
) -> List[List[ObjectDetectionResult]]:
Expand All @@ -99,4 +103,6 @@ def detect(
delegate=delegate,
score_threshold=score_threshold,
max_results=max_results,
category_allowlist=category_allowlist,
category_denylist=category_denylist,
)