Medical Imaging Interaction Toolkit  2025.12.02
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 
87 
93 
98  void TransferImageToProcess(const Image *inputAtTimeStep, std::string &UId);
99 
105  void TransferPointsToProcess(const std::string &triggerCSV) const;
106 
113  MultiLabelSegmentation::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
#define MITKSEGMENTATION_EXPORT
Image class for storing images.
Definition: mitkImage.h:70
Segment Anything Model Python process handler class.
static bool CheckStatus()
Checks CurrentStatus enum variable and returns true if daemon is READY (to read files) state,...
static void onPythonProcessEvent(itk::Object *, const itk::EventObject &, void *)
Static function to print out everything from itk::EventObject. Used as callback in mitk::ProcessExecu...
void TransferImageToProcess(const Image *inputAtTimeStep, std::string &UId)
Writes image as nifity file with unique id (UId) as file name.
~SegmentAnythingPythonService()
Destroy the Segment Anything Python Service object. Stop the async python process and deletes tempora...
mitkClassMacroItkParent(SegmentAnythingPythonService, itk::Object)
mitkNewMacro5Param(SegmentAnythingPythonService, std::string, std::string, std::string, unsigned int, std::string)
void StopAsyncProcess()
Writes KILL to the control file to stop the daemon process.
void TransferPointsToProcess(const std::string &triggerCSV) const
Writes csv stringstream of points to a csv file for python daemon to read.
void StartAsyncProcess()
Creates temp directories and calls start_python_daemon function async.
SegmentAnythingPythonService(std::string workingDir, std::string modelType, std::string checkPointPath, unsigned int gpuId, std::string backend)
Construct a new Segment Anything Python Service object. Specify working directory,...
MultiLabelSegmentation::Pointer RetrieveImageFromProcess(long timeOut=-1) const
Waits for output nifity file from the daemon to appear and reads it as a mitk::Image.
Find image slices visible on a given plane.
static const std::string CUDA_OUT_OF_MEMORY_ERROR
static const std::string TIMEOUT_ERROR