{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":7144905,"sourceType":"datasetVersion","datasetId":4118928}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# UBC-OCEAN QuPath GeoJSON Annotation Example","metadata":{}},{"cell_type":"markdown","source":"# Pyvips","metadata":{}},{"cell_type":"code","source":"!sudo apt-get update\n!sudo apt-get install -y libvips libvips42\n!pip install pyvips\nimport pyvips","metadata":{"execution":{"iopub.status.busy":"2023-12-07T09:41:23.747235Z","iopub.execute_input":"2023-12-07T09:41:23.748013Z","iopub.status.idle":"2023-12-07T09:42:09.09443Z","shell.execute_reply.started":"2023-12-07T09:41:23.747982Z","shell.execute_reply":"2023-12-07T09:42:09.092878Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing geojson is easy as it is already installed","metadata":{}},{"cell_type":"code","source":"#!pip install geojson\nimport geojson","metadata":{"execution":{"iopub.status.busy":"2023-12-07T09:42:54.557007Z","iopub.execute_input":"2023-12-07T09:42:54.55748Z","iopub.status.idle":"2023-12-07T09:43:03.763583Z","shell.execute_reply.started":"2023-12-07T09:42:54.557448Z","shell.execute_reply":"2023-12-07T09:43:03.762755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-07T09:43:05.97671Z","iopub.execute_input":"2023-12-07T09:43:05.977638Z","iopub.status.idle":"2023-12-07T09:43:06.281399Z","shell.execute_reply.started":"2023-12-07T09:43:05.977604Z","shell.execute_reply":"2023-12-07T09:43:06.280724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = '/kaggle/input/UBC-OCEAN'\nTRAIN_DIR = '/kaggle/input/UBC-OCEAN/train_images'\nANNOTATIONS_DIR = '/kaggle/input/ubc-ocean-annotations'\n\ndf = pd.read_csv(f\"{ROOT_DIR}/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-12-07T10:06:16.933395Z","iopub.execute_input":"2023-12-07T10:06:16.934706Z","iopub.status.idle":"2023-12-07T10:06:16.945078Z","shell.execute_reply.started":"2023-12-07T10:06:16.934662Z","shell.execute_reply":"2023-12-07T10:06:16.943861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get all image id's or a selection","metadata":{}},{"cell_type":"code","source":"#image_id_list = df['image_id'].tolist()\n#png_list = [f\"{i}.png\" for i in image_id_list]\npng_list = [f\"{i}.png\" for i in [4, 91]]\n\n# loop through the png_list\nfor png_file in png_list:\n    print(f'{png_file[:-4]}')\n    # read a png file from the directory\n    image = pyvips.Image.new_from_file(f'{TRAIN_DIR}/{png_file}')\n    \n    with open(f\"{ANNOTATIONS_DIR}/{png_file[:-4]}.geojson\") as f:\n            gj = geojson.load(f)","metadata":{"execution":{"iopub.status.busy":"2023-12-07T10:06:19.326022Z","iopub.execute_input":"2023-12-07T10:06:19.326339Z","iopub.status.idle":"2023-12-07T10:06:19.555828Z","shell.execute_reply.started":"2023-12-07T10:06:19.326316Z","shell.execute_reply":"2023-12-07T10:06:19.554679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Iterate through the dataset","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\nfor index, row in df.iterrows():\n        image_number = row['image_id']\n    \n        #image = pyvips.Image.new_from_file(f'{TRAIN_DIR}/{image_number}.png')\n        #image.tiffsave(f'{image_number}.tif', compression=\"jpeg\", Q=95, tile=True, pyramid=True, properties=True, xres=image.xres, yres=image.yres)\n        \n        if Path(f\"{ANNOTATIONS_DIR}/{image_number}.geojson\").is_file():    \n            with open(f\"{ANNOTATIONS_DIR}/{image_number}.geojson\") as f:\n                gj = geojson.load(f)\n                print(\"geojson loaded\")","metadata":{"execution":{"iopub.status.busy":"2023-12-07T10:06:22.23786Z","iopub.execute_input":"2023-12-07T10:06:22.238319Z","iopub.status.idle":"2023-12-07T10:06:23.084257Z","shell.execute_reply.started":"2023-12-07T10:06:22.238293Z","shell.execute_reply":"2023-12-07T10:06:23.083332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n","metadata":{},"execution_count":null,"outputs":[]}]}