raj999 commited on
Commit
503f165
1 Parent(s): c6e0287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -51
app.py CHANGED
@@ -1,52 +1,39 @@
1
  import gradio as gr
2
-
3
- def upload_image(image):
4
- return image
5
-
6
- iface = gr.Interface(
7
- fn=upload_image,
8
- inputs=gr.Image(type="pil", image_mode="RGB"),
9
- outputs="image",
10
- title="Image Uploader",
11
- description="Upload an image and see the result below."
12
- )
13
-
14
- # from PIL import Image
15
- # import os
16
- # import numpy as np
17
- # import urllib.request
18
- # import glob
19
-
20
- # # intake library and plugin
21
- # # import intake
22
- # # from intake_zenodo_fetcher import download_zenodo_files_for_entry
23
-
24
- # # geospatial libraries
25
- # import geopandas as gpd
26
-
27
- # from rasterio.transform import from_origin
28
- # import rasterio.features
29
-
30
- # import fiona
31
-
32
- # from shapely.geometry import shape, mapping, box
33
- # from shapely.geometry.multipolygon import MultiPolygon
34
-
35
- # # machine learning libraries
36
- # from detectron2 import model_zoo
37
- # from detectron2.engine import DefaultPredictor
38
- # from detectron2.utils.visualizer import Visualizer, ColorMode
39
- # from detectron2.config import get_cfg
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()