Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,74 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
from mpl_toolkits.mplot3d import Axes3D
|
6 |
+
from skimage import measure
|
7 |
+
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
|
8 |
+
from lungmask import LMInferer
|
9 |
+
import SimpleITK as sitk
|
10 |
+
import os
|
11 |
|
12 |
+
# --- Lung Segmentation Functions ---
|
|
|
13 |
|
14 |
+
def process_dcm_file(file_path, inferer):
|
15 |
+
"""Loads a DCM file, performs lung segmentation, and returns the mask."""
|
16 |
+
input_image = sitk.ReadImage(file_path)
|
17 |
+
segmentation = inferer.apply(input_image)
|
18 |
+
newseg = segmentation.reshape(512, 512) # Assuming 512x512 images
|
19 |
+
return newseg
|
20 |
+
|
21 |
+
def segment_lungs_from_dicom(dcm_folder):
|
22 |
+
"""Segments lungs from DICOM files in a folder and returns a 3D volume."""
|
23 |
+
if not os.path.exists(dcm_folder) or not os.path.isdir(dcm_folder):
|
24 |
+
raise ValueError("Invalid DICOM folder path.")
|
25 |
+
|
26 |
+
inferer = LMInferer()
|
27 |
+
segmentation_masks = []
|
28 |
+
for filename in os.listdir(dcm_folder):
|
29 |
+
if filename.endswith(".dcm"):
|
30 |
+
file_path = os.path.join(dcm_folder, filename)
|
31 |
+
mask = process_dcm_file(file_path, inferer)
|
32 |
+
segmentation_masks.append(mask)
|
33 |
+
volume = np.stack(segmentation_masks, axis=0)
|
34 |
+
return volume
|
35 |
+
|
36 |
+
# --- 3D Visualization Function ---
|
37 |
+
|
38 |
+
def plot_3d_lungs(lungs_volume, threshold=0.5):
|
39 |
+
"""Creates an interactive 3D plot of segmented lungs using Plotly (upright)."""
|
40 |
+
verts, faces, normals, values = measure.marching_cubes(lungs_volume.transpose(2, 1, 0), threshold)
|
41 |
+
|
42 |
+
# Apply rotation to make lungs upright
|
43 |
+
# Assuming you want to rotate 90 degrees counter-clockwise around the X-axis
|
44 |
+
rotation_angle_degrees = -90
|
45 |
+
rotation_angle_radians = np.radians(rotation_angle_degrees)
|
46 |
+
rotation_matrix = np.array([[1, 0, 0],
|
47 |
+
[0, np.cos(rotation_angle_radians), -np.sin(rotation_angle_radians)],
|
48 |
+
[0, np.sin(rotation_angle_radians), np.cos(rotation_angle_radians)]])
|
49 |
+
rotated_verts = np.dot(verts, rotation_matrix)
|
50 |
+
|
51 |
+
x, y, z = zip(*rotated_verts) # Use rotated vertices
|
52 |
+
i, j, k = zip(*faces)
|
53 |
+
|
54 |
+
mesh = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k, opacity=0.7, color='lightblue')
|
55 |
+
fig = go.Figure(data=[mesh])
|
56 |
+
fig.update_layout(scene_aspectmode='data') # Maintain aspect ratio
|
57 |
+
return fig
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
# --- Create Gradio Interface ---
|
62 |
+
|
63 |
+
inputs = gr.Textbox(label="DICOM Folder Path")
|
64 |
+
output = gr.Plot(label="3D Segmented Lungs")
|
65 |
+
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=lambda dcm_folder: plot_3d_lungs(segment_lungs_from_dicom(dcm_folder)),
|
68 |
+
inputs=inputs,
|
69 |
+
outputs=output,
|
70 |
+
title="3D Lung Segmentation Visualization",
|
71 |
+
description="Visualize segmented lungs from DICOM images.",
|
72 |
+
)
|
73 |
+
|
74 |
+
iface.launch()
|