Deployment dicom images


I am trying to deploy an algorithm that inputs DICOM files following 02_production.ipynb from fastbook

However, I got stucked trying to upload the image when using:

btn_upload = widgets.FileUpload()

img = PILDicom.create([-1])

I got the following error:

OSError                                   Traceback (most recent call last)
<ipython-input-65-dea520106493> in <module>
----> 1 img = PILDicom.create([-1])

<ipython-input-64-95a902c2b184> in create(cls, fn, mode)
      4     def create(cls, fn:(Path,str), mode=None)->None:
      5         "Open a `DICOM file` from path `fn` and load it as a `PIL Image`"
----> 6         im = Image.fromarray(dcmread(fn).pixel_array)
      7         im.load()
      8         im = im._new(

~/DeepLearning/fastai2/fastai2/medical/ in dcmread(fn, force)
     30 def dcmread(fn:Path, force = False):
     31     "Open a `DICOM` file"
---> 32     return pydicom.dcmread(str(fn), force)
     34 # Cell

~/anaconda3/envs/fastai2/lib/python3.7/site-packages/pydicom/ in dcmread(fp, defer_size, stop_before_pixels, force, specific_tags)
    844         except Exception:
    845             logger.debug("Reading file '{0}'".format(fp))
--> 846         fp = open(fp, 'rb')
    848     if config.debugging:

OSError: [Errno 36] File name too long: 'b\'\\ [...]

I see that PILDicom.create only accepts Path to the file to import it correctly so I guess I have to add bytes to the PILDicom class. However, I am not sure how to do it. Any hints?

For anyone having the same issue I modified the PILDicom class as follows:

class PILDicom(PILBase):
    _open_args,_tensor_cls,_show_args = {},TensorDicom,TensorDicom._show_args
    def create(cls, fn:(Path,str,bytes), mode=None)->None:
        "Open a `DICOM file` from path `fn` or bytes `fn` and load it as a `PIL Image`"
        if isinstance(fn,bytes): im = Image.fromarray(pydicom.dcmread(pydicom.filebase.DicomBytesIO(fn)).pixel_array)
        if isinstance(fn,Path): im = Image.fromarray(dcmread(fn).pixel_array)
        im = im._new(
        return cls(im.convert(mode) if mode else im)

PILDicom._tensor_cls = TensorDicom