{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **SIIM COVID-19 Convert DCM to JPG 640px**","metadata":{"papermill":{"duration":0.010083,"end_time":"2021-06-06T15:52:33.510209","exception":false,"start_time":"2021-06-06T15:52:33.500126","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"> #### Thanks:\n> - https://www.kaggle.com/xhlulu/siim-covid-19-convert-to-jpg-256px\n> - I used the above kernel directly, just changed the image size to (640x640)","metadata":{"papermill":{"duration":0.008715,"end_time":"2021-06-06T15:52:33.528159","exception":false,"start_time":"2021-06-06T15:52:33.519444","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Other Kernels (Training, Inferance ,EDA and Dataset) \n- [SIIM COVID-19 Detectron2 Training](https://www.kaggle.com/ammarnassanalhajali/siim-covid-19-detectron2-training)\n- [SIIM COVID-19 Detectron2 Inferance](https://www.kaggle.com/ammarnassanalhajali/siim-covid-19-detectron2-inferance)\n- [SIIM-FISABIO-RSNA COVID-19 Detection-EDA](https://www.kaggle.com/ammarnassanalhajali/siim-fisabio-rsna-covid-19-detection-eda)\n- [SIIM-COVID-19 Detection Training Labels (Dataset)](https://www.kaggle.com/ammarnassanalhajali/siimcovid19-detection-training-label)\n","metadata":{"papermill":{"duration":0.008695,"end_time":"2021-06-06T15:52:33.545996","exception":false,"start_time":"2021-06-06T15:52:33.537301","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!conda install gdcm -c conda-forge -y","metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2021-06-06T15:52:33.591103Z","iopub.status.busy":"2021-06-06T15:52:33.574326Z","iopub.status.idle":"2021-06-06T15:53:44.84968Z","shell.execute_reply":"2021-06-06T15:53:44.848948Z"},"papermill":{"duration":71.294818,"end_time":"2021-06-06T15:53:44.849875","exception":false,"start_time":"2021-06-06T15:52:33.555057","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2021-06-06T15:53:45.040092Z","iopub.status.busy":"2021-06-06T15:53:45.039406Z","iopub.status.idle":"2021-06-06T15:53:45.048359Z","shell.execute_reply":"2021-06-06T15:53:45.047537Z"},"papermill":{"duration":0.106504,"end_time":"2021-06-06T15:53:45.048531","exception":false,"start_time":"2021-06-06T15:53:44.942027","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data","metadata":{"execution":{"iopub.execute_input":"2021-06-06T15:53:45.242308Z","iopub.status.busy":"2021-06-06T15:53:45.241528Z","iopub.status.idle":"2021-06-06T15:53:45.432487Z","shell.execute_reply":"2021-06-06T15:53:45.433227Z"},"papermill":{"duration":0.291386,"end_time":"2021-06-06T15:53:45.433424","exception":false,"start_time":"2021-06-06T15:53:45.142038","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im","metadata":{"execution":{"iopub.execute_input":"2021-06-06T15:53:45.615504Z","iopub.status.busy":"2021-06-06T15:53:45.614817Z","iopub.status.idle":"2021-06-06T15:53:45.620654Z","shell.execute_reply":"2021-06-06T15:53:45.621292Z"},"papermill":{"duration":0.09881,"end_time":"2021-06-06T15:53:45.621482","exception":false,"start_time":"2021-06-06T15:53:45.522672","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')","metadata":{"execution":{"iopub.execute_input":"2021-06-06T15:53:45.810855Z","iopub.status.busy":"2021-06-06T15:53:45.810069Z","iopub.status.idle":"2021-06-06T15:53:45.869456Z","shell.execute_reply":"2021-06-06T15:53:45.868906Z"},"papermill":{"duration":0.154832,"end_time":"2021-06-06T15:53:45.869609","exception":false,"start_time":"2021-06-06T15:53:45.714777","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/siim-covid19-detection/train/ae3e63d94c13/288554eb6182/e00f9fe0cce5.dcm'\ndicom = pydicom.read_file(path)","metadata":{"execution":{"iopub.execute_input":"2021-06-06T15:53:46.053608Z","iopub.status.busy":"2021-06-06T15:53:46.052942Z","iopub.status.idle":"2021-06-06T15:53:46.407284Z","shell.execute_reply":"2021-06-06T15:53:46.406514Z"},"papermill":{"duration":0.448019,"end_time":"2021-06-06T15:53:46.407437","exception":false,"start_time":"2021-06-06T15:53:45.959418","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = []\ndim0 = []\ndim1 = []\nsplits = []\n\nfor split in ['test', 'train']:\n    save_dir = f'/kaggle/tmp/{split}/'\n\n    os.makedirs(save_dir, exist_ok=True)\n    \n    for dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n        for file in filenames:\n            # set keep_ratio=True to have original aspect ratio\n            xray = read_xray(os.path.join(dirname, file))\n            im = resize(xray, size=1024)  \n            im.save(os.path.join(save_dir, file.replace('dcm', 'jpg')))\n\n            image_id.append(file.replace('.dcm', ''))\n            dim0.append(xray.shape[0])\n            dim1.append(xray.shape[1])\n            splits.append(split)","metadata":{"execution":{"iopub.execute_input":"2021-06-06T15:53:46.594564Z","iopub.status.busy":"2021-06-06T15:53:46.59386Z","iopub.status.idle":"2021-06-06T16:43:44.434609Z","shell.execute_reply":"2021-06-06T16:43:44.433787Z"},"papermill":{"duration":2997.938512,"end_time":"2021-06-06T16:43:44.434794","exception":false,"start_time":"2021-06-06T15:53:46.496282","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n!tar -zcf train.tar.gz -C \"/kaggle/tmp/train/\" .\n!tar -zcf test.tar.gz -C \"/kaggle/tmp/test/\" .","metadata":{"execution":{"iopub.execute_input":"2021-06-06T16:43:44.625663Z","iopub.status.busy":"2021-06-06T16:43:44.619307Z","iopub.status.idle":"2021-06-06T16:43:56.325393Z","shell.execute_reply":"2021-06-06T16:43:56.32487Z"},"papermill":{"duration":11.80114,"end_time":"2021-06-06T16:43:56.325537","exception":false,"start_time":"2021-06-06T16:43:44.524397","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame.from_dict({'image_id': image_id, 'dim0': dim0, 'dim1': dim1, 'split': splits})\ndf.to_csv('meta.csv', index=False)","metadata":{"execution":{"iopub.execute_input":"2021-06-06T16:43:56.528787Z","iopub.status.busy":"2021-06-06T16:43:56.528056Z","iopub.status.idle":"2021-06-06T16:43:56.561587Z","shell.execute_reply":"2021-06-06T16:43:56.561017Z"},"papermill":{"duration":0.145741,"end_time":"2021-06-06T16:43:56.561752","exception":false,"start_time":"2021-06-06T16:43:56.416011","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# References\n1. https://www.kaggle.com/xhlulu/siim-covid19-resized-to-256px-jpg\n","metadata":{"papermill":{"duration":0.089667,"end_time":"2021-06-06T16:43:56.741561","exception":false,"start_time":"2021-06-06T16:43:56.651894","status":"completed"},"tags":[]}}]}