{"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":"! pip install pylibjpeg python-gdcm pydicom","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-10T16:31:09.000696Z","iopub.execute_input":"2023-02-10T16:31:09.001104Z","iopub.status.idle":"2023-02-10T16:31:21.210998Z","shell.execute_reply.started":"2023-02-10T16:31:09.001024Z","shell.execute_reply":"2023-02-10T16:31:21.209355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !jupyter notebook --NotebookApp.iopub_data_rate_limit=1e10 --allow-root","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:21.214603Z","iopub.execute_input":"2023-02-10T16:31:21.214931Z","iopub.status.idle":"2023-02-10T16:31:21.220929Z","shell.execute_reply.started":"2023-02-10T16:31:21.214904Z","shell.execute_reply":"2023-02-10T16:31:21.219092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport gdcm\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:21.223276Z","iopub.execute_input":"2023-02-10T16:31:21.223749Z","iopub.status.idle":"2023-02-10T16:31:22.169258Z","shell.execute_reply.started":"2023-02-10T16:31:21.223708Z","shell.execute_reply":"2023-02-10T16:31:22.167819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob.glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\")\n\nlen(train_images)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:22.170664Z","iopub.execute_input":"2023-02-10T16:31:22.170989Z","iopub.status.idle":"2023-02-10T16:31:55.163365Z","shell.execute_reply.started":"2023-02-10T16:31:22.170958Z","shell.execute_reply":"2023-02-10T16:31:55.162473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i in tqdm(train_images[:3]):\n#     person = i.split('/')[-2]\n#     image = i.split('/')[:-4]\n    \n#     dicom = pydicom.dcmread(i)\n#     img = dicom.pixel_array\n    \n#     img = (img - img.min()) / (img.max() - img.min())\n    \n#     if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n#         img = 1 - img\n        \n#     plt.figure(figsize = (15,15))\n#     plt.imshow(img, cmap=\"gray\")\n#     plt.title(f\"{person} {image}\")\n    \n#     plt.show()             ","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:55.165952Z","iopub.execute_input":"2023-02-10T16:31:55.166263Z","iopub.status.idle":"2023-02-10T16:31:55.171915Z","shell.execute_reply.started":"2023-02-10T16:31:55.166236Z","shell.execute_reply":"2023-02-10T16:31:55.170347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAVE_FOLDER = \"\"\nSIZE = 512\nEXTENSION = \"png\"\n\n# os.makedirs(SAVE_FOLDER,exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:55.173936Z","iopub.execute_input":"2023-02-10T16:31:55.174618Z","iopub.status.idle":"2023-02-10T16:31:55.194066Z","shell.execute_reply.started":"2023-02-10T16:31:55.174576Z","shell.execute_reply":"2023-02-10T16:31:55.192894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(i, size=512, save_folder =\"\",extension=\"png\"):\n    person = i.split('/')[-2]\n    image = i.split('/')[-1][:-4]\n    \n    dicom = pydicom.dcmread(i)\n    img = dicom.pixel_array\n    \n    img = (img -img.min()) / (img.max() - img.min())\n    \n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n    \n    img = cv2.resize(img, (size,size))\n    cv2.imwrite(save_folder + f\"{image}.{extension}\", (img * 255).astype(np.uint8))","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:55.195475Z","iopub.execute_input":"2023-02-10T16:31:55.19606Z","iopub.status.idle":"2023-02-10T16:31:55.206779Z","shell.execute_reply.started":"2023-02-10T16:31:55.196017Z","shell.execute_reply":"2023-02-10T16:31:55.205317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for uid in tqdm(train_images):\n    process(uid, size=512, save_folder =SAVE_FOLDER,extension=\"png\")","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:55.208507Z","iopub.execute_input":"2023-02-10T16:31:55.209191Z","iopub.status.idle":"2023-02-10T16:32:02.921554Z","shell.execute_reply.started":"2023-02-10T16:31:55.209148Z","shell.execute_reply":"2023-02-10T16:32:02.920464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# _ = Parallel(n_jobs = -1)(delayed(process)(uid,size = SIZE, save_folder = SAVE_FOLDER,\n#                                          extension = EXTENSION)\n#                         for uid in tqdm(train_images))","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:32:02.923325Z","iopub.execute_input":"2023-02-10T16:32:02.924162Z","iopub.status.idle":"2023-02-10T16:32:02.928328Z","shell.execute_reply.started":"2023-02-10T16:32:02.924128Z","shell.execute_reply":"2023-02-10T16:32:02.927405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}