How do I use a pretrained model to predict an image?#
from deepforest import main, get_data
import matplotlib.pyplot as plt
# Initialize the model
model = main.deepforest()
# Load a pretrained tree detection model from Hugging Face
model.load_model(model_name="weecology/deepforest-tree", revision="main")
# Get the sample image path and predict image
sample_image_path = get_data("OSBS_029.png")
img = model.predict_image(path=sample_image_path, return_plot=True)
# predict_image returns plot in BlueGreenRed (opencv style), but matplotlib likes RedGreenBlue
# Switch the channel order for correct display
plt.imshow(img[:,:,::-1])
plt.show()
Note
Please note that this video was made before the deepforest-pytorch -> deepforest name change.
For single images, predict_image
can read an image from memory or file and return predicted bounding boxes.