An interactive web-based tool for (i) visualizing surgical tools in a scene with description from MedGemma (ii) generating synthetic surgical training images with ground truth bounding box labels
git clone <repository-url>
cd surgical-tool-composite
Create a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
Activate the environment and configure the parameter cache_dir in the script. Run
python download_models.py
This step would download the various sub-modules needed to run the diffusion and medgemma pipeline. This step saves a lot of time when running the server.
The pre-trained Unet for the diffusion model needs to be downloaded from here.
cp config.yaml.example config.yaml
config.yaml with your paths:data:
no_tools_images_dir: "/path/to/your/base/images"
tool_regions_dir: "/path/to/your/tool_regions"
output_dir: "./output"
models:
sd_base_model: "stable-diffusion-v1-5/stable-diffusion-v1-5"
sd_unet_checkpoint: "/path/to/your/sd-distill/checkpoint"
huggingface_cache_dir: "/path/to/hf/cache"
medgemma_checkpoint: "/path/to/your/medgemma/checkpoint"
We need to create directories consisting of images without tools and some samples of different tools. Download the images and the tools and place them in a directory as shown below. For an exmple run, use the dataset directory for the different image and tools.
no_tools_images/
├── image_001.png
├── image_002.png
├── image_003.png
...
tool_regions/
├── bipolar/
│ ├── images/
│ │ ├── tool_001.png
│ │ ├── tool_002.png
│ │ ...
│ └── masks/
│ ├── tool_001.png
│ ├── tool_002.png
│ ...
├── grasper/
│ ├── images/
│ └── masks/
├── scissors/
│ ├── images/
│ └── masks/
...
Note: Each tool needs corresponding RGB images and grayscale masks where white (255) indicates the tool region.
python run_server.py
The server will:
http://localhost:5000Access the GUI: Open http://localhost:5000 in your browser
Select Mode:
Select Base Images: 4-6 surgical images are displayed. Click "Refresh Images" to load different ones.
Add Tools:
View Results:
The interface of the learning mode showing surgical tools and description from Medgemma
The different synthetic surgical images generated with bounding box from the generation mode
processing:
default_base_images: 4 # Number of images per session
image_resolution: 512 # Processing resolution
guidance_strength: 0.7 # SD guidance scale
num_inference_steps: 4 # SD inference steps
gpu:
device: "cuda" # cuda or cpu
optimize_memory: true # Enable memory optimizations
dtype: "float32" # float32 or float16
features:
use_medgemma: true # Enable image descriptions
use_diffusion: true # Enable SD refinement
draw_bboxes: true # Draw tool bounding boxes
run_server.py: Flask web server and API endpoints
/api/base_images: Get current base images/api/tools: Get available tools/api/process: Process selected images/api/download/<image_id>: Download processed image/api/refresh_images: Load new base imagespipeline_stages.py: Core image processing pipeline
ToolCompositor: Manages tool loading and compositingprocess_composite_stage(): Composite tool onto image + SD refinementinitialize_medgemma_pipeline(): Set up MedGemma for descriptionsrun_image_through_medgemma(): Generate image descriptionsSolution:
optimize_memory: true in GPU settingsuse_medgemma: falsenum_inference_steps to 1-4dtype: "float16" for lower memory usageSolution:
huggingface_cache_dir in confighuggingface-cli download google/medgemma-1.5-4b-it --cache-dir /your/cache/path
Solution:
nvidia-smidevice: "cpu" in configSolution:
tool_regions_dir structure matches expected layoutimages/ and masks/ subdirectoriesfrom pipeline_stages import ToolCompositor, load_config
# Load configuration
config = load_config('config.yaml')
# Initialize compositor
compositor = ToolCompositor(
no_tools_dir=config['data']['no_tools_images_dir'],
tool_regions_dir=config['data']['tool_regions_dir'],
output_dir=config['data']['output_dir']
)
# Load and composite a tool
tool_img, tool_mask = compositor.load_tool_sample('grasper', sample_idx=0)
base_img = Image.open('base_image.png').convert('RGB')
composite = compositor.paste_tool_excluding_white(base_img, tool_img, position=(10, 10))
Edit the prompt in process_composite_stage():
pipeline_args = {
"prompt": "your custom prompt here",
"guidance_scale": 4.5,
...
}
If you use this tool in your research, please cite:
@software{surgfast,
title={SurgFasT: Fast-tracking Surgical science with medGemma},
author={Danush Kumar Venkatesh},
year={2026},
}
For issues, questions, or contributions:
48 commits
Python
100.0%
An interactive web-based tool for (i) visualizing surgical tools in a scene with description from MedGemma (ii) generating synthetic surgical training images with ground truth bounding box labels
git clone <repository-url>
cd surgical-tool-composite
Create a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
Activate the environment and configure the parameter cache_dir in the script. Run
python download_models.py
This step would download the various sub-modules needed to run the diffusion and medgemma pipeline. This step saves a lot of time when running the server.
The pre-trained Unet for the diffusion model needs to be downloaded from here.
cp config.yaml.example config.yaml
config.yaml with your paths:data:
no_tools_images_dir: "/path/to/your/base/images"
tool_regions_dir: "/path/to/your/tool_regions"
output_dir: "./output"
models:
sd_base_model: "stable-diffusion-v1-5/stable-diffusion-v1-5"
sd_unet_checkpoint: "/path/to/your/sd-distill/checkpoint"
huggingface_cache_dir: "/path/to/hf/cache"
medgemma_checkpoint: "/path/to/your/medgemma/checkpoint"
We need to create directories consisting of images without tools and some samples of different tools. Download the images and the tools and place them in a directory as shown below. For an exmple run, use the dataset directory for the different image and tools.
no_tools_images/
├── image_001.png
├── image_002.png
├── image_003.png
...
tool_regions/
├── bipolar/
│ ├── images/
│ │ ├── tool_001.png
│ │ ├── tool_002.png
│ │ ...
│ └── masks/
│ ├── tool_001.png
│ ├── tool_002.png
│ ...
├── grasper/
│ ├── images/
│ └── masks/
├── scissors/
│ ├── images/
│ └── masks/
...
Note: Each tool needs corresponding RGB images and grayscale masks where white (255) indicates the tool region.
python run_server.py
The server will:
http://localhost:5000Access the GUI: Open http://localhost:5000 in your browser
Select Mode:
Select Base Images: 4-6 surgical images are displayed. Click "Refresh Images" to load different ones.
Add Tools:
View Results:
The interface of the learning mode showing surgical tools and description from Medgemma
The different synthetic surgical images generated with bounding box from the generation mode
processing:
default_base_images: 4 # Number of images per session
image_resolution: 512 # Processing resolution
guidance_strength: 0.7 # SD guidance scale
num_inference_steps: 4 # SD inference steps
gpu:
device: "cuda" # cuda or cpu
optimize_memory: true # Enable memory optimizations
dtype: "float32" # float32 or float16
features:
use_medgemma: true # Enable image descriptions
use_diffusion: true # Enable SD refinement
draw_bboxes: true # Draw tool bounding boxes
run_server.py: Flask web server and API endpoints
/api/base_images: Get current base images/api/tools: Get available tools/api/process: Process selected images/api/download/<image_id>: Download processed image/api/refresh_images: Load new base imagespipeline_stages.py: Core image processing pipeline
ToolCompositor: Manages tool loading and compositingprocess_composite_stage(): Composite tool onto image + SD refinementinitialize_medgemma_pipeline(): Set up MedGemma for descriptionsrun_image_through_medgemma(): Generate image descriptionsSolution:
optimize_memory: true in GPU settingsuse_medgemma: falsenum_inference_steps to 1-4dtype: "float16" for lower memory usageSolution:
huggingface_cache_dir in confighuggingface-cli download google/medgemma-1.5-4b-it --cache-dir /your/cache/path
Solution:
nvidia-smidevice: "cpu" in configSolution:
tool_regions_dir structure matches expected layoutimages/ and masks/ subdirectoriesfrom pipeline_stages import ToolCompositor, load_config
# Load configuration
config = load_config('config.yaml')
# Initialize compositor
compositor = ToolCompositor(
no_tools_dir=config['data']['no_tools_images_dir'],
tool_regions_dir=config['data']['tool_regions_dir'],
output_dir=config['data']['output_dir']
)
# Load and composite a tool
tool_img, tool_mask = compositor.load_tool_sample('grasper', sample_idx=0)
base_img = Image.open('base_image.png').convert('RGB')
composite = compositor.paste_tool_excluding_white(base_img, tool_img, position=(10, 10))
Edit the prompt in process_composite_stage():
pipeline_args = {
"prompt": "your custom prompt here",
"guidance_scale": 4.5,
...
}
If you use this tool in your research, please cite:
@software{surgfast,
title={SurgFasT: Fast-tracking Surgical science with medGemma},
author={Danush Kumar Venkatesh},
year={2026},
}
For issues, questions, or contributions:
48 commits
Python
100.0%