Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,12 +76,13 @@ def detect_objects(image_path):
|
|
| 76 |
def draw_bounding_boxes(img, results, dicom_meta):
|
| 77 |
dets = []
|
| 78 |
for (x1, y1, x2, y2), conf, cls in results:
|
| 79 |
-
|
|
|
|
| 80 |
xc = x1+(x2-x1)/2
|
| 81 |
yc = y1+(y2-y1)/2
|
| 82 |
conf = round(conf.detach().item(), 4)
|
| 83 |
#print("results in draw_bounding box:",[(x1, y1, x2, y2), conf, cls])
|
| 84 |
-
|
| 85 |
#dets.append([(x1, y1, x2, y2), conf, cls])
|
| 86 |
dets.append([(xc, yc, zc), conf, cls])
|
| 87 |
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
|
@@ -120,11 +121,7 @@ def read_and_preprocess_dicom(file_path: str):
|
|
| 120 |
|
| 121 |
image = image_pil.convert('RGB')
|
| 122 |
|
| 123 |
-
#print("In preprocess dicom:", image.size)
|
| 124 |
image = np.array(image)[:,:,::-1].copy()
|
| 125 |
-
|
| 126 |
-
# shape
|
| 127 |
-
#print("In preprocess dicom-image.shape2:",image.shape)
|
| 128 |
|
| 129 |
# Collect metadata in dictionary format and convert to DataFrame
|
| 130 |
metadata_dict = {elem.name: str(elem.value) for elem in dicom_data.iterall() if elem.name != 'Pixel Data'}
|
|
@@ -138,7 +135,6 @@ def read_and_preprocess_dicom(file_path: str):
|
|
| 138 |
|
| 139 |
# Define Gradio components
|
| 140 |
input_component = gr.File(label="Input DICOM Data")
|
| 141 |
-
#output_component = gr.components.Image(type="numpy", label="Output Image")
|
| 142 |
dicom_image = gr.Image(type="numpy", label="Output Image")
|
| 143 |
dicom_meta = gr.Dataframe(headers=None, label="Metadata")
|
| 144 |
dets_res = gr.Text(label="Detections")
|
|
@@ -156,52 +152,3 @@ interface = gr.Interface(
|
|
| 156 |
)
|
| 157 |
|
| 158 |
interface.launch()
|
| 159 |
-
|
| 160 |
-
'''
|
| 161 |
-
def build_interface():
|
| 162 |
-
"""
|
| 163 |
-
Function to build the Gradio interface
|
| 164 |
-
"""
|
| 165 |
-
theme = gr.themes.Soft(
|
| 166 |
-
primary_hue=gr.themes.colors.emerald,
|
| 167 |
-
secondary_hue=gr.themes.colors.emerald
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
with gr.Blocks(title='DICOM Viewer', theme=theme) as demo:
|
| 171 |
-
gr.Markdown(
|
| 172 |
-
"""
|
| 173 |
-
# DICOM Viewer
|
| 174 |
-
This app reads a DICOM file and displays the image and metadata.
|
| 175 |
-
"""
|
| 176 |
-
)
|
| 177 |
-
with gr.Column():
|
| 178 |
-
file_path = gr.File(label="Input DICOM Data")
|
| 179 |
-
|
| 180 |
-
with gr.Row():
|
| 181 |
-
dicom_image = gr.Image(type="numpy", label="Output Image")
|
| 182 |
-
#dicom_image = gr.Image(type="pil", label="DICOM Image")
|
| 183 |
-
dicom_meta = gr.Dataframe(headers=None, label="Metadata")
|
| 184 |
-
|
| 185 |
-
inputs = [file_path]
|
| 186 |
-
outputs = [dicom_image, dicom_meta]
|
| 187 |
-
file_path.upload(fn=read_and_preprocess_dicom, inputs=inputs, outputs=outputs)
|
| 188 |
-
|
| 189 |
-
clear_button = gr.ClearButton(components=inputs + outputs, )
|
| 190 |
-
|
| 191 |
-
example = gr.Examples(
|
| 192 |
-
['samples/81_80.dcm','samples/110_109.dcm','samples/189_188.dcm'],
|
| 193 |
-
inputs=inputs,
|
| 194 |
-
outputs=outputs,
|
| 195 |
-
#outputs=dicom_image,
|
| 196 |
-
#fn=read_and_preprocess_dicom,
|
| 197 |
-
fn=show_preds_image,
|
| 198 |
-
cache_examples=True
|
| 199 |
-
)
|
| 200 |
-
|
| 201 |
-
return demo
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
if __name__ == '__main__':
|
| 205 |
-
demo = build_interface()
|
| 206 |
-
demo.launch
|
| 207 |
-
'''
|
|
|
|
| 76 |
def draw_bounding_boxes(img, results, dicom_meta):
|
| 77 |
dets = []
|
| 78 |
for (x1, y1, x2, y2), conf, cls in results:
|
| 79 |
+
zc = dicom_meta.loc[dicom_meta.Key == 'Instance Number', 'Value'].iloc[0]
|
| 80 |
+
x1, y1, x2, y2, zc, cls = map(int, [x1, y1, x2, y2, zc, cls])
|
| 81 |
xc = x1+(x2-x1)/2
|
| 82 |
yc = y1+(y2-y1)/2
|
| 83 |
conf = round(conf.detach().item(), 4)
|
| 84 |
#print("results in draw_bounding box:",[(x1, y1, x2, y2), conf, cls])
|
| 85 |
+
|
| 86 |
#dets.append([(x1, y1, x2, y2), conf, cls])
|
| 87 |
dets.append([(xc, yc, zc), conf, cls])
|
| 88 |
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
|
|
|
| 121 |
|
| 122 |
image = image_pil.convert('RGB')
|
| 123 |
|
|
|
|
| 124 |
image = np.array(image)[:,:,::-1].copy()
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# Collect metadata in dictionary format and convert to DataFrame
|
| 127 |
metadata_dict = {elem.name: str(elem.value) for elem in dicom_data.iterall() if elem.name != 'Pixel Data'}
|
|
|
|
| 135 |
|
| 136 |
# Define Gradio components
|
| 137 |
input_component = gr.File(label="Input DICOM Data")
|
|
|
|
| 138 |
dicom_image = gr.Image(type="numpy", label="Output Image")
|
| 139 |
dicom_meta = gr.Dataframe(headers=None, label="Metadata")
|
| 140 |
dets_res = gr.Text(label="Detections")
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|