-- Microscopy Object Detection Application - Database Schema -- SQLite Database Schema for storing models, images, detections, and annotations -- Models table: stores trained model information CREATE TABLE IF NOT EXISTS models ( id INTEGER PRIMARY KEY AUTOINCREMENT, model_name TEXT NOT NULL, model_version TEXT NOT NULL, model_path TEXT NOT NULL, base_model TEXT NOT NULL DEFAULT 'yolov8s.pt', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, training_params TEXT, -- JSON string of training parameters metrics TEXT, -- JSON string of validation metrics UNIQUE(model_name, model_version) ); -- Images table: stores image metadata CREATE TABLE IF NOT EXISTS images ( id INTEGER PRIMARY KEY AUTOINCREMENT, relative_path TEXT NOT NULL UNIQUE, filename TEXT NOT NULL, width INTEGER, height INTEGER, captured_at TIMESTAMP, added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, checksum TEXT ); -- Detections table: stores detection results CREATE TABLE IF NOT EXISTS detections ( id INTEGER PRIMARY KEY AUTOINCREMENT, image_id INTEGER NOT NULL, model_id INTEGER NOT NULL, class_name TEXT NOT NULL, x_min REAL NOT NULL CHECK(x_min >= 0 AND x_min <= 1), y_min REAL NOT NULL CHECK(y_min >= 0 AND y_min <= 1), x_max REAL NOT NULL CHECK(x_max >= 0 AND x_max <= 1), y_max REAL NOT NULL CHECK(y_max >= 0 AND y_max <= 1), confidence REAL NOT NULL CHECK(confidence >= 0 AND confidence <= 1), segmentation_mask TEXT, -- JSON string of polygon coordinates [[x1,y1], [x2,y2], ...] detected_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, metadata TEXT, -- JSON string for additional metadata FOREIGN KEY (image_id) REFERENCES images (id) ON DELETE CASCADE, FOREIGN KEY (model_id) REFERENCES models (id) ON DELETE CASCADE ); -- Annotations table: stores manual annotations (future feature) CREATE TABLE IF NOT EXISTS annotations ( id INTEGER PRIMARY KEY AUTOINCREMENT, image_id INTEGER NOT NULL, class_name TEXT NOT NULL, x_min REAL NOT NULL CHECK(x_min >= 0 AND x_min <= 1), y_min REAL NOT NULL CHECK(y_min >= 0 AND y_min <= 1), x_max REAL NOT NULL CHECK(x_max >= 0 AND x_max <= 1), y_max REAL NOT NULL CHECK(y_max >= 0 AND y_max <= 1), segmentation_mask TEXT, -- JSON string of polygon coordinates [[x1,y1], [x2,y2], ...] annotator TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, verified BOOLEAN DEFAULT 0, FOREIGN KEY (image_id) REFERENCES images (id) ON DELETE CASCADE ); -- Create indexes for performance optimization CREATE INDEX IF NOT EXISTS idx_detections_image_id ON detections(image_id); CREATE INDEX IF NOT EXISTS idx_detections_model_id ON detections(model_id); CREATE INDEX IF NOT EXISTS idx_detections_class_name ON detections(class_name); CREATE INDEX IF NOT EXISTS idx_detections_detected_at ON detections(detected_at); CREATE INDEX IF NOT EXISTS idx_detections_confidence ON detections(confidence); CREATE INDEX IF NOT EXISTS idx_images_relative_path ON images(relative_path); CREATE INDEX IF NOT EXISTS idx_images_added_at ON images(added_at); CREATE INDEX IF NOT EXISTS idx_annotations_image_id ON annotations(image_id); CREATE INDEX IF NOT EXISTS idx_models_created_at ON models(created_at);