{"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":"# Visualize Blood Cells Interactively","metadata":{}},{"cell_type":"code","source":"import glob\nimport json\nimport cv2\nimport matplotlib.pyplot as plt\nimport ipywidgets as widgets\nimport numpy as np\nimport IPython.display as ipd\n\n\ntrain = glob.glob('/kaggle/input/hubmap-hacking-the-human-vasculature/train/*')\ntest = glob.glob('/kaggle/input/hubmap-hacking-the-human-vasculature/test/*')\n\nwith open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as f:\n    polygons = [json.loads(p) for p in list(f)]\n\nimg_map = {impath.split('/')[-1].split('.')[0]: impath for impath in train}\nimg_map.update({impath.split('/')[-1].split('.')[0]: impath for impath in test})\n\npolygon_map = {polygon['id']: polygon for polygon in polygons}\n\nprint(f'total images: {len(img_map)}')\nprint(f'annotated images: {len(polygon_map)}')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T09:58:14.012990Z","iopub.execute_input":"2023-05-24T09:58:14.014046Z","iopub.status.idle":"2023-05-24T09:58:17.807605Z","shell.execute_reply.started":"2023-05-24T09:58:14.014007Z","shell.execute_reply":"2023-05-24T09:58:17.805976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Select The Image You Want To Visualize\n\nBlood_vessel is **<span style='color:green;'>green</span>** and otherwise **black**","metadata":{}},{"cell_type":"code","source":"def draw(img_id):    \n    polygon = polygon_map[img_id]\n    img = cv2.imread(img_map[img_id])\n\n    blood_vessel = 0\n    glomerulus = 0\n    unsure = 0\n    for anno in polygon['annotations']:\n\n        if anno['type'] == 'blood_vessel':\n            color = (0,255,0)\n            blood_vessel += 1\n\n        elif anno['type'] == 'glomerulus':\n            color = (0,0,0)\n            glomerulus += 1\n\n        else:\n            color = (0,0,0)\n            unsure += 1\n\n        pts = anno['coordinates']\n        pts = np.array(pts)\n        pts = pts.reshape(-1, 1, 2)\n        cv2.polylines(img, pts, True, color, 3)\n    \n    print(f'{blood_vessel = }')\n    print(f'{glomerulus = }')\n    print(f'{unsure = }')\n\n    plt.imshow(img)\n    plt.axis('off')\n    plt.show()\n    \n    \noutput = widgets.Output()\n@output.capture()\ndef ipydisplay(change):\n    img_id = change['new']\n    ipd.clear_output()\n    draw(img_id)\n\n# you can only use this widget when actually running the notebook\nselect = widgets.Dropdown(options=list(polygon_map.keys()))\nselect.observe(ipydisplay, 'value')\nwidgets.VBox([select, output])","metadata":{"execution":{"iopub.status.busy":"2023-05-24T09:58:17.809869Z","iopub.execute_input":"2023-05-24T09:58:17.810378Z","iopub.status.idle":"2023-05-24T09:58:17.855580Z","shell.execute_reply.started":"2023-05-24T09:58:17.810329Z","shell.execute_reply":"2023-05-24T09:58:17.854420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5 Samples","metadata":{}},{"cell_type":"code","source":"for img_id in list(polygon_map.keys())[:5]:\n    draw(img_id)\n    print()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T09:58:17.857130Z","iopub.execute_input":"2023-05-24T09:58:17.857460Z","iopub.status.idle":"2023-05-24T09:58:19.047845Z","shell.execute_reply.started":"2023-05-24T09:58:17.857432Z","shell.execute_reply":"2023-05-24T09:58:19.046490Z"},"trusted":true},"execution_count":null,"outputs":[]}]}