add group by param to fetch attributes endpoint

This commit is contained in:
Josh Hawkins 2025-12-18 07:26:11 -06:00
parent 2872882782
commit 2689e74b18

View File

@ -625,33 +625,55 @@ def get_classification_dataset(name: str):
@router.get( @router.get(
"/classification/attributes", "/classification/attributes",
summary="Get all custom classification attributes", summary="Get custom classification attributes",
description="""Returns a list of all unique attribute labels from custom classification models. description="""Returns custom classification attributes for a given object type.
Only includes models with classification_type set to 'attribute'. Only includes models with classification_type set to 'attribute'.
Optionally filter by object_type to only return attributes from models that apply to that object type.""", By default returns a flat sorted list of all attribute labels.
If group_by_model is true, returns attributes grouped by model name.""",
) )
def get_custom_attributes(request: Request, object_type: str = None): def get_custom_attributes(
custom_attribute_labels = set() request: Request, object_type: str = None, group_by_model: bool = False
for name, model_config in request.app.frigate_config.classification.custom.items(): ):
models_with_attributes = {}
for (
model_key,
model_config,
) in request.app.frigate_config.classification.custom.items():
if ( if (
model_config.object_config not model_config.enabled
and model_config.object_config.classification_type or not model_config.object_config
== ObjectClassificationType.attribute or model_config.object_config.classification_type
!= ObjectClassificationType.attribute
): ):
# If object_type is specified, check if this model applies to that object type continue
if object_type is not None:
model_objects = getattr(model_config.object_config, "objects", []) or []
if object_type not in model_objects:
continue
dataset_dir = os.path.join(CLIPS_DIR, sanitize_filename(name), "dataset") model_objects = getattr(model_config.object_config, "objects", []) or []
if os.path.exists(dataset_dir): if object_type is not None and object_type not in model_objects:
for category_name in os.listdir(dataset_dir): continue
category_dir = os.path.join(dataset_dir, category_name)
if os.path.isdir(category_dir):
custom_attribute_labels.add(category_name)
return JSONResponse(content=sorted(list(custom_attribute_labels))) dataset_dir = os.path.join(CLIPS_DIR, sanitize_filename(model_key), "dataset")
if not os.path.exists(dataset_dir):
continue
attributes = []
for category_name in os.listdir(dataset_dir):
category_dir = os.path.join(dataset_dir, category_name)
if os.path.isdir(category_dir) and category_name != "none":
attributes.append(category_name)
if attributes:
model_name = model_config.name or model_key
models_with_attributes[model_name] = sorted(attributes)
if group_by_model:
return JSONResponse(content=models_with_attributes)
else:
# Flatten to a unique sorted list
all_attributes = set()
for attributes in models_with_attributes.values():
all_attributes.update(attributes)
return JSONResponse(content=sorted(list(all_attributes)))
@router.get( @router.get(