Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
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 #include <mitkCommon.h>
24 
25 namespace mitk
26 {
32  {
33  public:
34  enum Status
35  {
37  OFF,
39  CUDAError
40  };
42 
53  SegmentAnythingPythonService(std::string workingDir, std::string modelType,
54  std::string checkPointPath, unsigned int gpuId, std::string backend);
55 
61 
62  itkSetMacro(MitkTempDir, std::string);
63  itkGetConstMacro(MitkTempDir, std::string);
64  mitkNewMacro5Param(SegmentAnythingPythonService, std::string, std::string, std::string, unsigned int, std::string);
70  static void onPythonProcessEvent(itk::Object*, const itk::EventObject&, void*);
71 
79  static bool CheckStatus() /*throw(mitk::Exception)*/;
80 
86  void StartAsyncProcess();
87 
92  void StopAsyncProcess();
93 
98  void TransferImageToProcess(const Image *inputAtTimeStep, std::string &UId);
99 
105  void TransferPointsToProcess(const std::string &triggerCSV) const;
106 
113  LabelSetImage::Pointer RetrieveImageFromProcess(long timeOut= -1) const;
114 
116 
117  private:
122  void start_python_daemon() const;
123 
128  void WriteControlFile(const std::string &statusString) const;
129 
134  void CreateTempDirs(const std::string &dirPattern);
135 
140  template <typename TPixel, unsigned int VImageDimension>
141  void ITKWriter(const itk::Image<TPixel, VImageDimension> *image, std::string& outputFilename) const;
142 
143 
144  std::string m_MitkTempDir;
145  std::string m_PythonPath;
146  std::string m_ModelType;
147  std::string m_CheckpointPath;
148  std::string m_InDir, m_OutDir;
149  std::string m_Backend;
150  std::string m_CurrentUId;
151  int m_GpuId = 0;
152  const std::string PARENT_TEMP_DIR_PATTERN = "mitk-sam-XXXXXX";
153  const std::string TRIGGER_FILENAME = "trigger.csv";
154  const std::string SAM_PYTHON_FILE_NAME = "run_inference_daemon.py";
155  std::future<void> m_Future;
157  };
158 
160  {
161  static const std::string READY;
162  static const std::string KILL;
163  static const std::string OFF;
164  static const std::string CUDA_OUT_OF_MEMORY_ERROR;
165  static const std::string TIMEOUT_ERROR;
166  };
167 
168 } // namespace
169 
170 #endif
mitkImage.h
mitk::SIGNALCONSTANTS::CUDA_OUT_OF_MEMORY_ERROR
static const std::string CUDA_OUT_OF_MEMORY_ERROR
Definition: mitkSegmentAnythingPythonService.h:164
mitk::SIGNALCONSTANTS
Definition: mitkSegmentAnythingPythonService.h:159
mitk::SIGNALCONSTANTS::OFF
static const std::string OFF
Definition: mitkSegmentAnythingPythonService.h:163
mitkNewMacro5Param
#define mitkNewMacro5Param(classname, typea, typeb, typec, typed, typee)
Definition: mitkCommon.h:120
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
itk::SmartPointer< Self >
mitk::SegmentAnythingPythonService::Status
Status
Definition: mitkSegmentAnythingPythonService.h:34
mitk::SegmentAnythingPythonService
Segment Anything Model Python process handler class.
Definition: mitkSegmentAnythingPythonService.h:31
mitkClassMacroItkParent
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:45
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitkSegmentAnythingProcessExecutor.h
mitk::SegmentAnythingPythonService::OFF
@ OFF
Definition: mitkSegmentAnythingPythonService.h:37
mitkCommon.h
MITKSEGMENTATION_EXPORT
#define MITKSEGMENTATION_EXPORT
Definition: MitkSegmentationExports.h:15
mitk::SIGNALCONSTANTS::KILL
static const std::string KILL
Definition: mitkSegmentAnythingPythonService.h:162
mitk::SegmentAnythingPythonService::CurrentStatus
static Status CurrentStatus
Definition: mitkSegmentAnythingPythonService.h:115
mitk::SegmentAnythingPythonService::READY
@ READY
Definition: mitkSegmentAnythingPythonService.h:36
MitkSegmentationExports.h
mitk::SIGNALCONSTANTS::TIMEOUT_ERROR
static const std::string TIMEOUT_ERROR
Definition: mitkSegmentAnythingPythonService.h:165
mitk::SegmentAnythingPythonService::KILLED
@ KILLED
Definition: mitkSegmentAnythingPythonService.h:38
mitk::SIGNALCONSTANTS::READY
static const std::string READY
Definition: mitkSegmentAnythingPythonService.h:161
mitkLabelSetImage.h