Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,7 @@ from yolov5.models.experimental import attempt_load
|
|
| 18 |
from yolov5.utils.general import non_max_suppression
|
| 19 |
from yolov5.utils.augmentations import letterbox
|
| 20 |
|
|
|
|
| 21 |
# Example URLs for downloading images
|
| 22 |
file_urls = [
|
| 23 |
"https://www.dropbox.com/scl/fi/n3bs5xnl2kanqmwv483k3/1_jpg.rf.4a59a63d0a7339d280dd18ef3c2e675a.jpg?rlkey=4n9dnls1byb4wm54ycxzx3ovi&st=ue5xv8yx&dl=0",
|
|
@@ -39,6 +40,7 @@ def download_file(url, save_name):
|
|
| 39 |
# Download images
|
| 40 |
for i, url in enumerate(file_urls):
|
| 41 |
download_file(url, f"image_{i}.jpg")
|
|
|
|
| 42 |
|
| 43 |
# Load YOLOv5 model (placeholder)
|
| 44 |
model_path = "best.pt" # Path to your YOLOv5 model
|
|
@@ -105,7 +107,7 @@ def show_preds_image(filepath):
|
|
| 105 |
img0 = cv2.imread(filepath)
|
| 106 |
img_with_boxes = draw_bounding_boxes(img0, results)
|
| 107 |
return cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 108 |
-
|
| 109 |
# Define Gradio components
|
| 110 |
input_component = gr.components.Image(type="filepath", label="Input Image")
|
| 111 |
output_component = gr.components.Image(type="numpy", label="Output Image")
|
|
@@ -129,3 +131,82 @@ interface = gr.Interface(
|
|
| 129 |
)
|
| 130 |
|
| 131 |
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
from yolov5.utils.general import non_max_suppression
|
| 19 |
from yolov5.utils.augmentations import letterbox
|
| 20 |
|
| 21 |
+
'''
|
| 22 |
# Example URLs for downloading images
|
| 23 |
file_urls = [
|
| 24 |
"https://www.dropbox.com/scl/fi/n3bs5xnl2kanqmwv483k3/1_jpg.rf.4a59a63d0a7339d280dd18ef3c2e675a.jpg?rlkey=4n9dnls1byb4wm54ycxzx3ovi&st=ue5xv8yx&dl=0",
|
|
|
|
| 40 |
# Download images
|
| 41 |
for i, url in enumerate(file_urls):
|
| 42 |
download_file(url, f"image_{i}.jpg")
|
| 43 |
+
'''
|
| 44 |
|
| 45 |
# Load YOLOv5 model (placeholder)
|
| 46 |
model_path = "best.pt" # Path to your YOLOv5 model
|
|
|
|
| 107 |
img0 = cv2.imread(filepath)
|
| 108 |
img_with_boxes = draw_bounding_boxes(img0, results)
|
| 109 |
return cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 110 |
+
'''
|
| 111 |
# Define Gradio components
|
| 112 |
input_component = gr.components.Image(type="filepath", label="Input Image")
|
| 113 |
output_component = gr.components.Image(type="numpy", label="Output Image")
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
interface.launch()
|
| 134 |
+
'''
|
| 135 |
+
|
| 136 |
+
def read_and_preprocess_dicom(file_path: str):
|
| 137 |
+
"""
|
| 138 |
+
Function to read and preprocess DICOM files
|
| 139 |
+
:param file_path: Path to the DICOM file
|
| 140 |
+
:return: Image data (in PIL format) and metadata (in pandas DataFrame format)
|
| 141 |
+
"""
|
| 142 |
+
try:
|
| 143 |
+
# Read the DICOM file
|
| 144 |
+
dicom_data = pydicom.dcmread(file_path)
|
| 145 |
+
except InvalidDicomError:
|
| 146 |
+
raise gr.Error("The uploaded file is not a valid DICOM file.")
|
| 147 |
+
|
| 148 |
+
# Get the pixel data
|
| 149 |
+
try:
|
| 150 |
+
pixel_array = dicom_data.pixel_array
|
| 151 |
+
except AttributeError:
|
| 152 |
+
raise gr.Error("The uploaded DICOM file has no pixel data.")
|
| 153 |
+
|
| 154 |
+
# Normalize the pixel data to 8-bit and convert to a PIL image
|
| 155 |
+
if pixel_array.dtype != np.uint8:
|
| 156 |
+
pixel_array = ((pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array)) * 255).astype(
|
| 157 |
+
np.uint8)
|
| 158 |
+
image = Image.fromarray(pixel_array)
|
| 159 |
+
|
| 160 |
+
# Collect metadata in dictionary format and convert to DataFrame
|
| 161 |
+
metadata_dict = {elem.name: str(elem.value) for elem in dicom_data.iterall() if elem.name != 'Pixel Data'}
|
| 162 |
+
df_metadata = pl.DataFrame({
|
| 163 |
+
"Key": list(metadata_dict.keys()),
|
| 164 |
+
"Value": list(metadata_dict.values())
|
| 165 |
+
})
|
| 166 |
+
|
| 167 |
+
return image, df_metadata.to_pandas() # Convert to pandas DataFrame for Gradio compatibility
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def build_interface():
|
| 171 |
+
"""
|
| 172 |
+
Function to build the Gradio interface
|
| 173 |
+
"""
|
| 174 |
+
theme = gr.themes.Soft(
|
| 175 |
+
primary_hue=gr.themes.colors.emerald,
|
| 176 |
+
secondary_hue=gr.themes.colors.emerald
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
with gr.Blocks(title='DICOM Viewer', theme=theme) as demo:
|
| 180 |
+
gr.Markdown(
|
| 181 |
+
"""
|
| 182 |
+
# DICOM Viewer
|
| 183 |
+
This app reads a DICOM file and displays the image and metadata.
|
| 184 |
+
"""
|
| 185 |
+
)
|
| 186 |
+
with gr.Column():
|
| 187 |
+
file_path = gr.File(label="Input DICOM Data")
|
| 188 |
+
|
| 189 |
+
with gr.Row():
|
| 190 |
+
dicom_image = gr.Image(type="pil", label="DICOM Image")
|
| 191 |
+
dicom_meta = gr.Dataframe(headers=None, label="Metadata")
|
| 192 |
+
|
| 193 |
+
inputs = [file_path]
|
| 194 |
+
outputs = [dicom_image, dicom_meta]
|
| 195 |
+
file_path.upload(fn=read_and_preprocess_dicom, inputs=inputs, outputs=outputs)
|
| 196 |
+
|
| 197 |
+
clear_button = gr.ClearButton(components=inputs + outputs, )
|
| 198 |
+
|
| 199 |
+
example = gr.Examples(
|
| 200 |
+
['samples/81_80.dcm','samples/110_109.dcm','samples/189_188.dcm'],
|
| 201 |
+
inputs=inputs,
|
| 202 |
+
outputs=outputs,
|
| 203 |
+
fn=read_and_preprocess_dicom,
|
| 204 |
+
cache_examples=True
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
return demo
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
if __name__ == '__main__':
|
| 211 |
+
demo = build_interface()
|
| 212 |
+
demo.launch()
|