Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | import { getEnabledElementByViewportId } from '@cornerstonejs/core'; import { LabelmapBaseTool, ToolGroupManager, annotation, ProbeTool, addTool, } from '@cornerstonejs/tools'; import ONNXSegmentationController from './ONNXSegmentationController'; /** * Represents a tool used for segment selection and AI-assisted segmentation. * Integrates with ONNX models for automated segmentation tasks. */ class MarkerLabelmapTool extends LabelmapBaseTool { static toolName = 'MarkerLabelmap'; private segmentAI: ONNXSegmentationController; private _initialized = false; private _modelInitialized = false; private _toolsAdded = false; constructor( toolProps = {}, defaultToolProps = { supportedInteractionTypes: ['Mouse', 'Touch'], configuration: { sourceViewportId: '', modelName: 'sam_b', enabled: false, models: { sam_b: [ { name: 'sam-b-encoder', url: 'https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b_01ec64.encoder-fp16.onnx', size: 180, key: 'encoder', }, { name: 'sam-b-decoder', url: 'https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b_01ec64.decoder.onnx', size: 17, key: 'decoder', }, ], }, numRandomPoints: 5, searchBreadth: 10, }, } ) { super(toolProps, defaultToolProps); } /** * Checks if the tool should resolve preview requests. * This is used to determine if the tool is in a state where it can handle * preview requests. * @returns True if the tool should resolve preview requests, false otherwise. */ public shouldResolvePreviewRequests() { const MARKER_TOOLS = [ ONNXSegmentationController.MarkerInclude, ONNXSegmentationController.MarkerExclude, ONNXSegmentationController.BoxPrompt, ]; const toolGroup = this._getToolGroupId(); if (!toolGroup) { console.debug( `Tool group not found for tool: ${MarkerLabelmapTool.toolName}` ); return false; } return ( this.hasPreviewData() && MARKER_TOOLS.some((markerToolName) => { if (toolGroup.hasTool(markerToolName)) { const instance = toolGroup.getToolInstance(markerToolName); return instance.mode === 'Active' || instance.mode === 'Enabled'; } }) ); } _init = async () => { const { configuration } = this; if (!configuration.enabled || !configuration.sourceViewportId) { return; } // Create the controller only once if (!this.segmentAI) { this.segmentAI = new ONNXSegmentationController({ models: configuration.models, modelName: configuration.modelName, }); } // Set up the tool instances if not already done if (!this._toolsAdded) { this._addToolInstances(); } // Enable the controller this.segmentAI.enabled = true; // Initialize model only once if (!this._modelInitialized) { await this.segmentAI.initModel(); this._modelInitialized = true; } // Set up viewport const { sourceViewportId } = this.configuration; const enabledElement = getEnabledElementByViewportId(sourceViewportId); if (!enabledElement) { console.debug( 'No enabled element found for viewportId:', sourceViewportId ); return; } const { viewport } = enabledElement; this.segmentAI.initViewport(viewport); this._initialized = true; }; _getToolGroupId = () => { return ToolGroupManager.getToolGroupForViewport( this.configuration.sourceViewportId ); }; _addToolInstances = () => { // Get tool group const toolGroup = this._getToolGroupId(); if (!toolGroup) { console.debug(`Tool group not found`); return; } // Add marker tools addTool(ProbeTool); const MarkerIncludeToolName = ONNXSegmentationController.MarkerInclude; const MarkerExcludeToolName = ONNXSegmentationController.MarkerExclude; const BoxPromptToolName = ONNXSegmentationController.BoxPrompt; // MarkerInclude - a probe variant with primary mouse button toolGroup.addToolInstance(MarkerIncludeToolName, ProbeTool.toolName, { getTextLines: () => null, }); // toolGroup.setToolActive(MarkerIncludeToolName, { // bindings: [ // { mouseButton: MouseBindings.Primary }, // { // mouseButton: MouseBindings.Primary, // modifierKey: KeyboardBindings.Shift, // }, // ], // }); // MarkerExclude - a probe variant with Ctrl+click toolGroup.addToolInstance(MarkerExcludeToolName, ProbeTool.toolName, { getTextLines: () => null, }); // toolGroup.setToolActive(MarkerExcludeToolName, { // bindings: [ // { // mouseButton: MouseBindings.Primary, // modifierKey: KeyboardBindings.Ctrl, // }, // ], // }); // Set tool colors for marker tools annotation.config.style.setToolGroupToolStyles(toolGroup.id, { [MarkerIncludeToolName]: { color: 'rgb(0, 255, 0)', // Green colorHighlighted: 'rgb(0, 255, 0)', colorSelected: 'rgb(0, 255, 0)', }, [MarkerExcludeToolName]: { color: 'rgb(255, 0, 0)', // Red colorHighlighted: 'rgb(255, 0, 0)', colorSelected: 'rgb(255, 0, 0)', }, }); // BoxPrompt - a rectangle ROI variant // toolGroup.addToolInstance(BoxPromptToolName, RectangleROITool.toolName, { // getTextLines: () => null, // }); // toolGroup.setToolActive(BoxPromptToolName, { // bindings: [{ mouseButton: MouseBindings.Primary }], // }); this._toolsAdded = true; }; onSetToolConfiguration = (): void => { this._init(); }; onSetToolEnabled = async (): Promise<void> => { this.configuration.enabled = true; if (this.segmentAI) { this.segmentAI.enabled = true; } if (!this._initialized) { await this._init(); } }; onSetToolActive = (): void => { this.configuration.enabled = true; if (this.segmentAI) { this.segmentAI.enabled = true; } if (!this._initialized) { this._init(); } }; interpolateScroll = (): void => { this.segmentAI.interpolateScroll(); }; onSetToolDisabled = (): void => { this.configuration.enabled = false; if (this.segmentAI) { this.segmentAI.enabled = false; } }; /** * Clear all segmentations in the current viewport */ clearMarkers = (): void => { if (!this.segmentAI) { return; } const { sourceViewportId } = this.configuration; const enabledElement = getEnabledElementByViewportId(sourceViewportId); if (!enabledElement) { return; } const { viewport } = enabledElement; this.segmentAI.clear(viewport); viewport.render(); }; /** * Remove prompt annotations without clearing segmentations */ removePromptAnnotations = (): void => { if (!this.segmentAI) { return; } const { viewportId } = this.configuration; const enabledElement = getEnabledElementByViewportId(viewportId); if (!enabledElement) { return; } this.segmentAI.removePromptAnnotationsWithCache(enabledElement.viewport); }; /** * Accept the current segmentation preview */ acceptPreview = (): void => { if (!this.segmentAI) { return; } const { sourceViewportId } = this.configuration; const enabledElement = getEnabledElementByViewportId(sourceViewportId); if (!enabledElement) { return; } this.segmentAI.acceptPreview(enabledElement.viewport.element); }; /** * Reject the current segmentation preview */ rejectPreview = (): void => { if (!this.segmentAI) { return; } const { sourceViewportId } = this.configuration; const enabledElement = getEnabledElementByViewportId(sourceViewportId); if (!enabledElement) { return; } this.segmentAI.rejectPreview(enabledElement.viewport.element); }; } export default MarkerLabelmapTool; |