<jupyter_start><jupyter_text>Forest: Tree crown detection Context PurposeDetect tree crown using state-of-art models Modelling approachA prebuilt Deep Learning model, named _DeepForest_, is used to predict individual tree crowns from an airborne RGB image. Highlights* _DeepForest_ was trained on data from the National Ecological Observatory Network. Authors<NAME>, The Alan Turing Institute, [@acocac](https://github.com/acocac):::{note}The author acknowledges [DeepForest](https://deepforest.readthedocs.io/en/latest/) contributors. Some code snippets were extracted from DeepForest [GitHub public repository](https://github.com/weecology/DeepForest).:::  Install and load libraries<jupyter_code>!pip -q install git+https://github.com/ESM-VFC/intake_zenodo_fetcher.git ##Intake Zenodo Fetcher
!pip -q install DeepForest
import glob
import os
import urllib

import intake
from intake_zenodo_fetcher import download_zenodo_files_for_entry
import matplotlib.pyplot as plt
import xmltodict
import cv2

import tempfile

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Fetch a RGB image from Zenodo<jupyter_code># create a temp dir
path = tempfile.mkdtemp()

catalog_file = os.path.join(path, 'catalog.yaml')

with open(catalog_file, 'w') as f:
    f.write('''
sources:
  NEONTREE_rgb:
    driver: xarray_image
    description: 'NeonTreeEvaluation RGB images (collection)'
    metadata:
      zenodo_doi: "10.5281/zenodo.3459803"
    args:
      urlpath: "{{ CATALOG_DIR }}/NEONsample_RGB/2018_MLBS_3_541000_4140000_image_crop.tif"
      ''')
cat_tc = intake.open_catalog('tc.yaml')
for catalog_entry in list(cat_tc):
    download_zenodo_files_for_entry(
        cat_tc[catalog_entry],
        force_download=False
    )
tc_rgb = cat_tc["NEONTREE_rgb"].to_dask()
tc_rgb<jupyter_output><empty_output><jupyter_text>Load and prepare labels<jupyter_code>filenames = glob.glob('./NEONsample_RGB/*.tif')
filesn = [os.path.basename(i) for i in filenames]
##Create ordered dictionary of .xml annotation files
def loadxml(imagename):
  imagename = imagename.replace('.tif','')
  fullurl = "https://raw.githubusercontent.com/weecology/NeonTreeEvaluation/master/annotations/" + imagename + ".xml"
  file = urllib.request.urlopen(fullurl)
  data = file.read()
  file.close()
  data = xmltodict.parse(data)
  return data

allxml = [loadxml(i) for i in filesn]

# function to extract bounding boxes
def extractbb(i):
  bb = [f['bndbox'] for f in allxml[i]['annotation']['object']]
  return bb

bball = [extractbb(i) for i in range(0,len(allxml))]
print(len(bball))<jupyter_output><empty_output><jupyter_text>Visualise image and labels<jupyter_code># function to plot images
def cv2_imshow(a, **kwargs):
    a = a.clip(0, 255).astype('uint8')
    # cv2 stores colors as BGR; convert to RGB
    if a.ndim == 3:
        if a.shape[2] == 4:
            a = cv2.cvtColor(a, cv2.COLOR_BGRA2RGBA)
        else:
            a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)

    return plt.imshow(a, **kwargs)
image = tc_rgb
# plot predicted bbox
image2 = image.values.copy()
target_bbox = bball[0]

for row in target_bbox:
    cv2.rectangle(image2, (int(row["xmin"]), int(row["ymin"])), (int(row["xmax"]), int(row["ymax"])), (0, 0, 0), thickness=10, lineType=cv2.LINE_AA)

plt.figure(figsize=(15,15))
cv2_imshow(image2)
plt.show()<jupyter_output><empty_output><jupyter_text>Load *DeepForest* pretrained model<jupyter_code>from deepforest import main
# load deep forest model
model = main.deepforest()
model.use_release()
#boxes = model.predict_image(image=image.values)
#print(boxes)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12138084632516703, 'fraction_numerical': 0.018095768374164812, 'mean_word_length': 5.336860670194003, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '297152', 'license': 'MIT', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1253, 'n_words': 290, 'path': 'book/forest/modelling/forest-modelling-treecrown.ipynb', 'repo_name': 'svadams/environmental-ai-book', 'size': '3607'}