Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
#
|
39 |
-
|
40 |
-
# from detectron2.engine import DefaultTrainer
|
41 |
-
|
42 |
-
# # visualisation
|
43 |
-
# # import holoviews as hv
|
44 |
-
# # from IPython.display import display
|
45 |
-
# # import geoviews.tile_sources as gts
|
46 |
-
|
47 |
-
# # import hvplot.pandas
|
48 |
-
# # import hvplot.xarray
|
49 |
-
|
50 |
-
# hv.extension('bokeh', width=100)
|
51 |
-
|
52 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from deepforest import main
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
|
5 |
+
# Initialize the deepforest model and use the released version
|
6 |
+
model = main.deepforest()
|
7 |
+
model.use_release()
|
8 |
+
|
9 |
+
def predict_and_visualize(image):
|
10 |
+
"""
|
11 |
+
Function to predict and visualize the image using deepforest model.
|
12 |
+
|
13 |
+
Args:
|
14 |
+
- image: An image array.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
- An image with predictions visualized.
|
18 |
+
"""
|
19 |
+
# Predict image and return plot. Since Gradio passes image as array, save it temporarily.
|
20 |
+
temp_path = "/tmp/uploaded_image.png"
|
21 |
+
plt.imsave(temp_path, image)
|
22 |
+
img = model.predict_image(path=temp_path, return_plot=True)
|
23 |
+
|
24 |
+
# Since the output is BGR and matplotlib (and hence Gradio) needs RGB, we convert the color scheme
|
25 |
+
img_rgb = img[:, :, ::-1]
|
26 |
+
|
27 |
+
# Return the RGB image
|
28 |
+
return img_rgb
|
29 |
+
|
30 |
+
# Define the Gradio interface
|
31 |
+
iface = gr.Interface(fn=predict_and_visualize,
|
32 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
33 |
+
outputs=gr.Image(label="Predicted Image"),
|
34 |
+
title="DeepForest Tree Detection",
|
35 |
+
examples=["./example.jpg"],
|
36 |
+
description="Upload an image to detect trees using the DeepForest model.")
|
37 |
+
|
38 |
+
# Launch the Gradio app
|
39 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|