Prgckwb commited on
Commit
53e7528
1 Parent(s): 645c144

add error handling

Browse files
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -3,6 +3,7 @@ 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):
@@ -11,11 +12,17 @@ def read_and_preprocess_dicom(file_path: str):
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:
@@ -68,3 +75,4 @@ def build_interface():
68
  if __name__ == '__main__':
69
  demo = build_interface()
70
  demo.launch()
 
 
3
  import polars as pl
4
  import pydicom
5
  from PIL import Image
6
+ from pydicom.errors import InvalidDicomError
7
 
8
 
9
  def read_and_preprocess_dicom(file_path: str):
 
12
  :param file_path: Path to the DICOM file
13
  :return: Image data (in PIL format) and metadata (in pandas DataFrame format)
14
  """
15
+ try:
16
+ # Read the DICOM file
17
+ dicom_data = pydicom.dcmread(file_path)
18
+ except InvalidDicomError:
19
+ raise gr.Error("The uploaded file is not a valid DICOM file.")
20
 
21
  # Get the pixel data
22
+ try:
23
+ pixel_array = dicom_data.pixel_array
24
+ except AttributeError:
25
+ raise gr.Error("The uploaded DICOM file has no pixel data.")
26
 
27
  # Normalize the pixel data to 8-bit and convert to a PIL image
28
  if pixel_array.dtype != np.uint8:
 
75
  if __name__ == '__main__':
76
  demo = build_interface()
77
  demo.launch()
78
+