massimo-me's picture
Update app.py
5844b1e verified
raw
history blame
887 Bytes
from urllib import request
import detectree as dtr
import matplotlib.pyplot as plt
import rasterio as rio
from rasterio import plot
# download a tile from the SWISSIMAGE WMS
tile_url = (
"https://wms.geo.admin.ch/?SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&"
"FORMAT=image/png&LAYERS=ch.swisstopo.images-swissimage&CRS=EPSG:2056"
"&BBOX=2532980,1152150,2533380,1152450&WIDTH=800&HEIGHT=600"
)
tile_filename = "tile.png"
request.urlretrieve(tile_url, tile_filename)
# use the pre-trained model to segment the image into tree/non-tree-pixels
y_pred = dtr.Classifier().predict_img(tile_filename)
# side-by-side plot of the tile and the predicted tree/non-tree pixels
figwidth, figheight = plt.rcParams["figure.figsize"]
fig, axes = plt.subplots(1, 2, figsize=(2 * figwidth, figheight))
with rio.open(tile_filename) as src:
plot.show(src, ax=axes[0])
axes[1].imshow(y_pred)