diff --git a/Modules/Segmentation/Interactions/mitkSegmentAnythingProcessExecutor.h b/Modules/Segmentation/Interactions/mitkSegmentAnythingProcessExecutor.h index ae04662be4..8dace266f6 100644 --- a/Modules/Segmentation/Interactions/mitkSegmentAnythingProcessExecutor.h +++ b/Modules/Segmentation/Interactions/mitkSegmentAnythingProcessExecutor.h @@ -1,64 +1,63 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef mitkSegmentAnythingProcessExecutor_h #define mitkSegmentAnythingProcessExecutor_h #include #include #include #include namespace mitk { /** * @brief You may register an observer for an ExternalProcessOutputEvent, ExternalProcessStdOutEvent or * ExternalProcessStdErrEvent in order to get notified of any output. * @remark The events will only be invoked if the pipes are NOT(!) shared. By default the pipes are not shared. * */ class MITKSEGMENTATION_EXPORT SegmentAnythingProcessExecutor : public mitk::ProcessExecutor { public: using Self = SegmentAnythingProcessExecutor; using Superclass = mitk::ProcessExecutor; using Pointer = ::itk::SmartPointer; using ConstPointer = ::itk::SmartPointer; using mitk::ProcessExecutor::Execute; - itkTypeMacro(SegmentAnythingProcessExecutor, ::itk::Object); mitkNewMacro1Param(SegmentAnythingProcessExecutor, double&); itkSetMacro(Stop, bool); itkGetConstMacro(Stop, bool); /** * @brief Executes the process. This version assumes that the executable name is the first argument in the argument * list and has already been converted to its OS dependent name via the static convert function of this class. */ bool Execute(const std::string &executionPath, const ArgumentListType &argumentList); void SetTimeout(double &timeout); protected: SegmentAnythingProcessExecutor() = delete; SegmentAnythingProcessExecutor(double &); ~SegmentAnythingProcessExecutor() = default; private: itksysProcess *m_ProcessID = nullptr; double m_Timeout; bool m_Stop = false; // Exit token to force stop the daemon. }; } // namespace mitk #endif diff --git a/Modules/Segmentation/Interactions/mitkSegmentAnythingPythonService.h b/Modules/Segmentation/Interactions/mitkSegmentAnythingPythonService.h index 9df2b48605..1e54928e6b 100644 --- a/Modules/Segmentation/Interactions/mitkSegmentAnythingPythonService.h +++ b/Modules/Segmentation/Interactions/mitkSegmentAnythingPythonService.h @@ -1,152 +1,151 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef mitkSegmentAnythingPythonService_h #define mitkSegmentAnythingPythonService_h #include #include #include #include #include #include #include namespace mitk { /** * @brief Segment Anything Model Python process handler class. * */ class MITKSEGMENTATION_EXPORT SegmentAnythingPythonService : public itk::Object { public: enum Status { READY, OFF, KILLED, CUDAError }; SegmentAnythingPythonService(std::string, std::string, std::string, unsigned int); ~SegmentAnythingPythonService(); itkSetMacro(MitkTempDir, std::string); itkGetConstMacro(MitkTempDir, std::string); /** * @brief Static function to print out everything from itk::EventObject. * Used as callback in mitk::ProcessExecutor object. * */ static void onPythonProcessEvent(itk::Object*, const itk::EventObject&, void*); /** * @brief Checks CurrentStatus enum variable and returns * true if daemon is READY (to read files) state, false is OFF state or * throws exception if daemon is found KILL or Cuda error state. * * @return bool */ static bool CheckStatus() /*throw(mitk::Exception)*/; /** * @brief Creates temp directories and calls start_python_daemon * function async. * */ void StartAsyncProcess(); /** * @brief Writes KILL to the control file to stop the daemon process. * */ void StopAsyncProcess(); /** * @brief Writes image as nifity file with unique id (UId) as file name. * */ void TransferImageToProcess(const Image*, std::string &UId); /** * @brief Writes csv stringstream of points to a csv file for * python daemon to read. * */ void TransferPointsToProcess(std::stringstream&); /** * @brief Waits for output nifity file from the daemon to appear and * reads it as a mitk::Image * * @return Image::Pointer */ LabelSetImage::Pointer RetrieveImageFromProcess(long timeOut= -1); static Status CurrentStatus; private: /** * @brief Runs SAM python daemon using mitk::ProcessExecutor * */ void start_python_daemon(); /** * @brief Writes stringstream content into control file. * */ void WriteControlFile(std::stringstream&); /** * @brief Create a Temp Dirs * */ void CreateTempDirs(const std::string&); /** * @brief ITK-based file writer for dumping inputs into python daemon * */ template void ITKWriter(const itk::Image *image, std::string& outputFilename); std::string m_MitkTempDir; std::string m_PythonPath; std::string m_ModelType; std::string m_CheckpointPath; std::string m_InDir, m_OutDir; std::string m_CurrentUId; int m_GpuId = 0; const std::string PARENT_TEMP_DIR_PATTERN = "mitk-sam-XXXXXX"; const std::string TRIGGER_FILENAME = "trigger.csv"; const std::string SAM_PYTHON_FILE_NAME = "run_inference_daemon.py"; std::future m_Future; - //ProcessExecutor::Pointer m_DaemonExec; SegmentAnythingProcessExecutor::Pointer m_DaemonExec; }; struct SIGNALCONSTANTS { static const std::string READY; static const std::string KILL; static const std::string OFF; static const std::string CUDA_OUT_OF_MEMORY_ERROR; static const std::string TIMEOUT_ERROR; }; } // namespace #endif