{"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":"This basic notebook decodes the annotation of and visualizes them for a given image","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"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)\nimport os\nfrom torchvision.io import read_image\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-04T02:05:12.961812Z","iopub.execute_input":"2021-11-04T02:05:12.962162Z","iopub.status.idle":"2021-11-04T02:05:14.557043Z","shell.execute_reply.started":"2021-11-04T02:05:12.962073Z","shell.execute_reply":"2021-11-04T02:05:14.556229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the annotation table","metadata":{}},{"cell_type":"code","source":"dataDir = '/kaggle/input/sartorius-cell-instance-segmentation/'\ntrain = pd.read_csv(dataDir + 'train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-04T02:05:14.558554Z","iopub.execute_input":"2021-11-04T02:05:14.558756Z","iopub.status.idle":"2021-11-04T02:05:15.194337Z","shell.execute_reply.started":"2021-11-04T02:05:14.558730Z","shell.execute_reply":"2021-11-04T02:05:15.193421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define a function to decode RLE (Run Length Encoding)","metadata":{}},{"cell_type":"code","source":"def decodeRLE(annotation, imgShape=(520,704)):\n    s = annotation.split(' ')\n    pixelList = []\n    for i in range(len(s)//2):\n        start = int(s[2*i])\n        increment = int(s[2*i+1])\n        for j in range(start,start+increment):\n            pixelList.append(j)\n    annotation = np.zeros(imgShape).flatten()\n    annotation[pixelList] = 1.\n    annotation = annotation.reshape(imgShape)\n    return annotation","metadata":{"execution":{"iopub.status.busy":"2021-11-04T02:06:43.705099Z","iopub.execute_input":"2021-11-04T02:06:43.705427Z","iopub.status.idle":"2021-11-04T02:06:43.712255Z","shell.execute_reply.started":"2021-11-04T02:06:43.705393Z","shell.execute_reply":"2021-11-04T02:06:43.711295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize the first image and its annotations","metadata":{}},{"cell_type":"code","source":"imageID = '0030fd0e6378'\n\nfig, ax = plt.subplots(1, 3, figsize=(20,5))\nplt.subplots_adjust(wspace=0.01)\n\n# Load the image\nimg = read_image('/kaggle/input/sartorius-cell-instance-segmentation/train/' + imageID + '.png').squeeze()\nplt.subplot(131)\nplt.imshow(img, cmap='gray')\nplt.title('Original Image')\nplt.axis('off')\n\n# Single Annotation\nsingleAnnotation = decodeRLE(train.iloc[0,1])\nplt.subplot(132)\nplt.imshow(img, cmap='gray')\nplt.imshow(singleAnnotation, cmap='gray', alpha=0.5)\nplt.title('Single Annotation')\nplt.axis('off')\n\n# All annotations\nallAnnotations = np.zeros(img.shape)\ntrain_subset = train[train['id']==imageID]\nfor index, row in train_subset.iterrows():\n    allAnnotations += decodeRLE(row.annotation)\nplt.subplot(133)\nplt.imshow(img, cmap='gray')\nplt.imshow(allAnnotations, cmap='gray', alpha=0.5)\nplt.title('All Annotations')\nplt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-04T02:12:24.071417Z","iopub.execute_input":"2021-11-04T02:12:24.072407Z","iopub.status.idle":"2021-11-04T02:12:25.056047Z","shell.execute_reply.started":"2021-11-04T02:12:24.072349Z","shell.execute_reply":"2021-11-04T02:12:25.055197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}