-- 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 ); -- Object classes table: stores annotation class definitions with colors CREATE TABLE IF NOT EXISTS object_classes ( id INTEGER PRIMARY KEY AUTOINCREMENT, class_name TEXT NOT NULL UNIQUE, color TEXT NOT NULL, -- Hex color code (e.g., '#FF0000') created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, description TEXT ); -- Insert default object classes INSERT OR IGNORE INTO object_classes (class_name, color, description) VALUES ('cell', '#FF0000', 'Cell object'), ('nucleus', '#00FF00', 'Cell nucleus'), ('mitochondria', '#0000FF', 'Mitochondria'), ('vesicle', '#FFFF00', 'Vesicle'); -- Annotations table: stores manual annotations CREATE TABLE IF NOT EXISTS annotations ( id INTEGER PRIMARY KEY AUTOINCREMENT, image_id INTEGER NOT NULL, class_id INTEGER 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, FOREIGN KEY (class_id) REFERENCES object_classes (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_annotations_class_id ON annotations(class_id); CREATE INDEX IF NOT EXISTS idx_models_created_at ON models(created_at); CREATE INDEX IF NOT EXISTS idx_object_classes_class_name ON object_classes(class_name);