Neemah commited on
Commit
127d69e
·
verified ·
1 Parent(s): d6f8a22

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +24 -22
utils.py CHANGED
@@ -2,6 +2,7 @@ import pydicom
2
  import numpy as np
3
  from PIL import Image
4
  import time
 
5
  from datetime import datetime
6
  from reportlab.pdfgen import canvas
7
  from reportlab.lib.pagesizes import letter
@@ -20,8 +21,9 @@ def convert_dicom_zip_to_nifti(zip_file):
20
  """
21
 
22
  # Step 1: Setup temp directories
23
- tmp_dir = f"/tmp/dicom_{int(time.time())}"
24
- nifti_dir = f"/tmp/nifti_{int(time.time())}"
 
25
  os.makedirs(tmp_dir, exist_ok=True)
26
  os.makedirs(nifti_dir, exist_ok=True)
27
 
@@ -81,33 +83,33 @@ def convert_dicom_zip_to_nifti(zip_file):
81
 
82
  return sequence_images
83
 
84
- def load_dicoms(filepaths):
85
- """
86
- Accepts a list of DICOM file objects.
87
- Returns a list of PIL Images, one per sequence.
88
- """
89
 
90
- images=[]
91
- for file in filepaths:
92
- dicom = pydicom.dcmread(file.name)
93
 
94
- # Extract the pixel array
95
- pixel_array = dicom.pixel_array.astype(float)
96
 
97
- # Normalize to 0-255 range
98
- pixel_min = pixel_array.min()
99
- pixel_max = pixel_array.max()
100
 
101
- if pixel_max - pixel_min == 0:
102
- continue #to handle sequences not added
103
 
104
- normalized = (pixel_array - pixel_min) / (pixel_max - pixel_min) * 255
105
 
106
- # Convert to uint8 RGB image
107
- image = Image.fromarray(normalized.astype(np.uint8)).convert("RGB")
108
- images.append(image)
109
 
110
- return images
111
 
112
  def generate_pdf(report_text):
113
  """
 
2
  import numpy as np
3
  from PIL import Image
4
  import time
5
+ import uuid
6
  from datetime import datetime
7
  from reportlab.pdfgen import canvas
8
  from reportlab.lib.pagesizes import letter
 
21
  """
22
 
23
  # Step 1: Setup temp directories
24
+ unique_id = uuid.uuid4().hex
25
+ tmp_dir = f"/tmp/dicom_{unique_id}"
26
+ nifti_dir = f"/tmp/nifti_{unique_id}"
27
  os.makedirs(tmp_dir, exist_ok=True)
28
  os.makedirs(nifti_dir, exist_ok=True)
29
 
 
83
 
84
  return sequence_images
85
 
86
+ # def load_dicoms(filepaths):
87
+ # """
88
+ # Accepts a list of DICOM file objects.
89
+ # Returns a list of PIL Images, one per sequence.
90
+ # """
91
 
92
+ # images=[]
93
+ # for file in filepaths:
94
+ # dicom = pydicom.dcmread(file.name)
95
 
96
+ # # Extract the pixel array
97
+ # pixel_array = dicom.pixel_array.astype(float)
98
 
99
+ # # Normalize to 0-255 range
100
+ # pixel_min = pixel_array.min()
101
+ # pixel_max = pixel_array.max()
102
 
103
+ # if pixel_max - pixel_min == 0:
104
+ # continue #to handle sequences not added
105
 
106
+ # normalized = (pixel_array - pixel_min) / (pixel_max - pixel_min) * 255
107
 
108
+ # # Convert to uint8 RGB image
109
+ # image = Image.fromarray(normalized.astype(np.uint8)).convert("RGB")
110
+ # images.append(image)
111
 
112
+ # return images
113
 
114
  def generate_pdf(report_text):
115
  """