{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-21T04:24:44.534885Z","iopub.execute_input":"2021-06-21T04:24:44.535400Z","iopub.status.idle":"2021-06-21T04:24:44.539652Z","shell.execute_reply.started":"2021-06-21T04:24:44.535357Z","shell.execute_reply":"2021-06-21T04:24:44.538678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\n\nimport pydicom as dicom\nimport os\nimport cv2\nimport PIL # optional\nimport tensorflow as tf\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2021-06-21T04:24:53.247376Z","iopub.execute_input":"2021-06-21T04:24:53.247721Z","iopub.status.idle":"2021-06-21T04:24:55.932140Z","shell.execute_reply.started":"2021-06-21T04:24:53.247691Z","shell.execute_reply":"2021-06-21T04:24:55.931260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nkf = pd.DataFrame(columns=['data_set_type','StudyInstanceUID','Sub_folder','image_file_name','image_file_path'])\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        kf.loc[kf.shape[0]] = [dirname.split('/')[-3], dirname.split('/')[-2],dirname.split('/')[-1],filename,os.path.join(dirname, filename)]\n\ndf = pd.DataFrame(kf.drop([0,1,2],axis=0).values,columns=kf.columns)\ndf","metadata":{"execution":{"iopub.status.busy":"2021-06-21T04:24:58.401031Z","iopub.execute_input":"2021-06-21T04:24:58.401385Z","iopub.status.idle":"2021-06-21T04:25:34.605190Z","shell.execute_reply.started":"2021-06-21T04:24:58.401357Z","shell.execute_reply":"2021-06-21T04:25:34.604331Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport SimpleITK\nimport cv2\nfrom tqdm import tqdm\nimport shutil\n \ndef convert_from_dicom_to_jpg(dcm_image_path, output_jpg_path):\n           \n    ds_array = SimpleITK.ReadImage(dcm_image_path)  \n    img_array = SimpleITK.GetArrayFromImage(ds_array)  \n            \n    shape = img_array.shape\n    img_array = np.reshape(img_array, (shape[1], shape[2]))  \n    high = np.max(img_array)\n    low = np.min(img_array)\n        \n    lungwin = np.array([low*1.,high*1.])\n    newimg = (img_array-lungwin[0])/(lungwin[1]-lungwin[0])\n    newimg = (newimg*255).astype('uint8')\n    stacked_img = np.stack((newimg,) * 3, axis=-1)\n    x_imag = tf.keras.preprocessing.image.array_to_img(stacked_img)\n    \n    # required image format and size as a input to Deep learning network\n    \n    ht = tf.keras.preprocessing.image.img_to_array(x_imag.resize([256,256]))\n    \n    cv2.imwrite(output_jpg_path, ht, [int(cv2.IMWRITE_JPEG_QUALITY), 10])\n    #return ht","metadata":{"execution":{"iopub.status.busy":"2021-06-21T04:25:47.055830Z","iopub.execute_input":"2021-06-21T04:25:47.056321Z","iopub.status.idle":"2021-06-21T04:25:47.412224Z","shell.execute_reply.started":"2021-06-21T04:25:47.056291Z","shell.execute_reply":"2021-06-21T04:25:47.411359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use this file only if the images are readily avaiable in png format \n# We have converted dicom to png and upoaded to \"/Kaggle/input/\" folder\nitr = 0\n\nif os.path.exists('/kaggle/working/siim-covid19-detection/train/') == True:\n    shutil.rmtree('/kaggle/working/')\nif os.path.exists('/kaggle/working/siim-covid19-detection/test/') == True:\n    shutil.rmtree('/kaggle/working/')\n        \nos.makedirs('/kaggle/working/siim-covid19-detection/test/')\nos.makedirs('/kaggle/working/siim-covid19-detection/train/')\n\nfor i in df['data_set_type'].values:\n    if i == 'train':\n        dcm_image_path = df['image_file_path'].values[itr]\n        output_jpg_path = '/kaggle/working/siim-covid19-detection/train/' + df['StudyInstanceUID'].values[itr] +'_'+ df['image_file_name'].values[itr].split('.')[0] + '.png'\n        convert_from_dicom_to_jpg(dcm_image_path, output_jpg_path)\n    else:\n        dcm_image_path = df['image_file_path'].values[itr]\n        output_jpg_path = '/kaggle/working/siim-covid19-detection/test/' + df['StudyInstanceUID'].values[itr] +'_'+ df['image_file_name'].values[itr].split('.')[0] + '.png'\n        convert_from_dicom_to_jpg(dcm_image_path, output_jpg_path)\n        \n    itr = itr + 1\n    print(itr)","metadata":{"execution":{"iopub.status.busy":"2021-06-21T04:25:47.771736Z","iopub.execute_input":"2021-06-21T04:25:47.772071Z","iopub.status.idle":"2021-06-21T04:30:09.287714Z","shell.execute_reply.started":"2021-06-21T04:25:47.772042Z","shell.execute_reply":"2021-06-21T04:30:09.285341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}