Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated main.deepforest.predict_file for DF usage #852

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/deepforest/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def __init__(self,
Returns:
If train, path, image, targets else image
"""
self.annotations = pd.read_csv(csv_file)
# Check if csv_file is a DataFrame or a file path
if isinstance(csv_file, pd.DataFrame):
self.annotations = csv_file
else:
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
if transforms is None:
self.transform = get_transform(augment=train)
Expand Down
13 changes: 10 additions & 3 deletions src/deepforest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ def predict_image(self,
result = utilities.read_file(result, root_dir=root_dir)

return result


def predict_file(self, csv_file, root_dir, savedir=None, color=None, thickness=1):
"""Create a dataset and predict entire annotation file Csv file format
Expand All @@ -431,7 +432,7 @@ def predict_file(self, csv_file, root_dir, savedir=None, color=None, thickness=1
Deprecation warning: The return_plot argument is deprecated and will be removed in 2.0. Use visualize.plot_results on the result instead.

Args:
csv_file: path to csv file
csv_file (str or pd.DataFrame): Path to a CSV file or a DataFrame with annotations.
root_dir: directory of images. If none, uses "image_dir" in config
(deprecated) savedir: directory to save images with bounding boxes
(deprecated) color: color of the bounding box as a tuple of BGR color, e.g. orange annotations is (0, 165, 255)
Expand All @@ -441,11 +442,17 @@ def predict_file(self, csv_file, root_dir, savedir=None, color=None, thickness=1
df: pandas dataframe with bounding boxes, label and scores for each image in the csv file
"""

df = utilities.read_file(csv_file)
ds = dataset.TreeDataset(csv_file=csv_file,
# Use DataFrame directly if provided, otherwise treat as file path
if isinstance(csv_file, pd.DataFrame):
df = csv_file
else:
df = utilities.read_file(csv_file)

ds = dataset.TreeDataset(csv_file=df,
root_dir=root_dir,
transforms=None,
train=False)

dataloader = self.predict_dataloader(ds)

results = predict._dataloader_wrapper_(model=self,
Expand Down
5 changes: 2 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,7 @@ def test_predict_tile_with_crop_model(m, config):
"xmin", "ymin", "xmax", "ymax", "label", "score", "cropmodel_label", "geometry",
"cropmodel_score", "image_path"
}



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to come up with some test for the added feature functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay. I will write a new test case and commit it

def test_predict_tile_with_crop_model_empty():
"""If the model return is empty, the crop model should return an empty dataframe"""
raster_path = get_data("SOAP_061.png")
Expand All @@ -673,4 +672,4 @@ def test_predict_tile_with_crop_model_empty():
crop_model=crop_model)

# Assert the result
assert result is None
assert result is None
Loading