anicolson commited on
Commit
27c9559
·
verified ·
1 Parent(s): ce545f0

Update processing_cxrmate2.py

Browse files
Files changed (1) hide show
  1. processing_cxrmate2.py +15 -3
processing_cxrmate2.py CHANGED
@@ -6,6 +6,7 @@ from typing import Dict, List, Union
6
 
7
  import cv2
8
  import numpy as np
 
9
  import requests
10
  import torch
11
  import torch.nn.functional as F
@@ -298,18 +299,29 @@ class CXRMate2Processor(transformers.ProcessorMixin):
298
  for j in range(max_images):
299
  if j < len(images[i]):
300
 
 
 
301
  if isinstance(images[i][j], bytes):
302
  image = Image.open(io.BytesIO(images[i][j]))
303
 
304
  elif isinstance(images[i][j], str):
305
- response = requests.get(images[i][j], stream=True)
306
- image = Image.open(BytesIO(response.content))
 
 
 
 
 
 
307
 
308
  elif isinstance(images[i][j], Image.Image):
309
  image = images[i][j]
310
 
311
  if self.mimic_cxr_normalisation:
312
- image_np = np.array(image.convert('L'), dtype=np.float32)
 
 
 
313
  assert image_np.ndim == 2
314
  min_val = image_np.min()
315
  denom = image_np.max() - min_val
 
6
 
7
  import cv2
8
  import numpy as np
9
+ import pydicom
10
  import requests
11
  import torch
12
  import torch.nn.functional as F
 
299
  for j in range(max_images):
300
  if j < len(images[i]):
301
 
302
+ image_np = None
303
+
304
  if isinstance(images[i][j], bytes):
305
  image = Image.open(io.BytesIO(images[i][j]))
306
 
307
  elif isinstance(images[i][j], str):
308
+ if images[i][j].endswith('.dcm'):
309
+ assert self.mimic_cxr_normalisation, 'MIMIC-CXR normalisation must be True when using DICOM images.'
310
+ ds = pydicom.dcmread(images[i][j])
311
+ image_np = ds.pixel_array.astype(float)
312
+
313
+ else:
314
+ response = requests.get(images[i][j], stream=True)
315
+ image = Image.open(BytesIO(response.content))
316
 
317
  elif isinstance(images[i][j], Image.Image):
318
  image = images[i][j]
319
 
320
  if self.mimic_cxr_normalisation:
321
+
322
+ # MIMIC-CXR normalisation:
323
+ if image_np is None:
324
+ image_np = np.array(image.convert('L'), dtype=np.float32)
325
  assert image_np.ndim == 2
326
  min_val = image_np.min()
327
  denom = image_np.max() - min_val