{"action":"created","issue":{"url":"https://api.github.com/repos/fastai/fastai/issues/773","repository_url":"https://api.github.com/repos/fastai/fastai","labels_url":"https://api.github.com/repos/fastai/fastai/issues/773/labels{/name}","comments_url":"https://api.github.com/repos/fastai/fastai/issues/773/comments","events_url":"https://api.github.com/repos/fastai/fastai/issues/773/events","html_url":"https://github.com/fastai/fastai/issues/773","id":360168229,"node_id":"MDU6SXNzdWUzNjAxNjgyMjk=","number":773,"title":"Make open_image support dicom","user":{"login":"dov","id":338235,"node_id":"MDQ6VXNlcjMzODIzNQ==","avatar_url":"https://avatars3.githubusercontent.com/u/338235?v=4","gravatar_id":"","url":"https://api.github.com/users/dov","html_url":"https://github.com/dov","followers_url":"https://api.github.com/users/dov/followers","following_url":"https://api.github.com/users/dov/following{/other_user}","gists_url":"https://api.github.com/users/dov/gists{/gist_id}","starred_url":"https://api.github.com/users/dov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dov/subscriptions","organizations_url":"https://api.github.com/users/dov/orgs","repos_url":"https://api.github.com/users/dov/repos","events_url":"https://api.github.com/users/dov/events{/privacy}","received_events_url":"https://api.github.com/users/dov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-09-14T06:06:04Z","updated_at":"2018-09-14T14:14:06Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Do you want to update functionality in fastai? If so the following patch I made make fastai support dicom images out of the box. This is needed e.g. for the kaggle pneumonia detection competition. Please let me know whether to prepare a pull request. Note that I haven't checked the RGB or non .dcm extension code paths yet.\r\n\r\n```\r\ndiff --git a/fastai/dataset.py b/fastai/dataset.py\r\nindex feb8eec..18c33dc 100644\r\n--- a/fastai/dataset.py\r\n+++ b/fastai/dataset.py\r\n@@ -1,7 +1,10 @@\r\n from PIL.ImageFile import ImageFile\r\n from .dataloader import DataLoader\r\n from .transforms import *\r\n-\r\n+try:\r\n+    import pydicom\r\n+except:\r\n+    pass\r\n def get_cv_idxs(n, cv_idx=0, val_pct=0.2, seed=42):\r\n     \"\"\" Get a list of index values for Validation set from a dataset\r\n@@ -235,6 +238,13 @@ class BaseDataset(Dataset):\r\n         \"\"\"True if the data set is used to train regression models.\"\"\"\r\n         return False\r\n+def isdicom(fn):\r\n+  if fn.endswith('.dcm'):\r\n+    return True\r\n+  with open(fn) as fh:\r\n+    fh.seek(80)\r\n+    return fh.read(4)=='DICM'\r\n+\r\n def open_image(fn):\r\n     \"\"\" Opens an image using OpenCV given the file path.\r\n@@ -258,6 +268,15 @@ def open_image(fn):\r\n                 req = urllib.urlopen(str(fn))\r\n                 image = np.asarray(bytearray(req.read()), dtype=\"uint8\")\r\n                 im = cv2.imdecode(image, flags).astype(np.float32)/255\r\n+            elif isdicom(fn):\r\n+                slice = pydicom.read_file(fn)\r\n+                if slice.PhotometricInterpretation.startswith('MONOCHROME'):\r\n+                    # Make a fake RGB image\r\n+                    im = np.stack([slice.pixel_array]*3,-1)\r\n+                elif slice.PhotometricInterpretation == 'RGB':\r\n+                    im = slice.pixel_array\r\n+                else:\r\n+                    raise OSError('Unsupported DICOM image with PhotometricInterpretation=={}'.format(slice.PhotometricInterpretation))\r\n             else:\r\n                 im = cv2.imread(str(fn), flags).astype(np.float32)/255\r\n             if im is None: raise OSError(f'File not recognized by opencv: {fn}')\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/fastai/fastai/issues/comments/421371741","html_url":"https://github.com/fastai/fastai/issues/773#issuecomment-421371741","issue_url":"https://api.github.com/repos/fastai/fastai/issues/773","id":421371741,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMTM3MTc0MQ==","user":{"login":"sgugger","id":35901082,"node_id":"MDQ6VXNlcjM1OTAxMDgy","avatar_url":"https://avatars3.githubusercontent.com/u/35901082?v=4","gravatar_id":"","url":"https://api.github.com/users/sgugger","html_url":"https://github.com/sgugger","followers_url":"https://api.github.com/users/sgugger/followers","following_url":"https://api.github.com/users/sgugger/following{/other_user}","gists_url":"https://api.github.com/users/sgugger/gists{/gist_id}","starred_url":"https://api.github.com/users/sgugger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgugger/subscriptions","organizations_url":"https://api.github.com/users/sgugger/orgs","repos_url":"https://api.github.com/users/sgugger/repos","events_url":"https://api.github.com/users/sgugger/events{/privacy}","received_events_url":"https://api.github.com/users/sgugger/received_events","type":"User","site_admin":false},"created_at":"2018-09-14T14:14:06Z","updated_at":"2018-09-14T14:14:06Z","author_association":"COLLABORATOR","body":"You can definitely make a PR about it."}}
{"actor":{"display_login":"sgugger"}}