{"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":"# Install pycocotools package\nimport os\n!mkdir /kaggle/working/packages\n!cp -r /kaggle/input/pycocotools/* /kaggle/working/packages\nos.chdir(\"/kaggle/working/packages/pycocotools-2.0.6/\")\n!python setup.py install\n!pip install . --no-index --find-links /kaggle/working/packages/\nos.chdir(\"/kaggle/working\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-26T07:33:59.778694Z","iopub.execute_input":"2023-05-26T07:33:59.779194Z","iopub.status.idle":"2023-05-26T07:34:47.763700Z","shell.execute_reply.started":"2023-05-26T07:33:59.779155Z","shell.execute_reply":"2023-05-26T07:34:47.762345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\n\nimport base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\n\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n    \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n    # check input mask --\n    if mask.dtype != np.bool:\n        raise ValueError(\"encode_binary_mask expects a binary mask, received dtype == %s\" % mask.dtype)\n\n    mask = np.squeeze(mask)\n    if len(mask.shape) != 2:\n        raise ValueError(\"encode_binary_mask expects a 2d mask, received shape == %s\" % mask.shape)\n\n    # convert input mask to expected COCO API input --\n    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n    mask_to_encode = mask_to_encode.astype(np.uint8)\n    mask_to_encode = np.asfortranarray(mask_to_encode)\n\n    # RLE encode mask --\n    encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n    \n    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n    base64_str = base64.b64encode(binary_str)\n    return base64_str\n\n\ntest_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test\"\nsample_submission = pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv')\n\nids = []\nheights = []\nwidths = []\nprediction_strings = []\nfor img_name in os.listdir(test_path):\n    img = cv2.imread(f\"{test_path}/{img_name}\", 0)\n    h, w = img.shape\n    \n    mask = np.ones((h, w), dtype=np.bool)\n    encoded = encode_binary_mask(mask)\n    \n    ids.append(img_name.split('.')[0])\n    heights.append(h)\n    widths.append(w)\n    prediction_strings.append(f\"0 1.0 {encoded.decode('utf-8')}\")\n\nsubmission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\n\n\nsubmission = submission.set_index('id')\nprint(submission)\nsubmission.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-26T07:34:47.770179Z","iopub.execute_input":"2023-05-26T07:34:47.770577Z","iopub.status.idle":"2023-05-26T07:34:47.814883Z","shell.execute_reply.started":"2023-05-26T07:34:47.770543Z","shell.execute_reply":"2023-05-26T07:34:47.813766Z"},"trusted":true},"execution_count":null,"outputs":[]}]}