Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
mitkSegmentAnythingPythonService.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkSegmentAnythingPythonService_h
14 #define mitkSegmentAnythingPythonService_h
15 
18 #include <thread>
19 #include <future>
20 #include <mitkImage.h>
21 #include <mitkLabelSetImage.h>
22 #include <itkImage.h>
23 
24 namespace mitk
25 {
31  {
32  public:
33  enum Status
34  {
36  OFF,
38  CUDAError
39  };
40 
51  SegmentAnythingPythonService(std::string workingDir, std::string modelType,
52  std::string checkPointPath, unsigned int gpuId, std::string backend);
53 
59 
60  itkSetMacro(MitkTempDir, std::string);
61  itkGetConstMacro(MitkTempDir, std::string);
62 
68  static void onPythonProcessEvent(itk::Object*, const itk::EventObject&, void*);
69 
77  static bool CheckStatus() /*throw(mitk::Exception)*/;
78 
84  void StartAsyncProcess();
85 
90  void StopAsyncProcess();
91 
96  void TransferImageToProcess(const Image *inputAtTimeStep, std::string &UId);
97 
103  void TransferPointsToProcess(const std::string &triggerCSV) const;
104 
111  LabelSetImage::Pointer RetrieveImageFromProcess(long timeOut= -1) const;
112 
114 
115  private:
120  void start_python_daemon() const;
121 
126  void WriteControlFile(const std::string &statusString) const;
127 
132  void CreateTempDirs(const std::string &dirPattern);
133 
138  template <typename TPixel, unsigned int VImageDimension>
139  void ITKWriter(const itk::Image<TPixel, VImageDimension> *image, std::string& outputFilename) const;
140 
141 
142  std::string m_MitkTempDir;
143  std::string m_PythonPath;
144  std::string m_ModelType;
145  std::string m_CheckpointPath;
146  std::string m_InDir, m_OutDir;
147  std::string m_Backend;
148  std::string m_CurrentUId;
149  int m_GpuId = 0;
150  const std::string PARENT_TEMP_DIR_PATTERN = "mitk-sam-XXXXXX";
151  const std::string TRIGGER_FILENAME = "trigger.csv";
152  const std::string SAM_PYTHON_FILE_NAME = "run_inference_daemon.py";
153  std::future<void> m_Future;
155  };
156 
158  {
159  static const std::string READY;
160  static const std::string KILL;
161  static const std::string OFF;
162  static const std::string CUDA_OUT_OF_MEMORY_ERROR;
163  static const std::string TIMEOUT_ERROR;
164  };
165 
166 } // namespace
167 
168 #endif
mitkImage.h
mitk::SIGNALCONSTANTS::CUDA_OUT_OF_MEMORY_ERROR
static const std::string CUDA_OUT_OF_MEMORY_ERROR
Definition: mitkSegmentAnythingPythonService.h:162
mitk::SIGNALCONSTANTS
Definition: mitkSegmentAnythingPythonService.h:157
mitk::SIGNALCONSTANTS::OFF
static const std::string OFF
Definition: mitkSegmentAnythingPythonService.h:161
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
itk::SmartPointer< Self >
mitk::SegmentAnythingPythonService::Status
Status
Definition: mitkSegmentAnythingPythonService.h:33
mitk::SegmentAnythingPythonService
Segment Anything Model Python process handler class.
Definition: mitkSegmentAnythingPythonService.h:30
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitkSegmentAnythingProcessExecutor.h
mitk::SegmentAnythingPythonService::OFF
@ OFF
Definition: mitkSegmentAnythingPythonService.h:36
MITKSEGMENTATION_EXPORT
#define MITKSEGMENTATION_EXPORT
Definition: MitkSegmentationExports.h:15
mitk::SIGNALCONSTANTS::KILL
static const std::string KILL
Definition: mitkSegmentAnythingPythonService.h:160
mitk::SegmentAnythingPythonService::CurrentStatus
static Status CurrentStatus
Definition: mitkSegmentAnythingPythonService.h:113
mitk::SegmentAnythingPythonService::READY
@ READY
Definition: mitkSegmentAnythingPythonService.h:35
MitkSegmentationExports.h
mitk::SIGNALCONSTANTS::TIMEOUT_ERROR
static const std::string TIMEOUT_ERROR
Definition: mitkSegmentAnythingPythonService.h:163
mitk::SegmentAnythingPythonService::KILLED
@ KILLED
Definition: mitkSegmentAnythingPythonService.h:37
mitk::SIGNALCONSTANTS::READY
static const std::string READY
Definition: mitkSegmentAnythingPythonService.h:159
mitkLabelSetImage.h