{"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 os\nimport numpy as np\nimport json\nimport pandas as pd\nimport cv2\nimport tifffile as tiff\nfrom PIL import Image\nimport plotly.express as px\nimport plotly.graph_objects as go","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-18T11:07:52.993866Z","iopub.execute_input":"2023-06-18T11:07:52.994344Z","iopub.status.idle":"2023-06-18T11:07:54.276103Z","shell.execute_reply.started":"2023-06-18T11:07:52.994304Z","shell.execute_reply":"2023-06-18T11:07:54.274978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi_meta = pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv')\nwsi_meta","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:54.278689Z","iopub.execute_input":"2023-06-18T11:07:54.279015Z","iopub.status.idle":"2023-06-18T11:07:54.321796Z","shell.execute_reply.started":"2023-06-18T11:07:54.278987Z","shell.execute_reply":"2023-06-18T11:07:54.320650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tile_mask = pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv')\ntile_mask","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:54.323181Z","iopub.execute_input":"2023-06-18T11:07:54.323822Z","iopub.status.idle":"2023-06-18T11:07:54.355043Z","shell.execute_reply.started":"2023-06-18T11:07:54.323780Z","shell.execute_reply":"2023-06-18T11:07:54.354036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as json_file:\n    json_list = list(json_file)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:54.356306Z","iopub.execute_input":"2023-06-18T11:07:54.357151Z","iopub.status.idle":"2023-06-18T11:07:54.720515Z","shell.execute_reply.started":"2023-06-18T11:07:54.357120Z","shell.execute_reply":"2023-06-18T11:07:54.719441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(json_list)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:54.724454Z","iopub.execute_input":"2023-06-18T11:07:54.724914Z","iopub.status.idle":"2023-06-18T11:07:54.731252Z","shell.execute_reply.started":"2023-06-18T11:07:54.724855Z","shell.execute_reply":"2023-06-18T11:07:54.730243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tiles_dicts = []\nfor json_str in json_list:\n    tiles_dicts.append(json.loads(json_str))\n    \nlen(tiles_dicts)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:54.732623Z","iopub.execute_input":"2023-06-18T11:07:54.733483Z","iopub.status.idle":"2023-06-18T11:07:59.348743Z","shell.execute_reply.started":"2023-06-18T11:07:54.733445Z","shell.execute_reply":"2023-06-18T11:07:59.347690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_coordinates(annot):\n    xs = []\n    ys= []\n    coords = annot['coordinates']\n    for i in coords[0]:\n        x = i[0]\n        y = i[1]\n        xs.append(x)\n        ys.append(y)\n        \n    return xs, ys\n    \ndef plot_annotated_images(image_dict, scale_factor=1):\n    sample_img_path = f'/kaggle/input/hubmap-hacking-the-human-vasculature/train/{image_dict[\"id\"]}.tif'\n    annotations = tiles_dicts[0]['annotations']\n    sample_img = tiff.imread(sample_img_path)\n    sample_img = Image.fromarray(sample_img)\n    fig = go.Figure()\n    img_width = sample_img.size[0]\n    img_height = sample_img.size[1]\n    print(img_width, img_height)\n\n    fig.add_trace(\n        go.Scatter(x = [0, img_width],\n                   y = [0, img_height],\n                   mode= 'markers', marker_opacity=0.5)\n        )\n\n    fig.update_xaxes(\n        visible = False,\n        range= [0, img_width]\n    )\n\n    fig.update_yaxes(\n        visible = False,\n        range= [0, img_width],\n        scaleanchor = 'x'\n    )\n\n    fig.add_layout_image(dict(\n            x=0,\n            sizex = img_width,\n            y=img_height,\n            sizey = img_height,\n            xref='x',\n            yref='y',\n            layer='below',\n            opacity=1.0,\n            sizing='stretch',\n            source=sample_img\n        )\n    )\n\n    # Annotations\n    for annot in annotations:\n        name = str(annot['type'])\n        xs, ys = get_coordinates(annot)\n\n        fig.add_trace(\n            go.Scatter(x = xs, \n                       y = ys,\n                       name = name,\n                       hovertemplate = f'{name}',\n                       fill = 'toself',\n                       mode = 'lines'\n                      ))\n    fig.update_layout(\n        width = scale_factor * img_width,\n        height = scale_factor * img_height,\n        margin = {'l': 0, 'r': 0, 't': 0, 'b': 0},\n        showlegend = False\n    )\n\n\n    fig.show(config={'doubleClick': 'reset'})","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:59.350233Z","iopub.execute_input":"2023-06-18T11:07:59.350656Z","iopub.status.idle":"2023-06-18T11:07:59.365560Z","shell.execute_reply.started":"2023-06-18T11:07:59.350627Z","shell.execute_reply":"2023-06-18T11:07:59.364280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_images(tiles_dicts[1])","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:59.366882Z","iopub.execute_input":"2023-06-18T11:07:59.367204Z","iopub.status.idle":"2023-06-18T11:07:59.843647Z","shell.execute_reply.started":"2023-06-18T11:07:59.367176Z","shell.execute_reply":"2023-06-18T11:07:59.842590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nmask = np.zeros((512, 512))\nfor annot in tiles_dicts[33]['annotations']:\n    coords = annot['coordinates']\n    if annot['type'] == 'blood_vessel':\n        for cd in coords:\n            x, y = np.array([i[1] for i in cd]), np.asarray([i[0] for i in cd])\n            mask[x, y] = 1\nplt.imshow(mask)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:07:59.845578Z","iopub.execute_input":"2023-06-18T11:07:59.846347Z","iopub.status.idle":"2023-06-18T11:08:00.166066Z","shell.execute_reply.started":"2023-06-18T11:07:59.846308Z","shell.execute_reply":"2023-06-18T11:08:00.164918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"contours,_ = cv2.findContours((mask*255).astype(np.uint8), 1, 2)\nzero_img = np.zeros([mask.shape[0], mask.shape[1], 3], dtype=\"uint8\")\n\nfor p in contours:\n    cv2.fillPoly(zero_img, [p], (255, 255, 255))\n    \nplt.imshow(zero_img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:08:00.167152Z","iopub.execute_input":"2023-06-18T11:08:00.167493Z","iopub.status.idle":"2023-06-18T11:08:00.480244Z","shell.execute_reply.started":"2023-06-18T11:08:00.167463Z","shell.execute_reply":"2023-06-18T11:08:00.479394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from copy import deepcopy\ncontours, hierarchy = cv2.findContours(mask.astype(\"uint8\"), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\nimg_with_area = zero_img\nprint(img_with_area.shape)\n\nprint(len(contours))\n        \nfor i in range(len(contours)):\n    if cv2.contourArea(contours[i]) > (mask.shape[0] * mask.shape[1]) * 0.0001:\n        cv2.fillPoly(img_with_area, [contours[i][:,0,:]], (255-4*(i+1),255-4*(i+1),255-4*(i+1)), lineType=cv2.LINE_8, shift=0)\n        \nplt.imshow(img_with_area)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:08:00.481678Z","iopub.execute_input":"2023-06-18T11:08:00.482557Z","iopub.status.idle":"2023-06-18T11:08:00.786533Z","shell.execute_reply.started":"2023-06-18T11:08:00.482523Z","shell.execute_reply":"2023-06-18T11:08:00.785314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_seg_mask(tiles_dict):\n    mask = np.zeros((512, 512), dtype=np.float32)\n    for annot in tiles_dict['annotations']:\n        cords = annot['coordinates']\n        if annot['type'] == \"blood_vessel\":\n            for cd in cords:\n                rr, cc = np.array([i[1] for i in cd]), np.asarray([i[0] for i in cd])\n                mask[rr, cc] = 1\n                \n    contours,_ = cv2.findContours((mask*255).astype(np.uint8), 1, 2)\n    zero_img = np.zeros([mask.shape[0], mask.shape[1], 3], dtype=\"uint8\")\n\n    for p in contours:\n        cv2.fillPoly(zero_img, [p], (255, 255, 255))\n\n    contours, hierarchy = cv2.findContours(mask.astype(\"uint8\"), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n    img_with_area = zero_img\n\n    for i in range(len(contours)):\n        cv2.fillPoly(img_with_area, [contours[i][:,0,:]], (255-4*(i+1),255-4*(i+1),255-4*(i+1)), lineType=cv2.LINE_8, shift=0)\n            \n    return img_with_area    ","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:08:00.788029Z","iopub.execute_input":"2023-06-18T11:08:00.788438Z","iopub.status.idle":"2023-06-18T11:08:00.803657Z","shell.execute_reply.started":"2023-06-18T11:08:00.788407Z","shell.execute_reply":"2023-06-18T11:08:00.800713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nos.makedirs('train/image', exist_ok=True)\nos.makedirs('train/mask', exist_ok=True)\n\nfor i, tldc in enumerate(tqdm(tiles_dicts)):\n    array = tiff.imread(f'/kaggle/input/hubmap-hacking-the-human-vasculature/train/{tldc[\"id\"]}.tif')\n    img_example = Image.fromarray(array)\n    img = np.array(img_example)\n    mask = make_seg_mask(tldc)\n    \n    if np.sum(mask)>0:\n\n        cv2.imwrite(f'train/image/{tldc[\"id\"]}.png', img)\n        cv2.imwrite(f'train/mask/{tldc[\"id\"]}_mask.png', mask)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:08:00.805108Z","iopub.execute_input":"2023-06-18T11:08:00.805565Z","iopub.status.idle":"2023-06-18T11:09:33.631947Z","shell.execute_reply.started":"2023-06-18T11:08:00.805525Z","shell.execute_reply":"2023-06-18T11:09:33.631161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('test/image', exist_ok=True)\ntest_path = '/kaggle/input/hubmap-hacking-the-human-vasculature/test'\nimg_names = os.listdir(test_path)\nfor img_name in tqdm(img_names):\n    array = tiff.imread(f'/kaggle/input/hubmap-hacking-the-human-vasculature/test/' + img_name)\n    img_example = Image.fromarray(array)\n    img = np.array(img_example)\n    print(f'test/image/{img_name[:-4]}.png')\n    cv2.imwrite(f'test/image/{img_name[:-4]}.png', img)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T11:13:30.724019Z","iopub.execute_input":"2023-06-18T11:13:30.724431Z","iopub.status.idle":"2023-06-18T11:13:30.776494Z","shell.execute_reply.started":"2023-06-18T11:13:30.724398Z","shell.execute_reply":"2023-06-18T11:13:30.775400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}