{"cells":[{"metadata":{},"cell_type":"markdown","source":"Kaggle TPU [usage guide](https://www.kaggle.com/docs/tpu)"},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob\nimport matplotlib.pyplot as plt\nimport rasterio\nimport numpy as np\nfrom PIL import Image, ImageDraw\nimport json","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in glob.glob('/kaggle/input/hubmap-kidney-segmentation/train/*.tiff'):\n    print(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"identity = rasterio.Affine(1, 0, 0, 0, 1, 0)\npath = '/kaggle/input/hubmap-kidney-segmentation/train/'\nfilename = '095bf7a1f'\nimage = rasterio.open(path + filename + '.tiff', transform = identity)\nwith open(path + filename + '.json') as file:\n    label = json.load(file) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_image(img, offset):\n    \n    i = offset[1] * IMG_SIZE\n    j = offset[0] * IMG_SIZE\n    image = img.read([1,2,3], window=((i, i + IMG_SIZE),(j, j + IMG_SIZE)))\n    \n    return np.moveaxis(image, 0, -1)\n\ndef transform(x):\n    \n    x[0] -= offset[0] * IMG_SIZE\n    x[1] -= offset[1] * IMG_SIZE\n    \n    return tuple(x)\n\ndef get_label(polygon):\n    \n    geom = polygon['geometry']['coordinates'][0]\n    tup_arr = list(map(lambda x: transform(x), geom))\n    \n    return tup_arr\n\ndef get_overlay(image, label):\n    \n    img_copy = image.copy()\n    draw = ImageDraw.Draw(img_copy)\n    draw.polygon(label, fill = \"wheat\")\n    img_overlay = Image.blend(image, img_copy, 0.5)\n    \n    return img_overlay","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"POLY_NUM = 15\nIMG_SIZE = 1024","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"geometry = label[POLY_NUM]['geometry']['coordinates'][0]\noffset = np.array(geometry).min(axis=0) // IMG_SIZE\nimage_slice = get_image(image, offset)\narr = get_label(label[POLY_NUM])\npil_img = Image.fromarray(image_slice)\nimg_with_label = get_overlay(pil_img, arr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(20, 20))\n\nplt.subplot(121)\nplt.imshow(pil_img)\nplt.title('Original Image')\n\nplt.subplot(122)\nplt.imshow(img_with_label)\nplt.title('Image with segmentation');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}