fix: 🐞 update from_moondream function to remove label requirement and improve box denormalization

This commit is contained in:
Onuralp SEZER
2025-07-10 23:07:56 +03:00
parent 300426ed41
commit 399d2efa59
3 changed files with 2249 additions and 2252 deletions
+1 -4
View File
@@ -1035,10 +1035,7 @@ class Detections:
return cls(xyxy=xyxy, mask=mask, data=data)
if (
vlm == VLM.GOOGLE_GEMINI_2_0
or vlm == VLM.GOOGLE_GEMINI_2_5
):
if vlm == VLM.GOOGLE_GEMINI_2_0 or vlm == VLM.GOOGLE_GEMINI_2_5:
xyxy, class_name = from_google_gemini(result, **kwargs)
data = {CLASS_NAME_DATA_FIELD: class_name}
return cls(xyxy=xyxy, data=data)
+29 -29
View File
@@ -58,7 +58,7 @@ ALLOWED_ARGUMENTS: Dict[VLM, List[str]] = {
VLM.QWEN_2_5_VL: ["input_wh", "resolution_wh", "classes"],
VLM.GOOGLE_GEMINI_2_0: ["resolution_wh"],
VLM.GOOGLE_GEMINI_2_5: ["resolution_wh"],
VLM.MOONDREAM: ["resolution_wh", "classes"],
VLM.MOONDREAM: ["resolution_wh"],
}
SUPPORTED_TASKS_FLORENCE_2 = [
@@ -415,24 +415,30 @@ def from_google_gemini(
def from_moondream(
result: dict,
resolution_wh: Tuple[int, int],
classes: Optional[List[str]] = None,
) -> Tuple[np.ndarray, Optional[np.ndarray], np.ndarray]:
) -> Tuple[np.ndarray, np.ndarray]:
"""
Parse and scale bounding boxes from moondream JSON output.
The JSON is expected to have a key "objects" with a list of dictionaries:
{
"objects": [
{"x_min": 0.1, "y_min": 0.2, "x_max": 0.3, "y_max": 0.4, "label": "some class name"},
{"x_min": 0.1, "y_min": 0.2, "x_max": 0.3, "y_max": 0.4},
...
]
}
For Example:
{
"objects": [
{"x_min": 0.1, "y_min": 0.2, "x_max": 0.3, "y_max": 0.4, "label": "cat"},
{"x_min": 0.5, "y_min": 0.6, "x_max": 0.7, "y_max": 0.8, "label": "dog"}
]
}
Args:
result: Dictionary containing the JSON output from the model.
resolution_wh: (output_width, output_height) to which we rescale the boxes.
classes: Optional list of valid class names. If provided, returned boxes/labels
are filtered to only those classes found here.
Returns:
xyxy (np.ndarray): An array of shape `(n, 4)` containing
@@ -442,7 +448,8 @@ def from_moondream(
provided)
class_name (np.ndarray): An array of shape `(n,)` containing
the class labels for each bounding box
""" # noqa: E501 #docs
""" # docs
w, h = resolution_wh
if w <= 0 or h <= 0:
raise ValueError(
@@ -450,13 +457,12 @@ def from_moondream(
)
if "objects" not in result or not isinstance(result["objects"], list):
return np.empty((0, 4)), None, np.empty((0,), dtype=str)
return np.empty((0, 4)), np.empty((0,), dtype=int)
boxes_list = []
labels_list = []
denormalize_xyxy = []
for item in result["objects"]:
if not all(k in item for k in ["x_min", "y_min", "x_max", "y_max", "label"]):
if not all(k in item for k in ["x_min", "y_min", "x_max", "y_max"]):
continue
x_min = item["x_min"]
@@ -464,24 +470,18 @@ def from_moondream(
x_max = item["x_max"]
y_max = item["y_max"]
boxes_list.append([x_min, y_min, x_max, y_max])
labels_list.append(item["label"])
denormalize_xyxy.append(
denormalize_boxes(
np.array([x_min, y_min, x_max, y_max]).astype(np.float64),
resolution_wh=(w, h),
)
)
if not boxes_list:
return np.empty((0, 4)), None, np.empty((0,), dtype=str)
if not denormalize_xyxy:
return np.empty((0, 4)), np.empty((0,), dtype=int)
xyxy = np.array(boxes_list, dtype=float)
class_name = np.array(labels_list, dtype=str)
xyxy = np.array(denormalize_xyxy, dtype=float)
num_detections = len(xyxy)
class_id = np.arange(num_detections)
# Moondream boxes are normalized, so we scale them
xyxy = xyxy * np.array([w, h, w, h])
class_id = None
if classes is not None:
mask = np.array([label in classes for label in class_name], dtype=bool)
xyxy = xyxy[mask]
class_name = class_name[mask]
class_id = np.array([classes.index(label) for label in class_name], dtype=int)
return xyxy, class_id, class_name
return xyxy, class_id
Generated
+2219 -2219
View File
File diff suppressed because it is too large Load Diff