Spaces:
Running
Running
titi
commited on
Commit
·
781d212
1
Parent(s):
5eb4936
new version of app supporting url parameter and example file
Browse files- README.md +31 -13
- app.py +69 -1
- core/utils.py +17 -0
- requirements.txt +1 -3
README.md
CHANGED
@@ -1,21 +1,33 @@
|
|
1 |
-
---
|
2 |
-
title: Lungs Segmentation Web App
|
3 |
-
emoji: 🖥️
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: green
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.23.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
# 🖥️ Lungs segmentation web application
|
13 |
A web-based application for automated lung segmentation using deep learning, powered by **Gradio** and **PyTorch**. This tool allows users to upload lung images and obtain segmented outputs efficiently.
|
14 |
|
15 |
<p align="center">
|
16 |
-
<img src="
|
17 |
</p>
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
## Installation
|
20 |
We recommend performing the installation in a clean Python environment.
|
21 |
|
@@ -26,6 +38,8 @@ After that please run the following command:
|
|
26 |
pip install -r requirements.txt
|
27 |
```
|
28 |
|
|
|
|
|
29 |
## Usage
|
30 |
Run:
|
31 |
```sh
|
@@ -33,5 +47,9 @@ python app.py
|
|
33 |
```
|
34 |
And go to http://localhost:7860/.
|
35 |
|
|
|
|
|
36 |
## About Lungs Segmentation
|
37 |
-
If you are interesten in the package used for segmentation please check the following [GitHub repository](https://github.com/qchapp/lungs-segmentation)!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# 🖥️ Lungs segmentation web application
|
2 |
A web-based application for automated lung segmentation using deep learning, powered by **Gradio** and **PyTorch**. This tool allows users to upload lung images and obtain segmented outputs efficiently.
|
3 |
|
4 |
<p align="center">
|
5 |
+
<img src="images/app.png" height="700">
|
6 |
</p>
|
7 |
|
8 |
+
---
|
9 |
+
|
10 |
+
## Try the app
|
11 |
+
The application is running on [Hugging Face](https://huggingface.co/), try it using this [link](https://huggingface.co/spaces/qchapp/3d-lungs-segmentation)!
|
12 |
+
|
13 |
+
#### Example File
|
14 |
+
If you don't have your own `.tif` image, the app includes a built-in example file that can be used directly from the UI by clicking **"Try an example!"**.
|
15 |
+
|
16 |
+
#### Load from URL (file_url parameter)
|
17 |
+
You can also provide a `.tif` file hosted online using a URL parameter.
|
18 |
+
|
19 |
+
To do so, simply append `?file_url=...` to your app's URL.
|
20 |
+
|
21 |
+
##### Example (local):
|
22 |
+
`http://localhost:7860/?file_url=https://zenodo.org/record/8099852/files/lungs_ct.tif`
|
23 |
+
|
24 |
+
##### Example (hosted on Hugging Face):
|
25 |
+
`https://huggingface.co/spaces/qchapp/3d-lungs-segmentation/?file_url=https://zenodo.org/record/8099852/files/lungs_ct.tif`
|
26 |
+
|
27 |
+
The application will automatically download the file and load it into the viewer.
|
28 |
+
|
29 |
+
---
|
30 |
+
|
31 |
## Installation
|
32 |
We recommend performing the installation in a clean Python environment.
|
33 |
|
|
|
38 |
pip install -r requirements.txt
|
39 |
```
|
40 |
|
41 |
+
---
|
42 |
+
|
43 |
## Usage
|
44 |
Run:
|
45 |
```sh
|
|
|
47 |
```
|
48 |
And go to http://localhost:7860/.
|
49 |
|
50 |
+
---
|
51 |
+
|
52 |
## About Lungs Segmentation
|
53 |
+
If you are interesten in the package used for segmentation please check the following [GitHub repository](https://github.com/qchapp/lungs-segmentation)!
|
54 |
+
|
55 |
+
---
|
app.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
from core.utils import *
|
3 |
|
|
|
|
|
|
|
4 |
def get_axis_max(volume, axis):
|
5 |
"""Get the maximum index of each axis."""
|
6 |
if volume is None:
|
@@ -31,6 +34,14 @@ with gr.Blocks() as demo:
|
|
31 |
|
32 |
file_input = gr.File(file_types=[".tif", ".tiff"], label="Upload your 3D TIF or TIFF file")
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# ---- RAW SLICES VIEWER ----
|
35 |
with gr.Group(visible=False) as group_input:
|
36 |
gr.Markdown("### Raw Volume Slices")
|
@@ -59,7 +70,7 @@ with gr.Blocks() as demo:
|
|
59 |
|
60 |
reset_btn = gr.Button("Reset")
|
61 |
|
62 |
-
gr.Markdown("#### 📝 This work is based on the Bachelor
|
63 |
|
64 |
# ---- CALLBACKS ----
|
65 |
|
@@ -148,5 +159,62 @@ with gr.Blocks() as demo:
|
|
148 |
]
|
149 |
)
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
if __name__ == "__main__":
|
152 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from core.utils import *
|
3 |
|
4 |
+
import urllib.request
|
5 |
+
import tempfile
|
6 |
+
|
7 |
def get_axis_max(volume, axis):
|
8 |
"""Get the maximum index of each axis."""
|
9 |
if volume is None:
|
|
|
34 |
|
35 |
file_input = gr.File(file_types=[".tif", ".tiff"], label="Upload your 3D TIF or TIFF file")
|
36 |
|
37 |
+
# ---- Example loader ----
|
38 |
+
gr.Examples(
|
39 |
+
examples=[[example_file_path]],
|
40 |
+
inputs=[file_input],
|
41 |
+
label="Try an example!",
|
42 |
+
examples_per_page=1
|
43 |
+
)
|
44 |
+
|
45 |
# ---- RAW SLICES VIEWER ----
|
46 |
with gr.Group(visible=False) as group_input:
|
47 |
gr.Markdown("### Raw Volume Slices")
|
|
|
70 |
|
71 |
reset_btn = gr.Button("Reset")
|
72 |
|
73 |
+
gr.Markdown("#### 📝 This work is based on the Bachelor Project of Quentin Chappuis 2024; for more information, consult the [repository](https://github.com/qchapp/lungs-segmentation)!")
|
74 |
|
75 |
# ---- CALLBACKS ----
|
76 |
|
|
|
159 |
]
|
160 |
)
|
161 |
|
162 |
+
# ---- HANDLE QUERY PARAMETERS ----
|
163 |
+
@demo.load(
|
164 |
+
outputs=[
|
165 |
+
file_input,
|
166 |
+
volume_state,
|
167 |
+
group_input,
|
168 |
+
segment_btn,
|
169 |
+
z_slider, y_slider, x_slider,
|
170 |
+
z_img, y_img, x_img
|
171 |
+
]
|
172 |
+
)
|
173 |
+
def load_from_query(request: gr.Request):
|
174 |
+
params = request.query_params
|
175 |
+
|
176 |
+
if "file_url" in params:
|
177 |
+
try:
|
178 |
+
# A) Download the file from the URL to a temporary path
|
179 |
+
url = params["file_url"]
|
180 |
+
tmp_path = tempfile.mktemp(suffix=".tif")
|
181 |
+
urllib.request.urlretrieve(url, tmp_path)
|
182 |
+
|
183 |
+
# B) Open the file as a binary object
|
184 |
+
with open(tmp_path, "rb") as f:
|
185 |
+
volume = load_volume(f)
|
186 |
+
|
187 |
+
# C) Return values for all components
|
188 |
+
return [
|
189 |
+
gr.update(value=tmp_path),
|
190 |
+
volume,
|
191 |
+
gr.update(visible=True),
|
192 |
+
gr.update(visible=True),
|
193 |
+
gr.update(maximum=get_axis_max(volume, "Z")),
|
194 |
+
gr.update(maximum=get_axis_max(volume, "Y")),
|
195 |
+
gr.update(maximum=get_axis_max(volume, "X")),
|
196 |
+
browse_axis("Z", 0, volume),
|
197 |
+
browse_axis("Y", 0, volume),
|
198 |
+
browse_axis("X", 0, volume)
|
199 |
+
]
|
200 |
+
|
201 |
+
except Exception as e:
|
202 |
+
print(f"[Error loading file_url] {e}")
|
203 |
+
|
204 |
+
# Fallback if no file_url or failure
|
205 |
+
return [
|
206 |
+
None,
|
207 |
+
None,
|
208 |
+
gr.update(visible=False),
|
209 |
+
gr.update(visible=False),
|
210 |
+
gr.update(maximum=0),
|
211 |
+
gr.update(maximum=0),
|
212 |
+
gr.update(maximum=0),
|
213 |
+
None,
|
214 |
+
None,
|
215 |
+
None
|
216 |
+
]
|
217 |
+
|
218 |
+
|
219 |
if __name__ == "__main__":
|
220 |
demo.launch()
|
core/utils.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
import numpy as np
|
2 |
import tifffile
|
|
|
|
|
|
|
3 |
from PIL import Image
|
4 |
from unet_lungs_segmentation import LungsPredict
|
5 |
|
@@ -68,3 +71,17 @@ def browse_overlay_axis(axis, idx, volume, seg):
|
|
68 |
alpha = 0.3
|
69 |
blended = (1 - alpha) * raw_rgb + alpha * mask_rgb
|
70 |
return Image.fromarray(blended.astype(np.uint8))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
import tifffile
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
import urllib.request
|
6 |
from PIL import Image
|
7 |
from unet_lungs_segmentation import LungsPredict
|
8 |
|
|
|
71 |
alpha = 0.3
|
72 |
blended = (1 - alpha) * raw_rgb + alpha * mask_rgb
|
73 |
return Image.fromarray(blended.astype(np.uint8))
|
74 |
+
|
75 |
+
# Example file
|
76 |
+
def get_example_file():
|
77 |
+
url = "https://zenodo.org/record/8099852/files/lungs_ct.tif?download=1"
|
78 |
+
tmp_dir = tempfile.gettempdir()
|
79 |
+
tmp_path = os.path.join(tmp_dir, "example_lungs.tif")
|
80 |
+
|
81 |
+
# Only download if it doesn't already exist
|
82 |
+
if not os.path.exists(tmp_path):
|
83 |
+
urllib.request.urlretrieve(url, tmp_path)
|
84 |
+
|
85 |
+
return tmp_path
|
86 |
+
|
87 |
+
example_file_path = get_example_file()
|
requirements.txt
CHANGED
@@ -1,4 +1,2 @@
|
|
1 |
unet_lungs_segmentation
|
2 |
-
gradio==
|
3 |
-
torch==2.6.0
|
4 |
-
torchvision==0.21.0
|
|
|
1 |
unet_lungs_segmentation
|
2 |
+
gradio==5.25.1
|
|
|
|