Prgckwb commited on
Commit
645c144
1 Parent(s): ad211ff
Files changed (2) hide show
  1. app.py +70 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import polars as pl
4
+ import pydicom
5
+ from PIL import Image
6
+
7
+
8
+ def read_and_preprocess_dicom(file_path: str):
9
+ """
10
+ Function to read and preprocess DICOM files
11
+ :param file_path: Path to the DICOM file
12
+ :return: Image data (in PIL format) and metadata (in pandas DataFrame format)
13
+ """
14
+ # Read the DICOM file
15
+ dicom_data = pydicom.dcmread(file_path)
16
+
17
+ # Get the pixel data
18
+ pixel_array = dicom_data.pixel_array
19
+
20
+ # Normalize the pixel data to 8-bit and convert to a PIL image
21
+ if pixel_array.dtype != np.uint8:
22
+ pixel_array = ((pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array)) * 255).astype(
23
+ np.uint8)
24
+ image = Image.fromarray(pixel_array)
25
+
26
+ # Collect metadata in dictionary format and convert to DataFrame
27
+ metadata_dict = {elem.name: str(elem.value) for elem in dicom_data.iterall() if elem.name != 'Pixel Data'}
28
+ df_metadata = pl.DataFrame({
29
+ "Key": list(metadata_dict.keys()),
30
+ "Value": list(metadata_dict.values())
31
+ })
32
+
33
+ return image, df_metadata.to_pandas() # Convert to pandas DataFrame for Gradio compatibility
34
+
35
+
36
+ def build_interface():
37
+ """
38
+ Function to build the Gradio interface
39
+ """
40
+ theme = gr.themes.Soft(
41
+ primary_hue=gr.themes.colors.emerald,
42
+ secondary_hue=gr.themes.colors.emerald
43
+ )
44
+
45
+ with gr.Blocks(title='DICOM Viewer', theme=theme) as demo:
46
+ gr.Markdown(
47
+ """
48
+ # DICOM Viewer
49
+ This app reads a DICOM file and displays the image and metadata.
50
+ """
51
+ )
52
+ with gr.Column():
53
+ file_path = gr.File(label="Input DICOM Data")
54
+
55
+ with gr.Row():
56
+ dicom_image = gr.Image(type="pil", label="DICOM Image")
57
+ dicom_meta = gr.Dataframe(headers=None, label="Metadata")
58
+
59
+ inputs = [file_path]
60
+ outputs = [dicom_image, dicom_meta]
61
+ file_path.upload(fn=read_and_preprocess_dicom, inputs=inputs, outputs=outputs)
62
+
63
+ clear_button = gr.ClearButton(components=inputs + outputs, )
64
+
65
+ return demo
66
+
67
+
68
+ if __name__ == '__main__':
69
+ demo = build_interface()
70
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ pydicom
2
+ matplotlib
3
+ numpy
4
+ polars
5
+ pandas
6
+ Pillow