{"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":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nimport zipfile","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-01T15:51:30.618965Z","iopub.execute_input":"2021-08-01T15:51:30.619798Z","iopub.status.idle":"2021-08-01T15:51:30.630601Z","shell.execute_reply.started":"2021-08-01T15:51:30.619662Z","shell.execute_reply":"2021-08-01T15:51:30.629431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !wget 'https://anaconda.org/conda-forge/libjpeg-turbo/2.1.0/download/linux-64/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -q\n# !wget 'https://anaconda.org/conda-forge/libgcc-ng/9.3.0/download/linux-64/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -q\n# !wget 'https://anaconda.org/conda-forge/gdcm/2.8.9/download/linux-64/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -q\n# !wget 'https://anaconda.org/conda-forge/conda/4.10.1/download/linux-64/conda-4.10.1-py37h89c1867_0.tar.bz2' -q\n# !wget 'https://anaconda.org/conda-forge/certifi/2020.12.5/download/linux-64/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -q\n# !wget 'https://anaconda.org/conda-forge/openssl/1.1.1k/download/linux-64/openssl-1.1.1k-h7f98852_0.tar.bz2' -q","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:51:30.632036Z","iopub.execute_input":"2021-08-01T15:51:30.632491Z","iopub.status.idle":"2021-08-01T15:51:30.646085Z","shell.execute_reply.started":"2021-08-01T15:51:30.632445Z","shell.execute_reply":"2021-08-01T15:51:30.645275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !conda install 'libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n# !conda install 'libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n# !conda install 'gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n# !conda install 'conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n# !conda install 'certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n# !conda install 'openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y\n# import gdcm\n!conda install gdcm -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:51:30.647660Z","iopub.execute_input":"2021-08-01T15:51:30.648250Z","iopub.status.idle":"2021-08-01T15:52:35.373814Z","shell.execute_reply.started":"2021-08-01T15:51:30.648203Z","shell.execute_reply":"2021-08-01T15:52:35.372644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport cv2\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.status.busy":"2021-08-01T15:52:35.376623Z","iopub.execute_input":"2021-08-01T15:52:35.376928Z","iopub.status.idle":"2021-08-01T15:52:35.876737Z","shell.execute_reply.started":"2021-08-01T15:52:35.376894Z","shell.execute_reply":"2021-08-01T15:52:35.875560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_path_out = r'train_png'\nos.makedirs(dir_path_out)","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:52:35.877947Z","iopub.execute_input":"2021-08-01T15:52:35.878229Z","iopub.status.idle":"2021-08-01T15:52:35.883088Z","shell.execute_reply.started":"2021-08-01T15:52:35.878200Z","shell.execute_reply":"2021-08-01T15:52:35.881880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:52:35.884913Z","iopub.execute_input":"2021-08-01T15:52:35.885448Z","iopub.status.idle":"2021-08-01T15:52:35.894406Z","shell.execute_reply.started":"2021-08-01T15:52:35.885296Z","shell.execute_reply":"2021-08-01T15:52:35.893345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_imgs = []\ndir_path = r'../input/siim-covid19-detection/train'\nfor dirs, subdirs, filenames in tqdm.tqdm(os.walk(dir_path)):\n    for i_file in filenames:\n        path_full = os.path.join(dirs, i_file)\n        list_imgs.append(path_full)","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:52:35.895707Z","iopub.execute_input":"2021-08-01T15:52:35.896128Z","iopub.status.idle":"2021-08-01T15:52:59.682426Z","shell.execute_reply.started":"2021-08-01T15:52:35.896085Z","shell.execute_reply":"2021-08-01T15:52:59.681145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(list_imgs)\nzf = zipfile.ZipFile(os.path.join(dir_path_out, 'train_3.zip'), mode='w')","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:52:59.685031Z","iopub.execute_input":"2021-08-01T15:52:59.685487Z","iopub.status.idle":"2021-08-01T15:52:59.691548Z","shell.execute_reply.started":"2021-08-01T15:52:59.685440Z","shell.execute_reply":"2021-08-01T15:52:59.690353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for dirs, subdirs, filenames in tqdm.tqdm(os.walk(dir_path)):\nfor idx, img_path in tqdm.tqdm(enumerate(list_imgs), total=len(list_imgs)):\n    \n    if idx < 5000:\n        continue\n        \n#     if idx > 5000:\n#         break\n    \n    path_out = os.path.join(dir_path_out, os.path.relpath(img_path, dir_path))\n    path_out = os.path.splitext(path_out)[0] + '.png'\n    if not os.path.exists(path_out):\n        if not os.path.exists(os.path.split(path_out)[0]):\n            os.makedirs(os.path.split(path_out)[0])\n    img = read_xray(img_path)\n    if img is not None:\n        cv2.imwrite(path_out, img)\n        zf.write(path_out)\n        os.remove(path_out)","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:52:59.692886Z","iopub.execute_input":"2021-08-01T15:52:59.693302Z","iopub.status.idle":"2021-08-01T16:25:17.495724Z","shell.execute_reply.started":"2021-08-01T15:52:59.693268Z","shell.execute_reply":"2021-08-01T16:25:17.494560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n\n# img = cv2.imread(r'./train_png/a33697e797cc/69ccac342782/014a736182d3.tiff', cv2.IMREAD_UNCHANGED)\n# print(img.shape)\n# plt.imshow((img*255).astype(np.uint8))","metadata":{"execution":{"iopub.status.busy":"2021-08-01T16:25:17.497382Z","iopub.execute_input":"2021-08-01T16:25:17.497672Z","iopub.status.idle":"2021-08-01T16:25:17.501900Z","shell.execute_reply.started":"2021-08-01T16:25:17.497625Z","shell.execute_reply":"2021-08-01T16:25:17.500771Z"},"trusted":true},"execution_count":null,"outputs":[]}]}