Skip to content

fix: prevent adding cameras with invalid model names (#1162)#1165

Open
COZYTRICKSTER wants to merge 3 commits intoopen-edge-platform:release-2025.2from
COZYTRICKSTER:fix-camera-model-validation
Open

fix: prevent adding cameras with invalid model names (#1162)#1165
COZYTRICKSTER wants to merge 3 commits intoopen-edge-platform:release-2025.2from
COZYTRICKSTER:fix-camera-model-validation

Conversation

@COZYTRICKSTER
Copy link

📝 Description

When adding a camera, the system allowed specifying a model name that does not exist in the configured model list (model-config). The camera would still be created successfully even though the model was invalid.

This could result in a confusing state where:

  • The camera appears in the system
  • No video feed is processed
  • No object tracking appears on the GUI

This change introduces validation during camera creation to ensure that the provided model name exists in model-config. If the model is not found, the request is rejected with a validation error.

Fixes #1162


🔍 Behavior Before / After

Before

  • Cameras could be created with invalid model names that were not present in model-config.
  • The camera would appear in the system but no feed or tracking would be available on the GUI.

After

  • The system validates the model name during camera creation.
  • If the specified model does not exist in model-config, the request is rejected with an appropriate validation error.

✨ Type of Change

  • 🐞 Bug fix – Non-breaking change which fixes an issue
  • 🚀 New feature – Non-breaking change which adds functionality
  • 🔨 Refactor – Non-breaking change which refactors the code base
  • 💥 Breaking change – Changes that break existing functionality
  • 📚 Documentation update
  • 🔒 Security update
  • 🧪 Tests
  • 🚂 CI

🧪 Testing Scenarios

  • ✅ Tested manually
  • 🤖 Ran automated end-to-end tests

Tested locally by attempting to add a camera with both valid and invalid model names. Verified that:

  • Cameras with valid models are created successfully.
  • Cameras with invalid model names are rejected with a validation error.

✅ Checklist

  • 🔍 PR title is clear and descriptive
  • 📝 For internal contributors: If applicable, include the JIRA ticket number in the PR title
  • 💬 I have commented my code, especially in hard-to-understand areas
  • 📄 I have made corresponding changes to the documentation
  • ✅ I have added tests that prove my fix is effective or my feature works

@COZYTRICKSTER
Copy link
Author

Test Results


Sharing screenshots from testing to demonstrate the behavior before and after the fix.

In this example, I attempted to add a camera with the model pv2000, which is not present in model-config.

Before Fix

  • The system allowed adding a camera with a model name that was not present in model-config.
  • The camera was created but no incoming feed appeared in the GUI.
image

After Fix

  • The system now validates the model name against model-config when adding a camera.
  • If the model does not exist, the request is rejected with a validation error, preventing the camera from being created.
image

Copy link
Contributor

@tdorauintc tdorauintc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@COZYTRICKSTER thank you for your contribution.
For the sake of code maintainability and reliability, I would advice to try to reuse existing code for validation. Please see my comments.

Comment on lines +196 to +219
from manager.ppl_generator.model_chain import CameraChainOperations

# Load model config
model_config_path = Path(os.environ.get('MODEL_CONFIGS_FOLDER', '/models/model_configs')) / 'model_config.json'
if not model_config_path.is_file():
raise ValidationError(f"Model config file not found at {model_config_path}")

try:
with open(model_config_path, 'r') as f:
model_config = json.load(f)
except (json.JSONDecodeError, IOError) as e:
raise ValidationError(f"Error reading model config: {str(e)}")

# Extract model names from chain
chain_parts = camerachain.replace(CameraChainOperations.PARALLEL.value, CameraChainOperations.SEQUENTIAL.value)
model_names = [part.split('=')[0].strip() for part in chain_parts.split(CameraChainOperations.SEQUENTIAL.value) if part.strip()]

# Validate models exist
missing = [m for m in model_names if m not in model_config]
if missing:
missing_str = ', '.join(f"'{m}'" for m in missing)
available = ', '.join(f"'{m}'" for m in sorted(model_config.keys()))
raise ValidationError(f"Error adding camera: Model(s) {missing_str} not found in model config file. Available models: {available}")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to address this issue by reusing existing code in one of the following ways:

  • Reusing the function def parse_model_chain(model_chain: str, models_folder: str, model_config: dict) to validate camerachain.
  • Reusing the function generate_pipeline_string_from_dict to validate the entire form, similarly as it is done here. Since the model config falls back to default if empty, it should also handle the new camera form with limited number of fields.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tdorauintc Thank you for the review and suggestions!

I analyzed both options:

  • Option 1 (parse_model_chain): Works well for validating camerachain in isolation.
    However, since it builds InferenceNode objects one by one and fails fast on the first
    missing model, it would only report the first missing model.
    For example, for retail+pv2000+pv20 where the last two are missing, only pv2000 is reported.

  • Option 2 (generate_pipeline_string_from_dict): Ultimately calls the same parse_model_chain
    internally, so has the same fail-fast behavior.

I have a concern: the current fix collects all missing models at once and reports them in a single error message,
which is slightly better UX.

I'm happy to refactor to use parse_model_chain as suggested if the fail-fast behavior is acceptable.
Could you confirm your preference? I'll proceed with the refactor once I hear back.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@COZYTRICKSTER fail-fast behavior is acceptable, please refactor with the 1st option as you suggested. Reusing existing parser parse_model_chain makes the implementation future-proof in case we change syntax some time in future, this brings significant benefits.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tdorauintc Thanks for the clarification!

I have refactored the validation to reuse parse_model_chain() as suggested.
The latest commit updates CamCreateForm.clean_camerachain() accordingly.

Please let me know if any further adjustments are needed.

Replace manual model validation in CamCreateForm.clean_camerachain()
with parse_model_chain() from ppl_generator. This aligns validation
with the existing pipeline parser and ensures future compatibility
with potential chain syntax changes.

Also includes minor indentation fixes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds server-side validation to prevent cameras being created with a camerachain that references models not present in the configured model-config, avoiding “camera exists but nothing processes/tracks” states.

Changes:

  • Add CamCreateForm.clean_camerachain() validation that parses the model chain against model_config.json and rejects invalid models.
  • Override CamCreateView.form_invalid() to return HTTP 400 on validation failure.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
manager/src/django/forms.py Adds camerachain validation by loading model-config and calling parse_model_chain() during camera creation.
manager/src/django/views.py Returns 400 status for invalid camera-create form submissions.

Comment on lines +199 to +205
model_config_path = Path(os.environ.get('MODEL_CONFIGS_FOLDER', '/models/model_configs')) / 'model_config.json'
if not model_config_path.is_file():
raise ValidationError(f"Model config file not found at {model_config_path}")

try:
with open(model_config_path, 'r') as f:
model_config = json.load(f)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants