File size: 3,602 Bytes
83695bd
44d1038
 
 
 
83695bd
44d1038
83695bd
 
44d1038
83695bd
 
44d1038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
title: OncoSeg Inference API
emoji: 🏥
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
license: cc-by-nc-4.0
---

# OncoSeg Medical Image Segmentation API

GPU-accelerated segmentation for CT and MRI volumes using the OncoSeg/MedSAM3 model.

## Features

- **Text-prompted segmentation**: Describe what to find (e.g., "tumor", "lesion")
- **Multiple organ checkpoints**: Brain, liver, breast, lung, kidney, spine
- **NIfTI support**: Upload .nii or .nii.gz files
- **API-first design**: Programmatic access for integration with viewers

## API Endpoints

### `POST /api/segment_slice_api`

Segment a single slice from a volume.

**Request:**
```json
{
    "nifti_b64": "<base64-encoded NIfTI file>",
    "slice_idx": 77,
    "text_prompt": "tumor",
    "checkpoint": "brain"
}
```

**Response:**
```json
{
    "success": true,
    "mask_b64": "<base64-encoded mask>",
    "mask_shape": [240, 240],
    "contours": [[[y1, x1], [y2, x2], ...]],
    "slice_idx": 77,
    "inference_time_ms": 1234
}
```

### `POST /api/segment_volume_api`

Segment an entire volume and return contours for all slices with detections.

**Request:**
```json
{
    "nifti_b64": "<base64-encoded NIfTI file>",
    "text_prompt": "tumor",
    "checkpoint": "brain",
    "skip_empty": true,
    "min_area": 50
}
```

**Response:**
```json
{
    "success": true,
    "contours": {
        "32": [[[y, x], ...]],
        "33": [[[y, x], ...]],
        ...
    },
    "num_slices": 155,
    "slices_with_tumor": ["32", "33", ...],
    "inference_time_ms": 45000
}
```

## Available Checkpoints

| Checkpoint | Organ/Task | Best For |
|------------|------------|----------|
| `brain` | Glioblastoma (BraTS) | Brain MRI FLAIR |
| `liver` | Liver lesions | Abdominal CT |
| `breast` | Breast tumor (DCE-MRI) | Breast MRI |
| `lung` | Lung cancer (NSCLC) | Chest CT |
| `kidney` | Kidney tumor (KiTS) | Abdominal CT |
| `spine` | Spine structures | CT |

## Usage Example (Python)

```python
import requests
import base64

# Read NIfTI file
with open("brain_mri.nii.gz", "rb") as f:
    nifti_b64 = base64.b64encode(f.read()).decode()

# Call API
response = requests.post(
    "https://tp53-oncoseg-api.hf.space/api/segment_slice_api",
    json={
        "nifti_b64": nifti_b64,
        "slice_idx": 77,
        "text_prompt": "tumor",
        "checkpoint": "brain",
    },
    timeout=120,  # Allow time for cold start
)

result = response.json()

if result["success"]:
    # Decode mask
    import numpy as np
    mask_bytes = base64.b64decode(result["mask_b64"])
    mask = np.frombuffer(mask_bytes, dtype=np.uint8).reshape(result["mask_shape"])
    
    # Use contours for visualization
    contours = result["contours"]
    print(f"Found {len(contours)} contours in {result['inference_time_ms']}ms")
```

## Integration with OncoSeg Viewer

This Space is designed to work with the [OncoSeg Viewer](https://github.com/tp53/oncoseg) browser-based medical image viewer.

Set environment variable in the viewer:
```bash
export INFERENCE_MODE=hf
export HF_SPACE_URL=https://tp53-oncoseg-api.hf.space
```

## Performance

| Metric | Value |
|--------|-------|
| Cold start | 10-30s (model loading) |
| Warm inference | 1-3s per slice |
| Full volume (155 slices) | 3-5 minutes |

## License

CC BY-NC 4.0 - For research and non-commercial use.

## Citation

If you use OncoSeg in your research, please cite:
```bibtex
@software{oncoseg2025,
  title = {OncoSeg: Medical Image Segmentation with MedSAM3},
  year = {2025},
  url = {https://huggingface.co/spaces/tp53/oncoseg-api}
}
```