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ions"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "markdown",
|
| 14 |
+
"metadata": {
|
| 15 |
+
"id": "bZPXVnSG8UZ6"
|
| 16 |
+
},
|
| 17 |
+
"source": [
|
| 18 |
+
"Mount drive, install package\n"
|
| 19 |
+
]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"cell_type": "markdown",
|
| 23 |
+
"source": [],
|
| 24 |
+
"metadata": {
|
| 25 |
+
"id": "vR4HxtPIneU3"
|
| 26 |
+
}
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"source": [
|
| 31 |
+
"import sys\n",
|
| 32 |
+
"print(\"Python version\")\n",
|
| 33 |
+
"print(sys.version)"
|
| 34 |
+
],
|
| 35 |
+
"metadata": {
|
| 36 |
+
"id": "xoNfSu7hWryz",
|
| 37 |
+
"outputId": "368b44fc-279a-450e-ce95-9b15b28f2b55",
|
| 38 |
+
"colab": {
|
| 39 |
+
"base_uri": "https://localhost:8080/"
|
| 40 |
+
}
|
| 41 |
+
},
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"outputs": [
|
| 44 |
+
{
|
| 45 |
+
"output_type": "stream",
|
| 46 |
+
"name": "stdout",
|
| 47 |
+
"text": [
|
| 48 |
+
"Python version\n",
|
| 49 |
+
"3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]\n"
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
]
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"cell_type": "code",
|
| 56 |
+
"execution_count": null,
|
| 57 |
+
"metadata": {
|
| 58 |
+
"colab": {
|
| 59 |
+
"base_uri": "https://localhost:8080/"
|
| 60 |
+
},
|
| 61 |
+
"id": "CBxnNgct8T84",
|
| 62 |
+
"outputId": "2c910ebf-3efe-4776-d2bd-02f0c23083a0"
|
| 63 |
+
},
|
| 64 |
+
"outputs": [
|
| 65 |
+
{
|
| 66 |
+
"output_type": "stream",
|
| 67 |
+
"name": "stdout",
|
| 68 |
+
"text": [
|
| 69 |
+
"Collecting git+https://github.com/PatBall1/detectree2.git\n",
|
| 70 |
+
" Cloning https://github.com/PatBall1/detectree2.git to /tmp/pip-req-build-vztr3c29\n",
|
| 71 |
+
" Running command git clone --filter=blob:none --quiet https://github.com/PatBall1/detectree2.git /tmp/pip-req-build-vztr3c29\n",
|
| 72 |
+
" Resolved https://github.com/PatBall1/detectree2.git to commit 2f29886bafcb63743a7f3ab596c5c3796ca4a243\n",
|
| 73 |
+
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
|
| 74 |
+
"Collecting detectron2@ git+https://github.com/facebookresearch/detectron2.git (from detectree2==1.0.8)\n",
|
| 75 |
+
" Cloning https://github.com/facebookresearch/detectron2.git to /tmp/pip-install-3kcq93pe/detectron2_814ab9c3956248cf876dae13021793d0\n",
|
| 76 |
+
" Running command git clone --filter=blob:none --quiet https://github.com/facebookresearch/detectron2.git /tmp/pip-install-3kcq93pe/detectron2_814ab9c3956248cf876dae13021793d0\n",
|
| 77 |
+
" Resolved https://github.com/facebookresearch/detectron2.git to commit 0df2d73d0013db7de629602c23cc120219b4f2b8\n",
|
| 78 |
+
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
|
| 79 |
+
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (6.0.1)\n",
|
| 80 |
+
"Requirement already satisfied: GDAL>=1.11 in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (3.6.4)\n",
|
| 81 |
+
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (1.25.2)\n",
|
| 82 |
+
"Requirement already satisfied: rtree in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (1.2.0)\n",
|
| 83 |
+
"Requirement already satisfied: proj in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (0.2.0)\n",
|
| 84 |
+
"Requirement already satisfied: geos in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (0.2.3)\n",
|
| 85 |
+
"Requirement already satisfied: pypng in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (0.20220715.0)\n",
|
| 86 |
+
"Requirement already satisfied: pygeos in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (0.14)\n",
|
| 87 |
+
"Requirement already satisfied: shapely in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (2.0.4)\n",
|
| 88 |
+
"Requirement already satisfied: geopandas in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (0.13.2)\n",
|
| 89 |
+
"Requirement already satisfied: rasterio==1.3a3 in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (1.3a3)\n",
|
| 90 |
+
"Requirement already satisfied: fiona in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (1.9.6)\n",
|
| 91 |
+
"Requirement already satisfied: pycrs in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (1.0.2)\n",
|
| 92 |
+
"Requirement already satisfied: descartes in /usr/local/lib/python3.10/dist-packages (from detectree2==1.0.8) (1.1.0)\n",
|
| 93 |
+
"Requirement already satisfied: affine in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (2.4.0)\n",
|
| 94 |
+
"Requirement already satisfied: attrs in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (23.2.0)\n",
|
| 95 |
+
"Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (2024.2.2)\n",
|
| 96 |
+
"Requirement already satisfied: click>=4.0 in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (8.1.7)\n",
|
| 97 |
+
"Requirement already satisfied: cligj>=0.5 in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (0.7.2)\n",
|
| 98 |
+
"Requirement already satisfied: snuggs>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (1.4.7)\n",
|
| 99 |
+
"Requirement already satisfied: click-plugins in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (1.1.1)\n",
|
| 100 |
+
"Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from rasterio==1.3a3->detectree2==1.0.8) (67.7.2)\n",
|
| 101 |
+
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from descartes->detectree2==1.0.8) (3.7.1)\n",
|
| 102 |
+
"Requirement already satisfied: Pillow>=7.1 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (9.4.0)\n",
|
| 103 |
+
"Requirement already satisfied: pycocotools>=2.0.2 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.0.7)\n",
|
| 104 |
+
"Requirement already satisfied: termcolor>=1.1 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.4.0)\n",
|
| 105 |
+
"Requirement already satisfied: yacs>=0.1.8 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.1.8)\n",
|
| 106 |
+
"Requirement already satisfied: tabulate in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.9.0)\n",
|
| 107 |
+
"Requirement already satisfied: cloudpickle in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.2.1)\n",
|
| 108 |
+
"Requirement already satisfied: tqdm>4.29.0 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (4.66.4)\n",
|
| 109 |
+
"Requirement already satisfied: tensorboard in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.15.2)\n",
|
| 110 |
+
"Requirement already satisfied: fvcore<0.1.6,>=0.1.5 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.1.5.post20221221)\n",
|
| 111 |
+
"Requirement already satisfied: iopath<0.1.10,>=0.1.7 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.1.9)\n",
|
| 112 |
+
"Requirement already satisfied: omegaconf<2.4,>=2.1 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.3.0)\n",
|
| 113 |
+
"Requirement already satisfied: hydra-core>=1.1 in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (1.3.2)\n",
|
| 114 |
+
"Requirement already satisfied: black in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (24.4.2)\n",
|
| 115 |
+
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (24.0)\n",
|
| 116 |
+
"Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from fiona->detectree2==1.0.8) (1.16.0)\n",
|
| 117 |
+
"Requirement already satisfied: pandas>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from geopandas->detectree2==1.0.8) (2.0.3)\n",
|
| 118 |
+
"Requirement already satisfied: pyproj>=3.0.1 in /usr/local/lib/python3.10/dist-packages (from geopandas->detectree2==1.0.8) (3.6.1)\n",
|
| 119 |
+
"Requirement already satisfied: flask in /usr/local/lib/python3.10/dist-packages (from geos->detectree2==1.0.8) (2.2.5)\n",
|
| 120 |
+
"Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from geos->detectree2==1.0.8) (4.9.4)\n",
|
| 121 |
+
"Requirement already satisfied: arrow>=0.15.6 in /usr/local/lib/python3.10/dist-packages (from proj->detectree2==1.0.8) (1.3.0)\n",
|
| 122 |
+
"Requirement already satisfied: python-dateutil>=2.7.0 in /usr/local/lib/python3.10/dist-packages (from arrow>=0.15.6->proj->detectree2==1.0.8) (2.8.2)\n",
|
| 123 |
+
"Requirement already satisfied: types-python-dateutil>=2.8.10 in /usr/local/lib/python3.10/dist-packages (from arrow>=0.15.6->proj->detectree2==1.0.8) (2.9.0.20240316)\n",
|
| 124 |
+
"Requirement already satisfied: antlr4-python3-runtime==4.9.* in /usr/local/lib/python3.10/dist-packages (from hydra-core>=1.1->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (4.9.3)\n",
|
| 125 |
+
"Requirement already satisfied: portalocker in /usr/local/lib/python3.10/dist-packages (from iopath<0.1.10,>=0.1.7->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.8.2)\n",
|
| 126 |
+
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.1.0->geopandas->detectree2==1.0.8) (2023.4)\n",
|
| 127 |
+
"Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.1.0->geopandas->detectree2==1.0.8) (2024.1)\n",
|
| 128 |
+
"Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->descartes->detectree2==1.0.8) (1.2.1)\n",
|
| 129 |
+
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->descartes->detectree2==1.0.8) (0.12.1)\n",
|
| 130 |
+
"Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->descartes->detectree2==1.0.8) (4.51.0)\n",
|
| 131 |
+
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->descartes->detectree2==1.0.8) (1.4.5)\n",
|
| 132 |
+
"Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->descartes->detectree2==1.0.8) (3.1.2)\n",
|
| 133 |
+
"Requirement already satisfied: mypy-extensions>=0.4.3 in /usr/local/lib/python3.10/dist-packages (from black->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (1.0.0)\n",
|
| 134 |
+
"Requirement already satisfied: pathspec>=0.9.0 in /usr/local/lib/python3.10/dist-packages (from black->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.12.1)\n",
|
| 135 |
+
"Requirement already satisfied: platformdirs>=2 in /usr/local/lib/python3.10/dist-packages (from black->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (4.2.2)\n",
|
| 136 |
+
"Requirement already satisfied: tomli>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from black->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.0.1)\n",
|
| 137 |
+
"Requirement already satisfied: typing-extensions>=4.0.1 in /usr/local/lib/python3.10/dist-packages (from black->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (4.11.0)\n",
|
| 138 |
+
"Requirement already satisfied: Werkzeug>=2.2.2 in /usr/local/lib/python3.10/dist-packages (from flask->geos->detectree2==1.0.8) (3.0.3)\n",
|
| 139 |
+
"Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.10/dist-packages (from flask->geos->detectree2==1.0.8) (3.1.4)\n",
|
| 140 |
+
"Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.10/dist-packages (from flask->geos->detectree2==1.0.8) (2.2.0)\n",
|
| 141 |
+
"Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (1.4.0)\n",
|
| 142 |
+
"Requirement already satisfied: grpcio>=1.48.2 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (1.64.0)\n",
|
| 143 |
+
"Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.27.0)\n",
|
| 144 |
+
"Requirement already satisfied: google-auth-oauthlib<2,>=0.5 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (1.2.0)\n",
|
| 145 |
+
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (3.6)\n",
|
| 146 |
+
"Requirement already satisfied: protobuf!=4.24.0,>=3.19.6 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (3.20.3)\n",
|
| 147 |
+
"Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.31.0)\n",
|
| 148 |
+
"Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.7.2)\n",
|
| 149 |
+
"Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (5.3.3)\n",
|
| 150 |
+
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.4.0)\n",
|
| 151 |
+
"Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (4.9)\n",
|
| 152 |
+
"Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib<2,>=0.5->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (1.3.1)\n",
|
| 153 |
+
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from Jinja2>=3.0->flask->geos->detectree2==1.0.8) (2.1.5)\n",
|
| 154 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (3.3.2)\n",
|
| 155 |
+
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (3.7)\n",
|
| 156 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (2.0.7)\n",
|
| 157 |
+
"Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (0.6.0)\n",
|
| 158 |
+
"Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<2,>=0.5->tensorboard->detectron2@ git+https://github.com/facebookresearch/detectron2.git->detectree2==1.0.8) (3.2.2)\n"
|
| 159 |
+
]
|
| 160 |
+
}
|
| 161 |
+
],
|
| 162 |
+
"source": [
|
| 163 |
+
"from google.colab import drive\n",
|
| 164 |
+
"# drive.mount('/content/drive')\n",
|
| 165 |
+
"!pip install git+https://github.com/PatBall1/detectree2.git"
|
| 166 |
+
]
|
| 167 |
+
},
|
| 168 |
+
{
|
| 169 |
+
"cell_type": "markdown",
|
| 170 |
+
"metadata": {
|
| 171 |
+
"id": "cZfxTuOxnXKQ"
|
| 172 |
+
},
|
| 173 |
+
"source": [
|
| 174 |
+
"## Tiling\n",
|
| 175 |
+
"Tile up the entire orthomosaic to make predictions"
|
| 176 |
+
]
|
| 177 |
+
},
|
| 178 |
+
{
|
| 179 |
+
"cell_type": "markdown",
|
| 180 |
+
"metadata": {
|
| 181 |
+
"id": "hvKTOryJQR0v"
|
| 182 |
+
},
|
| 183 |
+
"source": [
|
| 184 |
+
"## Create full site predictions\n",
|
| 185 |
+
"\n",
|
| 186 |
+
"### Access pre-trained model"
|
| 187 |
+
]
|
| 188 |
+
},
|
| 189 |
+
{
|
| 190 |
+
"cell_type": "markdown",
|
| 191 |
+
"source": [
|
| 192 |
+
"Download pre-trained model"
|
| 193 |
+
],
|
| 194 |
+
"metadata": {
|
| 195 |
+
"id": "4CnWRxJB9eA3"
|
| 196 |
+
}
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"cell_type": "markdown",
|
| 200 |
+
"source": [
|
| 201 |
+
"There might be a cleaner way to do it with `pkg_resources` but I haven't figured this out yet"
|
| 202 |
+
],
|
| 203 |
+
"metadata": {
|
| 204 |
+
"id": "cpHMH39aELgb"
|
| 205 |
+
}
|
| 206 |
+
},
|
| 207 |
+
{
|
| 208 |
+
"cell_type": "markdown",
|
| 209 |
+
"metadata": {
|
| 210 |
+
"id": "JeT66YPmreRv"
|
| 211 |
+
},
|
| 212 |
+
"source": [
|
| 213 |
+
"Combine predictions into full output"
|
| 214 |
+
]
|
| 215 |
+
},
|
| 216 |
+
{
|
| 217 |
+
"cell_type": "code",
|
| 218 |
+
"source": [
|
| 219 |
+
"import cv2\n",
|
| 220 |
+
"from PIL import Image\n",
|
| 221 |
+
"import os\n",
|
| 222 |
+
"import numpy as np\n",
|
| 223 |
+
"import urllib.request\n",
|
| 224 |
+
"import glob\n",
|
| 225 |
+
"\n",
|
| 226 |
+
"# intake library and plugin\n",
|
| 227 |
+
"# import intake\n",
|
| 228 |
+
"# from intake_zenodo_fetcher import download_zenodo_files_for_entry\n",
|
| 229 |
+
"\n",
|
| 230 |
+
"# geospatial libraries\n",
|
| 231 |
+
"import geopandas as gpd\n",
|
| 232 |
+
"\n",
|
| 233 |
+
"from rasterio.transform import from_origin\n",
|
| 234 |
+
"import rasterio.features\n",
|
| 235 |
+
"\n",
|
| 236 |
+
"import fiona\n",
|
| 237 |
+
"\n",
|
| 238 |
+
"from shapely.geometry import shape, mapping, box\n",
|
| 239 |
+
"from shapely.geometry.multipolygon import MultiPolygon\n",
|
| 240 |
+
"\n",
|
| 241 |
+
"# machine learning libraries\n",
|
| 242 |
+
"from detectron2 import model_zoo\n",
|
| 243 |
+
"from detectron2.engine import DefaultPredictor\n",
|
| 244 |
+
"from detectron2.utils.visualizer import Visualizer, ColorMode\n",
|
| 245 |
+
"from detectron2.config import get_cfg\n",
|
| 246 |
+
"from detectron2.engine import DefaultTrainer\n",
|
| 247 |
+
"\n",
|
| 248 |
+
"# visualisation\n",
|
| 249 |
+
"import holoviews as hv\n",
|
| 250 |
+
"from IPython.display import display\n",
|
| 251 |
+
"# import geoviews.tile_sources as gts\n",
|
| 252 |
+
"\n",
|
| 253 |
+
"# import hvplot.pandas\n",
|
| 254 |
+
"# import hvplot.xarray\n",
|
| 255 |
+
"\n",
|
| 256 |
+
"# hv.extension('bokeh', width=100)"
|
| 257 |
+
],
|
| 258 |
+
"metadata": {
|
| 259 |
+
"id": "lftCpCayrPR5"
|
| 260 |
+
},
|
| 261 |
+
"execution_count": null,
|
| 262 |
+
"outputs": []
|
| 263 |
+
},
|
| 264 |
+
{
|
| 265 |
+
"cell_type": "code",
|
| 266 |
+
"source": [
|
| 267 |
+
"%matplotlib inline"
|
| 268 |
+
],
|
| 269 |
+
"metadata": {
|
| 270 |
+
"id": "fpHuk_s3uAXz"
|
| 271 |
+
},
|
| 272 |
+
"execution_count": null,
|
| 273 |
+
"outputs": []
|
| 274 |
+
},
|
| 275 |
+
{
|
| 276 |
+
"cell_type": "code",
|
| 277 |
+
"source": [
|
| 278 |
+
"filepath = '/content/DJI_20240504221329_0168_D.JPG'\n",
|
| 279 |
+
"im = cv2.imread(filepath)\n",
|
| 280 |
+
"display(Image.fromarray(im))"
|
| 281 |
+
],
|
| 282 |
+
"metadata": {
|
| 283 |
+
"id": "g0YQ1U2trbI5",
|
| 284 |
+
"colab": {
|
| 285 |
+
"base_uri": "https://localhost:8080/",
|
| 286 |
+
"height": 297
|
| 287 |
+
},
|
| 288 |
+
"outputId": "c766edda-6deb-4eae-e5bf-8c55104316f1"
|
| 289 |
+
},
|
| 290 |
+
"execution_count": null,
|
| 291 |
+
"outputs": [
|
| 292 |
+
{
|
| 293 |
+
"output_type": "error",
|
| 294 |
+
"ename": "AttributeError",
|
| 295 |
+
"evalue": "'NoneType' object has no attribute '__array_interface__'",
|
| 296 |
+
"traceback": [
|
| 297 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 298 |
+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
| 299 |
+
"\u001b[0;32m<ipython-input-11-0d695c62c78d>\u001b[0m in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mfilepath\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'/content/DJI_20240504221329_0168_D.JPG'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mim\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mdisplay\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mImage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfromarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
| 300 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/PIL/Image.py\u001b[0m in \u001b[0;36mfromarray\u001b[0;34m(obj, mode)\u001b[0m\n\u001b[1;32m 3067\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0;34m.\u001b[0m \u001b[0mversionadded\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1.1\u001b[0m\u001b[0;36m.6\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3068\u001b[0m \"\"\"\n\u001b[0;32m-> 3069\u001b[0;31m \u001b[0marr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__array_interface__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3070\u001b[0m \u001b[0mshape\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"shape\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3071\u001b[0m \u001b[0mndim\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 301 |
+
"\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute '__array_interface__'"
|
| 302 |
+
]
|
| 303 |
+
}
|
| 304 |
+
]
|
| 305 |
+
},
|
| 306 |
+
{
|
| 307 |
+
"cell_type": "code",
|
| 308 |
+
"source": [
|
| 309 |
+
"import matplotlib.pyplot as plt\n"
|
| 310 |
+
],
|
| 311 |
+
"metadata": {
|
| 312 |
+
"id": "D8YtfLmluG3e"
|
| 313 |
+
},
|
| 314 |
+
"execution_count": null,
|
| 315 |
+
"outputs": []
|
| 316 |
+
},
|
| 317 |
+
{
|
| 318 |
+
"cell_type": "code",
|
| 319 |
+
"source": [
|
| 320 |
+
"plt.imshow(im)"
|
| 321 |
+
],
|
| 322 |
+
"metadata": {
|
| 323 |
+
"id": "fqHOtxxvuDnl"
|
| 324 |
+
},
|
| 325 |
+
"execution_count": null,
|
| 326 |
+
"outputs": []
|
| 327 |
+
},
|
| 328 |
+
{
|
| 329 |
+
"cell_type": "code",
|
| 330 |
+
"source": [
|
| 331 |
+
"# Define the project main folder\n",
|
| 332 |
+
"data_folder = './forest-modelling-detectree'\n",
|
| 333 |
+
"\n",
|
| 334 |
+
"# Set the folder structure\n",
|
| 335 |
+
"config = {\n",
|
| 336 |
+
" 'in_geotiff': os.path.join(data_folder, 'input','tiff'),\n",
|
| 337 |
+
" 'in_png': os.path.join(data_folder, 'input','png'),\n",
|
| 338 |
+
" 'model': os.path.join(data_folder, 'model'),\n",
|
| 339 |
+
" 'out_geotiff': os.path.join(data_folder, 'output','raster'),\n",
|
| 340 |
+
" 'out_shapefile': os.path.join(data_folder, 'output','vector'),\n",
|
| 341 |
+
"}\n",
|
| 342 |
+
"\n",
|
| 343 |
+
"# List comprehension for the folder structure code\n",
|
| 344 |
+
"[os.makedirs(val) for key, val in config.items() if not os.path.exists(val)]"
|
| 345 |
+
],
|
| 346 |
+
"metadata": {
|
| 347 |
+
"id": "_cPnLA53sNWi"
|
| 348 |
+
},
|
| 349 |
+
"execution_count": null,
|
| 350 |
+
"outputs": []
|
| 351 |
+
},
|
| 352 |
+
{
|
| 353 |
+
"cell_type": "code",
|
| 354 |
+
"source": [
|
| 355 |
+
"# define the URL to retrieve the model\n",
|
| 356 |
+
"fn = 'model_final.pth'\n",
|
| 357 |
+
"url = f'https://zenodo.org/record/5515408/files/{fn}?download=1'\n",
|
| 358 |
+
"\n",
|
| 359 |
+
"urllib.request.urlretrieve(url, config['model'] + '/' + fn)"
|
| 360 |
+
],
|
| 361 |
+
"metadata": {
|
| 362 |
+
"id": "SazYGXJWr6rW"
|
| 363 |
+
},
|
| 364 |
+
"execution_count": null,
|
| 365 |
+
"outputs": []
|
| 366 |
+
},
|
| 367 |
+
{
|
| 368 |
+
"cell_type": "code",
|
| 369 |
+
"source": [],
|
| 370 |
+
"metadata": {
|
| 371 |
+
"id": "Yu6ixAMoObsv"
|
| 372 |
+
},
|
| 373 |
+
"execution_count": null,
|
| 374 |
+
"outputs": []
|
| 375 |
+
},
|
| 376 |
+
{
|
| 377 |
+
"cell_type": "code",
|
| 378 |
+
"source": [
|
| 379 |
+
"cfg = get_cfg()\n",
|
| 380 |
+
"\n",
|
| 381 |
+
"# if you want to make predictions using a CPU, run the following line. If using GPU, hash it out.\n",
|
| 382 |
+
"cfg.MODEL.DEVICE='cuda'\n",
|
| 383 |
+
"\n",
|
| 384 |
+
"# model and hyperparameter selection\n",
|
| 385 |
+
"cfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml\"))\n",
|
| 386 |
+
"cfg.DATALOADER.NUM_WORKERS = 2\n",
|
| 387 |
+
"cfg.SOLVER.IMS_PER_BATCH = 2\n",
|
| 388 |
+
"cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1\n",
|
| 389 |
+
"\n",
|
| 390 |
+
"### path to the saved pre-trained model weights\n",
|
| 391 |
+
"cfg.MODEL.WEIGHTS = config['model'] + '/model_final.pth'\n",
|
| 392 |
+
"\n",
|
| 393 |
+
"# set confidence threshold at which we predict\n",
|
| 394 |
+
"cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.15\n",
|
| 395 |
+
"\n",
|
| 396 |
+
"#### Settings for predictions using detectron config\n",
|
| 397 |
+
"\n",
|
| 398 |
+
"predictor = DefaultPredictor(cfg)"
|
| 399 |
+
],
|
| 400 |
+
"metadata": {
|
| 401 |
+
"id": "ibJGEKdmsZmq"
|
| 402 |
+
},
|
| 403 |
+
"execution_count": null,
|
| 404 |
+
"outputs": []
|
| 405 |
+
},
|
| 406 |
+
{
|
| 407 |
+
"cell_type": "code",
|
| 408 |
+
"source": [
|
| 409 |
+
"outputs = predictor(im)\n",
|
| 410 |
+
"v = Visualizer(im[:, :, ::-1], scale=1.5, instance_mode=ColorMode.IMAGE_BW) # remove the colors of unsegmented pixels\n",
|
| 411 |
+
"v = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\n",
|
| 412 |
+
"image = cv2.cvtColor(v.get_image()[:, :, :], cv2.COLOR_BGR2RGB)\n",
|
| 413 |
+
"display(Image.fromarray(image))"
|
| 414 |
+
],
|
| 415 |
+
"metadata": {
|
| 416 |
+
"id": "zH-Okks_sVBF"
|
| 417 |
+
},
|
| 418 |
+
"execution_count": null,
|
| 419 |
+
"outputs": []
|
| 420 |
+
},
|
| 421 |
+
{
|
| 422 |
+
"cell_type": "code",
|
| 423 |
+
"source": [
|
| 424 |
+
"\n",
|
| 425 |
+
"plt.imshow(image)"
|
| 426 |
+
],
|
| 427 |
+
"metadata": {
|
| 428 |
+
"id": "ZH3jAypEsqdG"
|
| 429 |
+
},
|
| 430 |
+
"execution_count": null,
|
| 431 |
+
"outputs": []
|
| 432 |
+
},
|
| 433 |
+
{
|
| 434 |
+
"cell_type": "code",
|
| 435 |
+
"source": [
|
| 436 |
+
"mask_array = outputs['instances'].pred_masks.cpu().numpy()\n",
|
| 437 |
+
"\n",
|
| 438 |
+
"# get confidence scores too\n",
|
| 439 |
+
"mask_array_scores = outputs['instances'].scores.cpu().numpy()"
|
| 440 |
+
],
|
| 441 |
+
"metadata": {
|
| 442 |
+
"id": "ewUypuTCSYSL"
|
| 443 |
+
},
|
| 444 |
+
"execution_count": null,
|
| 445 |
+
"outputs": []
|
| 446 |
+
},
|
| 447 |
+
{
|
| 448 |
+
"cell_type": "code",
|
| 449 |
+
"source": [
|
| 450 |
+
"\n",
|
| 451 |
+
"num_instances = mask_array.shape[0]\n",
|
| 452 |
+
"mask_array_instance = []\n",
|
| 453 |
+
"output = np.zeros_like(mask_array)\n",
|
| 454 |
+
"\n",
|
| 455 |
+
"mask_array_instance.append(mask_array)\n",
|
| 456 |
+
"output = np.where(mask_array_instance[0] == True, 255, output)\n",
|
| 457 |
+
"fresh_output = output.astype(np.float)\n",
|
| 458 |
+
"x_scaling = 140/fresh_output.shape[1]\n",
|
| 459 |
+
"y_scaling = 140/fresh_output.shape[2]\n",
|
| 460 |
+
"# this is an affine transform. This needs to be altered significantly.\n",
|
| 461 |
+
"transform = from_origin(int(filepath[-17:-11])-20, int(filepath[-10:-4])+120, y_scaling, x_scaling)"
|
| 462 |
+
],
|
| 463 |
+
"metadata": {
|
| 464 |
+
"id": "i_25LDd9ShHj"
|
| 465 |
+
},
|
| 466 |
+
"execution_count": null,
|
| 467 |
+
"outputs": []
|
| 468 |
+
},
|
| 469 |
+
{
|
| 470 |
+
"cell_type": "code",
|
| 471 |
+
"source": [
|
| 472 |
+
"mask_array = outputs['instances'].pred_masks.numpy()\n",
|
| 473 |
+
"\n",
|
| 474 |
+
"# get confidence scores too\n",
|
| 475 |
+
"mask_array_scores = outputs['instances'].scores.numpy()\n",
|
| 476 |
+
"\n",
|
| 477 |
+
"num_instances = mask_array.shape[0]\n",
|
| 478 |
+
"mask_array_instance = []\n",
|
| 479 |
+
"output = np.zeros_like(mask_array)\n",
|
| 480 |
+
"\n",
|
| 481 |
+
"mask_array_instance.append(mask_array)\n",
|
| 482 |
+
"output = np.where(mask_array_instance[0] == True, 255, output)\n",
|
| 483 |
+
"fresh_output = output.astype(np.float)\n",
|
| 484 |
+
"x_scaling = 140/fresh_output.shape[1]\n",
|
| 485 |
+
"y_scaling = 140/fresh_output.shape[2]\n",
|
| 486 |
+
"# this is an affine transform. This needs to be altered significantly.\n",
|
| 487 |
+
"transform = from_origin(int(filepath[-17:-11])-20, int(filepath[-10:-4])+120, y_scaling, x_scaling)\n",
|
| 488 |
+
"\n",
|
| 489 |
+
"output_raster = config['out_geotiff'] + '/' + 'predicted_rasters_' + filepath[-17:-4]+ '.tif'\n",
|
| 490 |
+
"\n",
|
| 491 |
+
"new_dataset = rasterio.open(output_raster, 'w', driver='GTiff',\n",
|
| 492 |
+
" height = fresh_output.shape[1], width = fresh_output.shape[2], count = fresh_output.shape[0],\n",
|
| 493 |
+
" dtype=str(fresh_output.dtype),\n",
|
| 494 |
+
" crs='+proj=utm +zone=50 +datum=WGS84 +units=m +no_defs',\n",
|
| 495 |
+
" transform=transform)\n",
|
| 496 |
+
"\n",
|
| 497 |
+
"new_dataset.write(fresh_output)\n",
|
| 498 |
+
"new_dataset.close()"
|
| 499 |
+
],
|
| 500 |
+
"metadata": {
|
| 501 |
+
"id": "juM3wDRLvZ4m"
|
| 502 |
+
},
|
| 503 |
+
"execution_count": null,
|
| 504 |
+
"outputs": []
|
| 505 |
+
},
|
| 506 |
+
{
|
| 507 |
+
"cell_type": "code",
|
| 508 |
+
"source": [
|
| 509 |
+
"print(outputs)"
|
| 510 |
+
],
|
| 511 |
+
"metadata": {
|
| 512 |
+
"id": "u0hdjykMSNX8"
|
| 513 |
+
},
|
| 514 |
+
"execution_count": null,
|
| 515 |
+
"outputs": []
|
| 516 |
+
},
|
| 517 |
+
{
|
| 518 |
+
"cell_type": "code",
|
| 519 |
+
"source": [
|
| 520 |
+
"mask_array = outputs['instances'].pred_masks.cpu.numpy()\n",
|
| 521 |
+
"\n",
|
| 522 |
+
"# get confidence scores too\n",
|
| 523 |
+
"mask_array_scores = outputs['instances'].scores.cpu.numpy()\n",
|
| 524 |
+
"\n",
|
| 525 |
+
"num_instances = mask_array.shape[0]\n",
|
| 526 |
+
"mask_array_instance = []\n",
|
| 527 |
+
"output = np.zeros_like(mask_array)\n",
|
| 528 |
+
"\n",
|
| 529 |
+
"mask_array_instance.append(mask_array)\n",
|
| 530 |
+
"output = np.where(mask_array_instance[0] == True, 255, output)\n",
|
| 531 |
+
"fresh_output = output.astype(np.float)"
|
| 532 |
+
],
|
| 533 |
+
"metadata": {
|
| 534 |
+
"id": "Jg0r8IgQD_W8"
|
| 535 |
+
},
|
| 536 |
+
"execution_count": null,
|
| 537 |
+
"outputs": []
|
| 538 |
+
},
|
| 539 |
+
{
|
| 540 |
+
"cell_type": "code",
|
| 541 |
+
"source": [
|
| 542 |
+
"# # Read input band with Rasterio\n",
|
| 543 |
+
"\n",
|
| 544 |
+
"# with rasterio.open(output_raster) as src:\n",
|
| 545 |
+
"# shp_schema = {'geometry': 'MultiPolygon','properties': {'pixelvalue': 'int', 'score': 'float'}}\n",
|
| 546 |
+
"\n",
|
| 547 |
+
"# crs = src.crs\n",
|
| 548 |
+
"# for i in range(src.count):\n",
|
| 549 |
+
"# src_band = src.read(i+1)\n",
|
| 550 |
+
"# src_band = np.float32(src_band)\n",
|
| 551 |
+
"# conf = mask_array_scores[i]\n",
|
| 552 |
+
"# # Keep track of unique pixel values in the input band\n",
|
| 553 |
+
"# unique_values = np.unique(src_band)\n",
|
| 554 |
+
"# # Polygonize with Rasterio. `shapes()` returns an iterable\n",
|
| 555 |
+
"# # of (geom, value) as tuples\n",
|
| 556 |
+
"# shapes = list(rasterio.features.shapes(src_band, transform=src.transform))\n",
|
| 557 |
+
"\n",
|
| 558 |
+
"# if i == 0:\n",
|
| 559 |
+
"# with fiona.open(config['out_shapefile'] + '/predicted_polygons_' + filepath[-17:-4] + '_' + str(0) + '.shp', 'w', 'ESRI Shapefile',\n",
|
| 560 |
+
"# shp_schema) as shp:\n",
|
| 561 |
+
"# polygons = [shape(geom) for geom, value in shapes if value == 255.0]\n",
|
| 562 |
+
"# multipolygon = MultiPolygon(polygons)\n",
|
| 563 |
+
"# # simplify not needed here\n",
|
| 564 |
+
"# #multipolygon = multipolygon_a.simplify(0.1, preserve_topology=False)\n",
|
| 565 |
+
"# shp.write({\n",
|
| 566 |
+
"# 'geometry': mapping(multipolygon),\n",
|
| 567 |
+
"# 'properties': {'pixelvalue': int(unique_values[1]), 'score': float(conf)}\n",
|
| 568 |
+
"# })\n",
|
| 569 |
+
"# else:\n",
|
| 570 |
+
"# with fiona.open(config['out_shapefile'] + '/predicted_polygons_' + filepath[-17:-4] + '_' + str(0)+'.shp', 'a', 'ESRI Shapefile',\n",
|
| 571 |
+
"# shp_schema) as shp:\n",
|
| 572 |
+
"# polygons = [shape(geom) for geom, value in shapes if value == 255.0]\n",
|
| 573 |
+
"# multipolygon = MultiPolygon(polygons)\n",
|
| 574 |
+
"# # simplify not needed here\n",
|
| 575 |
+
"# #multipolygon = multipolygon_a.simplify(0.1, preserve_topology=False)\n",
|
| 576 |
+
"# shp.write({\n",
|
| 577 |
+
"# 'geometry': mapping(multipolygon),\n",
|
| 578 |
+
"# 'properties': {'pixelvalue': int(unique_values[1]), 'score': float(conf)}\n",
|
| 579 |
+
"# })"
|
| 580 |
+
],
|
| 581 |
+
"metadata": {
|
| 582 |
+
"id": "-Enr0Hg7vbtr"
|
| 583 |
+
},
|
| 584 |
+
"execution_count": null,
|
| 585 |
+
"outputs": []
|
| 586 |
+
},
|
| 587 |
+
{
|
| 588 |
+
"cell_type": "code",
|
| 589 |
+
"source": [
|
| 590 |
+
"# load and plot polygons\n",
|
| 591 |
+
"in_shp = glob.glob(config['out_shapefile'] + '/*.shp')\n",
|
| 592 |
+
"\n",
|
| 593 |
+
"poly_df = gpd.read_file(in_shp[0])\n",
|
| 594 |
+
"\n",
|
| 595 |
+
"plot_vector = poly_df.hvplot(hover_cols=['score'], legend=False).opts(fill_color=None,line_color=None,alpha=0.5, width=800, height=600)\n",
|
| 596 |
+
"\n",
|
| 597 |
+
"plot_vector"
|
| 598 |
+
],
|
| 599 |
+
"metadata": {
|
| 600 |
+
"id": "DWve5Kc8vI5s"
|
| 601 |
+
},
|
| 602 |
+
"execution_count": null,
|
| 603 |
+
"outputs": []
|
| 604 |
+
},
|
| 605 |
+
{
|
| 606 |
+
"cell_type": "markdown",
|
| 607 |
+
"metadata": {
|
| 608 |
+
"id": "KW3j29mJGjO5"
|
| 609 |
+
},
|
| 610 |
+
"source": [
|
| 611 |
+
"Remove invalid geometries before cleaning. Might be a way to fix them rather than remove all together but I think the ones with invalid geometries tend to be bad crowns anyway."
|
| 612 |
+
]
|
| 613 |
+
},
|
| 614 |
+
{
|
| 615 |
+
"cell_type": "markdown",
|
| 616 |
+
"metadata": {
|
| 617 |
+
"id": "zF0qy7vpGmem"
|
| 618 |
+
},
|
| 619 |
+
"source": [
|
| 620 |
+
"Clean overlapping crowns. For significant overlaps, the most confident crown is selected. IoU can be varied based on how strict you want the filtering to be."
|
| 621 |
+
]
|
| 622 |
+
}
|
| 623 |
+
],
|
| 624 |
+
"metadata": {
|
| 625 |
+
"accelerator": "GPU",
|
| 626 |
+
"colab": {
|
| 627 |
+
"provenance": [],
|
| 628 |
+
"gpuType": "T4"
|
| 629 |
+
},
|
| 630 |
+
"kernelspec": {
|
| 631 |
+
"display_name": "Python 3",
|
| 632 |
+
"name": "python3"
|
| 633 |
+
},
|
| 634 |
+
"language_info": {
|
| 635 |
+
"name": "python"
|
| 636 |
+
}
|
| 637 |
+
},
|
| 638 |
+
"nbformat": 4,
|
| 639 |
+
"nbformat_minor": 0
|
| 640 |
+
}
|
test_json.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import pandas
|
| 3 |
+
|
| 4 |
+
with open(r'E:\Backup_K20pro\Download\treesat_benchmark\coco-panoptic\export_coco-panoptic_r_drone-panoramic_drone-panoramic.v01.json') as f:
|
| 5 |
+
data = json.load(f)
|
| 6 |
+
|
| 7 |
+
# Extracting general information
|
| 8 |
+
info = data['info']
|
| 9 |
+
categories = data['categories']
|
| 10 |
+
images = data['images']
|
| 11 |
+
annotations = data['annotations']
|
| 12 |
+
|
| 13 |
+
print("Dataset description:", info['description'])
|
| 14 |
+
print("Dataset version:", info['version'])
|
| 15 |
+
print("\nCategories:")
|
| 16 |
+
for category in categories:
|
| 17 |
+
print(f"ID: {category['id']}, Name: {category['name']}, Color: {category['color']}")
|
| 18 |
+
|
| 19 |
+
print("\nImages:")
|
| 20 |
+
for image in images:
|
| 21 |
+
print(f"ID: {image['id']}, File Name: {image['file_name']}, Width: {image['width']}, Height: {image['height']}")
|
| 22 |
+
|
| 23 |
+
print("\nAnnotations:")
|
| 24 |
+
for annotation in annotations:
|
| 25 |
+
print(f"Image ID: {annotation['image_id']}, File Name: {annotation['file_name']}")
|
| 26 |
+
for segment_info in annotation['segments_info']:
|
| 27 |
+
print(f" Segment ID: {segment_info['id']}, Category ID: {segment_info['category_id']}, BBox: {segment_info['bbox']}, Area: {segment_info['area']}")
|
| 28 |
+
|
| 29 |
+
def get_images_by_category(category_name, data):
|
| 30 |
+
category_id = None
|
| 31 |
+
for category in data['categories']:
|
| 32 |
+
if category['name'] == category_name:
|
| 33 |
+
category_id = category['id']
|
| 34 |
+
break
|
| 35 |
+
|
| 36 |
+
if category_id is None:
|
| 37 |
+
print(f"Category '{category_name}' not found.")
|
| 38 |
+
return []
|
| 39 |
+
|
| 40 |
+
images_with_category = []
|
| 41 |
+
for annotation in data['annotations']:
|
| 42 |
+
for segment_info in annotation['segments_info']:
|
| 43 |
+
if segment_info['category_id'] == category_id:
|
| 44 |
+
images_with_category.append(annotation['image_id'])
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
return images_with_category
|
| 48 |
+
|
| 49 |
+
# Example usage
|
| 50 |
+
category_name = "Attalea Maripa"
|
| 51 |
+
print("ATTALIA MARIPAAAAAAA")
|
| 52 |
+
image_ids = get_images_by_category(category_name, data)
|
| 53 |
+
print(image_ids)
|
| 54 |
+
|