weecology / DeepForest

Python Package for Airborne RGB machine learning
https://deepforest.readthedocs.io/
MIT License
478 stars 172 forks source link

predict_tile doesn't add image_path to boxes df #368

Closed youngdjn closed 10 months ago

youngdjn commented 1 year ago

Describe the bug To save the predicted tree boxes to shapefile using boxes_to_shapefile, the boxes df produced by predict_tile needs to have a column image_path, but it doesn't. I'm using the recent update intended to fix this, but it's still not working. (The updated code looks good to my inexperienced python eye so I'm stumped.) I can manually add the image path with boxes["image_path"] = os.path.basename("/path/to/ortho.tif") and then boxes_to_shapefile works.

To Reproduce I insalled deepforest from github after the PR was merged: pip install git+https://github.com/weecology/DeepForest.git

To use the orthomosaic I have been using, download it from here: https://ucdavis.box.com/s/f6ddi0hzvtd5hq11bgp9l91fyjaydwlf

Then run my full script but change the ortho directory to where you downloaded it to:

from deepforest import main
from deepforest import get_data
from deepforest import utilities
import os
import rasterio as rio
import numpy as  np

r = rio.open("/home/exouser/Downloads/ept-ortho-crop.tif")
df = r.read()

rolled_df = np.rollaxis(df, 0,3)

m = main.deepforest()
m.use_release()

boxes = m.predict_tile(image=rolled_df, patch_size=1500)

# It only works if I also run this:
# boxes["image_path"] = os.path.basename("/home/exouser/Downloads/ept-ortho-crop.tif")

shp = utilities.boxes_to_shapefile(boxes, root_dir="/home/exouser/Downloads", projected=True)
shp.to_file("/home/exouser/Downloads/boxes.gpkg")

Environment (please complete the following information):

$ pip freeze | grep deepforest
deepforest @ git+https://github.com/weecology/DeepForest.git@d2de708bfcb2c170dd166def36988c7870c9a3c1
Om-Doiphode commented 1 year ago

I would like to work on this issue. Can you guide me on how to get started with it?

bw4sz commented 10 months ago

Yes, this additional line of code is needed, but i'm not convinced that's the problem. I think its that boxes_to_shapefile should take in the path to the RGB object and forget about the silly root dir (#512). I think this would be more explicit.

from deepforest import main
from deepforest import get_data
from deepforest import utilities
import os
import rasterio as rio
import numpy as  np

raster_path = get_data("OSBS_029.tif")
r = rio.open(raster_path)
df = r.read()

rolled_df = np.rollaxis(df, 0,3)

m = main.deepforest()
m.use_release()

boxes = m.predict_tile(image=rolled_df, patch_size=1500)

shp = utilities.boxes_to_shapefile(boxes, rgb=raster_path, projected=True)
shp.to_file("/home/exouser/Downloads/boxes.gpkg")

This way the boxes_to_shapefile function isn't making any assumptions about what columns are in 'boxes' for finding the rgb image. Let's close this issue and make a new one focused on that.