Skip to content

Commit

Permalink
Add sweden data
Browse files Browse the repository at this point in the history
  • Loading branch information
bw4sz committed Oct 1, 2024
1 parent df0f676 commit 438d284
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
94 changes: 94 additions & 0 deletions data_prep/Radogoshi_Sweden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import os
import pandas as pd
from deepforest.utilities import read_file
import numpy as np
import xmltodict


def xml_to_annotations(xml_path, image_dir):
"""
Load annotations from xml format (e.g. RectLabel editor) and convert
them into retinanet annotations format.
Args:
xml_path (str): Path to the annotations xml, formatted by RectLabel
image_dir: Directory to search for images
Returns:
Annotations (pandas dataframe): in the
format -> path-to-image.png,x1,y1,x2,y2,class_name
"""
# parse
with open(xml_path) as fd:
doc = xmltodict.parse(fd.read())

# grab xml objects
try:
tile_xml = doc["annotation"]["object"]
except Exception as e:
raise Exception("error {} for path {} with doc annotation{}".format(
e, xml_path, doc["annotation"]))

xmin = []
xmax = []
ymin = []
ymax = []
label = []

if isinstance(tile_xml, list):
# Construct frame if multiple trees
for tree in tile_xml:
xmin.append(tree["bndbox"]["xmin"])
xmax.append(tree["bndbox"]["xmax"])
ymin.append(tree["bndbox"]["ymin"])
ymax.append(tree["bndbox"]["ymax"])
else:
xmin.append(tile_xml["bndbox"]["xmin"])
xmax.append(tile_xml["bndbox"]["xmax"])
ymin.append(tile_xml["bndbox"]["ymin"])
ymax.append(tile_xml["bndbox"]["ymax"])

rgb_name = os.path.basename(doc["annotation"]["filename"])

if os.path.exists("{}/{}".format(image_dir,rgb_name)):
# set dtypes, check for floats and round
xmin = [int(np.round(float(x))) for x in xmin]
xmax = [int(np.round(float(x)))for x in xmax]
ymin = [int(np.round(float(x))) for x in ymin]
ymax = [int(np.round(float(x))) for x in ymax]

annotations = pd.DataFrame({
"image_path": rgb_name,
"xmin": xmin,
"ymin": ymin,
"xmax": xmax,
"ymax": ymax,
})
return (annotations)
else:
raise IOError("{} doesn't exist".format(rgb_name))

def recursive_search_and_process(root_dir):
xml_files = []
for subdir, _, files in os.walk(root_dir):
for file in files:
if file.endswith(".xml"):
xml_files.append(os.path.join(subdir, file))

dataframes = []
for xml_file in xml_files:
try:
df = xml_to_annotations(xml_file, root_dir + "/images")
except Exception as e:
print("Error processing file: {}. Error: {}".format(xml_file, e))
continue
df['image_path'] = os.path.join(root_dir + "/images", os.path.splitext(os.path.basename(xml_file))[0] + '.JPG')
df = read_file(df, root_dir + "/images")
dataframes.append(df)

combined_df = pd.concat(dataframes, ignore_index=True)
combined_df['label'] = 'Tree'
combined_df['source'] = 'Radogoshi et al. 2021'

combined_df.to_csv(os.path.join(root_dir, 'annotations.csv'), index=False)

root_directory = "/orange/ewhite/DeepForest/Radogoshi_Sweden"
recursive_search_and_process(root_directory)
1 change: 1 addition & 0 deletions data_prep/collect_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"/orange/ewhite/DeepForest/Ryoungseob_2023/train_datasets/images/train.csv",
"/orange/ewhite/DeepForest/Velasquez_urban_trees/tree_canopies/nueva_carpeta/annotations.csv",
'/orange/ewhite/DeepForest/individual_urban_tree_crown_detection/annotations.csv',
'/orange/ewhite/DeepForest/Radogoshi_Sweden/annotations.csv',
'/orange/ewhite/DeepForest/ReForestTree/images/train.csv']

TreePoints = [
Expand Down
10 changes: 10 additions & 0 deletions docs/datasets.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ ISPRS Journal of Photogrammetry and Remote Sensing, Volume 206, 2023

**Location:** Suwon, South Korea

### Ragadoshi_Sweden

![sample_image](public/Radogoshi_et_al._2021.png)

**Link:** [https://lila.science/datasets/forest-damages-larch-casebearer/](https://lila.science/datasets/forest-damages-larch-casebearer/)

**Location:** Sweden

### Velasquez-Camacho et al. 2023

![sample_image](public/Velasquez-Camacho_et_al._2023.png)
Expand Down Expand Up @@ -107,6 +115,8 @@ International Journal of Applied Earth Observation and Geoinformation, 130, 1038

### Safonova et al. 2021

![sample_image](public/Safonova_et_al._2021.png)

**Link:** [https://www.mdpi.com/1424-8220/21/5/1617](https://www.mdpi.com/1424-8220/21/5/1617)

**Location:** Spain
Expand Down
Binary file added docs/public/Radogoshi_et_al._2021.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/public/Ragadoshi_2021.png
Binary file not shown.

0 comments on commit 438d284

Please sign in to comment.