Medical Imaging Interaction Toolkit  2023.12.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 
41  SegmentAnythingPythonService(std::string, std::string, std::string, unsigned int);
43 
44  itkSetMacro(MitkTempDir, std::string);
45  itkGetConstMacro(MitkTempDir, std::string);
46 
52  static void onPythonProcessEvent(itk::Object*, const itk::EventObject&, void*);
53 
61  static bool CheckStatus() /*throw(mitk::Exception)*/;
62 
68  void StartAsyncProcess();
69 
74  void StopAsyncProcess();
75 
80  void TransferImageToProcess(const Image*, std::string &UId);
81 
87  void TransferPointsToProcess(std::stringstream&);
88 
95  LabelSetImage::Pointer RetrieveImageFromProcess(long timeOut= -1);
96 
98 
99  private:
104  void start_python_daemon();
105 
110  void WriteControlFile(std::stringstream&);
111 
116  void CreateTempDirs(const std::string&);
117 
122  template <typename TPixel, unsigned int VImageDimension>
123  void ITKWriter(const itk::Image<TPixel, VImageDimension> *image, std::string& outputFilename);
124 
125 
126  std::string m_MitkTempDir;
127  std::string m_PythonPath;
128  std::string m_ModelType;
129  std::string m_CheckpointPath;
130  std::string m_InDir, m_OutDir;
131  std::string m_CurrentUId;
132  int m_GpuId = 0;
133  const std::string PARENT_TEMP_DIR_PATTERN = "mitk-sam-XXXXXX";
134  const std::string TRIGGER_FILENAME = "trigger.csv";
135  const std::string SAM_PYTHON_FILE_NAME = "run_inference_daemon.py";
136  std::future<void> m_Future;
138  };
139 
141  {
142  static const std::string READY;
143  static const std::string KILL;
144  static const std::string OFF;
145  static const std::string CUDA_OUT_OF_MEMORY_ERROR;
146  static const std::string TIMEOUT_ERROR;
147  };
148 
149 } // namespace
150 
151 #endif
mitkImage.h
mitk::SIGNALCONSTANTS::CUDA_OUT_OF_MEMORY_ERROR
static const std::string CUDA_OUT_OF_MEMORY_ERROR
Definition: mitkSegmentAnythingPythonService.h:145
mitk::SIGNALCONSTANTS
Definition: mitkSegmentAnythingPythonService.h:140
mitk::SIGNALCONSTANTS::OFF
static const std::string OFF
Definition: mitkSegmentAnythingPythonService.h:144
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:143
mitk::SegmentAnythingPythonService::CurrentStatus
static Status CurrentStatus
Definition: mitkSegmentAnythingPythonService.h:97
mitk::SegmentAnythingPythonService::READY
@ READY
Definition: mitkSegmentAnythingPythonService.h:35
MitkSegmentationExports.h
mitk::SIGNALCONSTANTS::TIMEOUT_ERROR
static const std::string TIMEOUT_ERROR
Definition: mitkSegmentAnythingPythonService.h:146
mitk::SegmentAnythingPythonService::KILLED
@ KILLED
Definition: mitkSegmentAnythingPythonService.h:37
mitk::SIGNALCONSTANTS::READY
static const std::string READY
Definition: mitkSegmentAnythingPythonService.h:142
mitkLabelSetImage.h