{"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 pandas as pd\nimport numpy as np\nimport cv2\n\nimport os\n\nfrom matplotlib import pyplot as plt\n\nfrom tqdm.auto import tqdm\n\nsave_dir = f'/kaggle/tmp/mask'\nsize = 512\n\nos.makedirs(save_dir, exist_ok=True)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-26T11:53:33.857490Z","iopub.execute_input":"2021-06-26T11:53:33.857921Z","iopub.status.idle":"2021-06-26T11:53:33.864245Z","shell.execute_reply.started":"2021-06-26T11:53:33.857885Z","shell.execute_reply":"2021-06-26T11:53:33.863119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\n\nmeta = pd.read_csv('../input/siim-covid19-resized-to-512px-png/meta.csv')\nmeta = meta[meta.split=='train'].reset_index(drop=True)\n\ntrain['id'] = train.apply(lambda row: row.id.split('_')[0], axis=1)\ntrain = train.rename(columns={'id': 'image_id'})\ntrain_meta = pd.merge(train, meta, how='inner', on='image_id')\ntrain_meta = train_meta.drop(['boxes', 'split', 'StudyInstanceUID'], axis=1)\ntrain_meta.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:35.143635Z","iopub.execute_input":"2021-06-26T11:53:35.144281Z","iopub.status.idle":"2021-06-26T11:53:35.320745Z","shell.execute_reply.started":"2021-06-26T11:53:35.144232Z","shell.execute_reply":"2021-06-26T11:53:35.320052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show one mask image","metadata":{}},{"cell_type":"code","source":"image_id = '000a312787f2'\ni = int(train_meta[train_meta.image_id==image_id].index.values)\n\nlabel = train_meta.loc[i, 'label'].split()\nif label != 'none':\n    label = label\n    dim0 = train_meta.loc[i, 'dim0']\n    dim1 = train_meta.loc[i, 'dim1']\n\n    mask = np.zeros((size, size), dtype=np.uint8)\n    for j in range(len(label) // 6):\n        xmin = int(np.round(float(label[6*j+2])*size/dim1, 4))\n        ymin = int(np.round(float(label[6*j+3])*size/dim0, 4))\n        xmax = int(np.round(float(label[6*j+4])*size/dim1, 4))\n        ymax = int(np.round(float(label[6*j+5])*size/dim0, 4))\n        cv2.rectangle(mask, (xmin, ymin), (xmax, ymax), color=255, thickness=-1)\n\n    plt.imshow(mask)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:36.789851Z","iopub.execute_input":"2021-06-26T11:53:36.790254Z","iopub.status.idle":"2021-06-26T11:53:36.957665Z","shell.execute_reply.started":"2021-06-26T11:53:36.790206Z","shell.execute_reply":"2021-06-26T11:53:36.956620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save all mask images","metadata":{}},{"cell_type":"code","source":"for i in tqdm(range(len(train_meta))):\n    label = train_meta.loc[i, 'label'].split()\n    \n    if label[0] == 'none':\n        continue\n    \n    image_id = train_meta.loc[i, 'image_id']\n    dim0 = train_meta.loc[i, 'dim0']\n    dim1 = train_meta.loc[i, 'dim1']\n    \n    mask = np.zeros((size, size), dtype=np.uint8)\n    for j in range(len(label) // 6):\n        xmin = int(np.round(float(label[j*6+2])*size/dim1, 4))\n        ymin = int(np.round(float(label[j*6+3])*size/dim0, 4))\n        xmax = int(np.round(float(label[j*6+4])*size/dim1, 4))\n        ymax = int(np.round(float(label[j*6+5])*size/dim0, 4))\n        cv2.rectangle(mask, (xmin, ymin), (xmax, ymax), color=255, thickness=-1)\n    cv2.imwrite(f'{save_dir}/{image_id}.png', mask)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:53:48.556759Z","iopub.execute_input":"2021-06-26T11:53:48.557204Z","iopub.status.idle":"2021-06-26T11:53:54.626375Z","shell.execute_reply.started":"2021-06-26T11:53:48.557163Z","shell.execute_reply":"2021-06-26T11:53:54.625325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n!tar -zcf mask.tar.gz -C \"/kaggle/tmp/mask/\" .","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:54:00.383045Z","iopub.execute_input":"2021-06-26T11:54:00.383544Z","iopub.status.idle":"2021-06-26T11:54:01.344667Z","shell.execute_reply.started":"2021-06-26T11:54:00.383511Z","shell.execute_reply":"2021-06-26T11:54:01.343535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}