Spaces:
Running
Running
File size: 3,095 Bytes
54b4471 55223b8 9840e47 55223b8 54b4471 4a657e9 54b4471 55223b8 54b4471 55223b8 54b4471 4a657e9 54b4471 4a657e9 54b4471 0a78ec2 55223b8 3f98068 4a657e9 a6be0a5 54b4471 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import gradio as gr
import pandas as pd
import numpy as np
import pydicom
import os
from skimage import transform
import torch
from segment_anything import sam_model_registry
import matplotlib.pyplot as plt
from PIL import Image
import io
# Function to load bounding boxes from CSV
def load_bounding_boxes(csv_file):
# Assuming CSV file has columns: 'filename', 'x_min', 'y_min', 'x_max', 'y_max'
df = pd.read_csv(csv_file)
return df
def load_image(file_path):
if file_path.endswith(".dcm"):
ds = pydicom.dcmread(file_path)
img = ds.pixel_array
else:
img = np.array(Image.open(file_path).convert('L')) # Convert to grayscale
H, W = img.shape
return img, H, W
# MedSAM inference function
def medsam_inference(medsam_model, img, box, H, W, target_size):
# Resize image and box to target size
img_resized = transform.resize(img, (target_size, target_size), anti_aliasing=True)
box_resized = np.array(box) * (target_size / np.array([W, H, W, H]))
# Convert image to PyTorch tensor
img_tensor = torch.from_numpy(img_resized).float().unsqueeze(0).unsqueeze(0).to(device) # Add channel and batch dimension
# Model expects box in format (x0, y0, x1, y1)
box_tensor = torch.tensor(box_resized, dtype=torch.float32).unsqueeze(0).to(device) # Add batch dimension
# MedSAM inference
img_embed = medsam_model.image_encoder(img_tensor)
mask = medsam_model.predict(img_embed, box_tensor)
# Post-process mask: resize back to original size
mask_resized = transform.resize(mask[0].cpu().numpy(), (H, W))
return mask_resized
# Function for visualizing images with masks
def visualize(image, mask, box):
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(image, cmap='gray')
ax[0].add_patch(plt.Rectangle((box[0], box[1]), box[2] - box[0], box[3] - box[1], edgecolor="red", facecolor="none"))
ax[1].imshow(image, cmap='gray')
ax[1].imshow(mask, alpha=0.5, cmap="jet")
plt.tight_layout()
buf = io.BytesIO()
plt.savefig(buf, format='png')
plt.close(fig)
buf.seek(0)
return buf
# Main function for Gradio app
def process_images(file, x_min, y_min, x_max, y_max):
image, H, W = load_image(file)
# Initialize MedSAM model
device = 'cuda' if torch.cuda.is_available() else 'cpu'
medsam_model = sam_model_registry['vit_b'](checkpoint="medsam_vit_b.pth") # Ensure the correct path
medsam_model = medsam_model.to(device)
medsam_model.eval()
box = [x_min, y_min, x_max, y_max]
mask = medsam_inference(medsam_model, image, box, H, W, H) # Assuming target size is the same as the image height
visualization = visualize(image, mask, box)
return visualization.getvalue() # Returning the byte stream
# Set up Gradio interface
iface = gr.Interface(
fn=process_images,
inputs=[
gr.File(label="MRI Slice (DICOM, PNG, etc.)"),
gr.Number(label="X min"),
gr.Number(label="Y min"),
gr.Number(label="X max"),
gr.Number(label="Y max")
],
outputs="plot"
)
iface.launch()
|