Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

Adds infrastructure for managing AI object detection models, classes, and settings in ZoneMinder. Provides database tables for storing models, object classes, detection settings per monitor, and detection results, along with a web UI for managing object classes.

Database Schema (db/AI_Models.sql)

Four new tables with foreign key constraints:

  • AI_Models - Model metadata (name, framework, version, path)
  • AI_Object_Classes - Object classes per model (name, index, description)
  • AI_Detection_Settings - Per-monitor or global detection configuration (confidence threshold, reporting, color)
  • AI_Detections - Detection results (event, frame, bounding box, confidence, timestamp)

Web UI

Options → AI tab for managing AI_Object_Classes:

  • List view (_options_ai_classes.php) - Table with model name, class name, class index, bulk delete
  • Modal editor (ai_class.php) - Form for add/edit with model dropdown
  • Action handler (ai_class.php) - Save/delete with canEdit('System') permission checks
  • JavaScript (ai_classes.js, options.js) - Modal handling and validation

Example usage flow:

// User clicks "Add New AI Class"
// Modal loads via AJAX: ?request=modal&modal=ai_class&id=0
// Form submits to: ?view=ai_class&action=save
// Action handler uses getFormChanges() with parameterized queries
// Redirects back to: ?view=options&tab=ai

Security

  • Parameterized queries with validCardinal() / validInt() sanitization
  • Output escaped via validHtmlStr()
  • CSRF tokens in forms
  • Permission checks on all mutations

Integration

  • Registered 'ai' in submenu after 'auth'
  • Added translations for AddNewAIClass, AIClass, ClassName, ClassIndex, Model
  • Bootstrap table configuration for responsive design

Follows existing patterns from Storage/Servers tabs.

Original prompt

Overview

Add database schema and user interface for managing AI object detection capabilities in ZoneMinder. This includes tables for AI models, object classes, detection settings per monitor, and actual detection results.

Database Schema Changes

Create new SQL file db/AI_Models.sql with the following tables:

1. AI_Models Table

Stores AI models being used for detection:

CREATE TABLE `AI_Models` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `Name` varchar(64) NOT NULL,
  `Description` TEXT,
  `ModelPath` varchar(255),
  `Framework` enum('TensorFlow','PyTorch','ONNX','Other') NOT NULL default 'ONNX',
  `Version` varchar(32),
  PRIMARY KEY (`Id`),
  UNIQUE KEY `AI_Models_Name_idx` (`Name`)
) ENGINE=@ZM_MYSQL_ENGINE@;

2. AI_Object_Classes Table

Stores object classes that can be detected by each model:

CREATE TABLE `AI_Object_Classes` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `ModelId` int(10) unsigned NOT NULL,
  `ClassName` varchar(64) NOT NULL,
  `ClassIndex` int(10) unsigned NOT NULL,
  `Description` TEXT,
  PRIMARY KEY (`Id`),
  KEY `AI_Object_Classes_ModelId_idx` (`ModelId`),
  UNIQUE KEY `AI_Object_Classes_Model_Class_idx` (`ModelId`, `ClassName`),
  FOREIGN KEY (`ModelId`) REFERENCES `AI_Models` (`Id`) ON DELETE CASCADE
) ENGINE=@ZM_MYSQL_ENGINE@;

3. AI_Detection_Settings Table

Stores detection settings per monitor and object class (MonitorId can be NULL for defaults):

CREATE TABLE `AI_Detection_Settings` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `MonitorId` int(10) unsigned NULL,
  `ObjectClassId` int(10) unsigned NOT NULL,
  `Enabled` tinyint(1) unsigned NOT NULL default 1,
  `ReportDetection` tinyint(1) unsigned NOT NULL default 1,
  `ConfidenceThreshold` tinyint(3) unsigned NOT NULL default 50,
  `BoxColor` varchar(7) NOT NULL default '#FF0000',
  PRIMARY KEY (`Id`),
  KEY `AI_Detection_Settings_MonitorId_idx` (`MonitorId`),
  KEY `AI_Detection_Settings_ObjectClassId_idx` (`ObjectClassId`),
  UNIQUE KEY `AI_Detection_Settings_Monitor_Object_idx` (`MonitorId`, `ObjectClassId`),
  FOREIGN KEY (`MonitorId`) REFERENCES `Monitors` (`Id`) ON DELETE CASCADE,
  FOREIGN KEY (`ObjectClassId`) REFERENCES `AI_Object_Classes` (`Id`) ON DELETE CASCADE
) ENGINE=@ZM_MYSQL_ENGINE@;

4. AI_Detections Table

Stores actual detection results:

CREATE TABLE `AI_Detections` (
  `Id` BIGINT unsigned NOT NULL auto_increment,
  `EventId` BIGINT unsigned NOT NULL,
  `FrameId` int(10) unsigned,
  `ObjectClassId` int(10) unsigned NOT NULL,
  `Confidence` decimal(5,4) NOT NULL,
  `BoundingBoxX` int(10) unsigned,
  `BoundingBoxY` int(10) unsigned,
  `BoundingBoxWidth` int(10) unsigned,
  `BoundingBoxHeight` int(10) unsigned,
  `DetectedAt` TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`Id`),
  KEY `AI_Detections_EventId_idx` (`EventId`),
  KEY `AI_Detections_FrameId_idx` (`FrameId`),
  KEY `AI_Detections_ObjectClassId_idx` (`ObjectClassId`),
  FOREIGN KEY (`EventId`) REFERENCES `Events` (`Id`) ON DELETE CASCADE,
  FOREIGN KEY (`FrameId`) REFERENCES `Frames` (`FrameId`) ON DELETE SET NULL,
  FOREIGN KEY (`ObjectClassId`) REFERENCES `AI_Object_Classes` (`Id`) ON DELETE CASCADE
) ENGINE=@ZM_MYSQL_ENGINE@;

UI Implementation

1. Create AI Classes Management UI

Create web/skins/classic/views/_options_ai_classes.php:

  • Table listing all AI Object Classes
  • Columns: Id, Model, ClassName, ClassIndex, Description, Mark (checkbox)
  • Add New AI Class button
  • Delete button (enabled when items are marked)
  • Click on row to edit
  • Follow ZoneMinder patterns (similar to storage/servers tabs)

2. Create AI Class Editor Modal

Create web/skins/classic/views/ai_class.php:

  • Modal popup for adding/editing AI Object Classes
  • Fields: Model (dropdown), ClassName, ClassIndex, Description
  • Save/Cancel buttons
  • Validation for required fields

3. Create JavaScript Handler

Create web/skins/classic/views/js/ai_classes.js:

  • Handle Add New button click - open modal popup
  • Handle row clicks for editing - open modal with class ID
  • Handle delete button enable/disable based on checkbox selection
  • Form validation before submit

4. Create Backend Action Handler

Create web/includes/actions/ai_class.php:

  • Handle 'save' action (INSERT or UPDATE AI_Object_Classes)
  • Handle 'delete' action (DELETE from AI_Object_Classes based on markIds)
  • Permission checks using canEdit('System')
  • Redirect back to options&tab=ai after save/delete

5. Update Options Page

Modify web/skins/classic/views/options.php around line 142:

} else if ($tab == 'ai') {
  include('_options_ai_classes.php');

6. Register AI Tab in Submenu

Modify web/skins/classic/includes/functions.php around line 984:
Add 'ai' to the $categoryDisplayOrder array in buildSubMenuOptions:

$zmMenu::buildSubMenuOptions($categoryDisplayOrder = [
    'display',
    'system',
    'auth',
    'ai',        // ADD THIS
    'config',
    // ... re...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Copilot AI changed the title [WIP] Add database schema for AI object detection capabilities Add database schema and UI for AI object detection management Dec 22, 2025
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.

2 participants