Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

Implements database schema and admin UI for managing AI object detection models, datasets, and classes in ZoneMinder. Separates concerns between AI datasets (COCO, ImageNet), model implementations (YOLOv8, SSD), and object classes, with per-monitor detection settings.

Database Schema

Five interconnected tables in db/AI_Models.sql:

  • AI_Datasets: Dataset registry (COCO, ImageNet, custom)
  • AI_Models: Model implementations with framework support (TensorFlow, PyTorch, ONNX, OpenVINO, TensorRT)
  • AI_Object_Classes: Object classes indexed by dataset
  • AI_Detection_Settings: Per-monitor detection config (MonitorId NULL = global default)
  • AI_Detections: Detection results with bounding boxes, linked to Events/Frames

Pre-populated COCO 2017 dataset with 80 object classes in db/coco_dataset.sql. Default detection settings enabled for person (60% threshold) and vehicles (50% threshold).

UI Implementation

Three new tabs in Options menu following existing Storage/Servers patterns:

  • AI Datasets: Manage datasets (name, version, class count)
  • AI Models: Manage models with framework selection and dataset linkage
  • AI Object Classes: Manage classes with dataset filtering

Modal editors for CRUD operations, backend action handlers in web/includes/actions/, JavaScript consolidated in existing options.js.

Architecture

AI_Datasets (1) ─┬─→ (N) AI_Models
                 └─→ (N) AI_Object_Classes ─┬─→ (N) AI_Detection_Settings ←─ (N) Monitors
                                             └─→ (N) AI_Detections ─→ Events/Frames

Foreign keys enforce referential integrity. INSERT IGNORE for idempotency. Uses subqueries instead of hardcoded IDs for resilience.

All UI components follow ZoneMinder conventions: canEdit('System') permission checks, makeLink() clickable rows, Bootstrap styling, CSRF protection, helper functions (dbQuery(), validHtmlStr(), getFormChanges()).

Original prompt

Overview

REVISION: Updated architecture with proper separation between AI Models, Datasets, and Object Classes

Add comprehensive database schema and user interfaces for managing AI object detection in ZoneMinder. This includes proper separation between AI model implementations, datasets (like COCO), object classes, detection settings per monitor, and detection results.

Database Schema Changes

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

1. AI_Datasets Table

Stores AI datasets (COCO, ImageNet, custom datasets):

CREATE TABLE `AI_Datasets` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `Name` varchar(64) NOT NULL,
  `Description` TEXT,
  `Version` varchar(32),
  `NumClasses` int(10) unsigned NOT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `AI_Datasets_Name_idx` (`Name`)
) ENGINE=@ZM_MYSQL_ENGINE@;

2. AI_Models Table

Stores AI model implementations (YOLOv8, SSD, etc.):

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','OpenVINO','TensorRT','Other') NOT NULL default 'ONNX',
  `Version` varchar(32),
  `DatasetId` int(10) unsigned,
  `Enabled` tinyint(1) unsigned NOT NULL default 0,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `AI_Models_Name_idx` (`Name`),
  FOREIGN KEY (`DatasetId`) REFERENCES `AI_Datasets` (`Id`) ON DELETE SET NULL
) ENGINE=@ZM_MYSQL_ENGINE@;

3. AI_Object_Classes Table

Stores object classes from datasets:

CREATE TABLE `AI_Object_Classes` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `DatasetId` 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_DatasetId_idx` (`DatasetId`),
  UNIQUE KEY `AI_Object_Classes_Dataset_Class_idx` (`DatasetId`, `ClassName`),
  FOREIGN KEY (`DatasetId`) REFERENCES `AI_Datasets` (`Id`) ON DELETE CASCADE
) ENGINE=@ZM_MYSQL_ENGINE@;

4. AI_Detection_Settings Table

Stores detection settings per monitor and object class (MonitorId NULL = default):

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@;

5. 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@;

COCO Dataset Pre-population

Create db/coco_dataset.sql:

-- Insert COCO 2017 Dataset
INSERT IGNORE INTO AI_Datasets (Id, Name, Description, Version, NumClasses) VALUES 
(1, 'COCO', 'Microsoft Common Objects in Context', '2017', 80);

-- Insert all 80 COCO object classes with correct indices (0-79)
INSERT IGNORE INTO AI_Object_Classes (DatasetId, ClassName, ClassIndex, Description) VALUES
(1, 'person', 0, 'Person'),
(1, 'bicycle', 1, 'Bicycle'),
(1, 'car', 2, 'Car'),
(1, 'motorcycle', 3, 'Motorcycle'),
(1, 'airplane', 4, 'Airplane'),
(1, 'bus', 5, 'Bus'),
(1, 'train', 6, 'Train'),
(1, 'truck', 7, 'Truck'),
(1, 'boat', 8, 'Boat'),
(1, 'traffic light', 9, 'Traffic light'),
(1, 'fire hydrant', 10, 'Fire hydrant'),
(1, 'stop sign', 11, 'Stop sign'),
(1, 'parking meter', 12, 'Parking meter'),
(1, 'bench', 13, 'Bench'),
(1, 'bird', 14, 'Bird'),
(1, 'cat', 15, 'Cat'),
(1, 'dog', 16, 'Dog'),
(1, 'horse', 17, 'Horse'),
(1, 'sheep', 18, 'Sheep'),
(1, 'cow', 19, 'Cow'),
(1, 'elephant', 20, 'Elephant'),
(1, 'bear', 2...

</details>



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

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

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

✨ Let Copilot coding agent [set things up for you](https://github.com/ZoneMinder/zoneminder/issues/new?title=+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 22, 2025 18:29
- Created db/AI_Models.sql with 5 tables for AI management
- Created db/coco_dataset.sql with COCO dataset pre-population
- Added UI views for AI Datasets, Models, and Object Classes
- Added modal editors and JavaScript handlers for each component
- Added backend action handlers for CRUD operations
- Integrated new tabs into options.php and menu system

Co-authored-by: connortechnology <[email protected]>
- Integrated AI Datasets, Models, and Classes handlers into options.js
- Removed separate JS files to follow ZoneMinder pattern
- Added dataset filter functionality for AI Classes tab

Co-authored-by: connortechnology <[email protected]>
- Improved jQuery selector efficiency in dataset filter
- Use subquery for DatasetId to be more resilient to insertion order

Co-authored-by: connortechnology <[email protected]>
Copilot AI changed the title [WIP] Add database schema and user interfaces for AI detection management Add AI Models database schema and management UI 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