File size: 3,885 Bytes
3f834bf
 
 
 
 
373c783
 
 
 
 
 
 
3d9e5f4
373c783
 
3d9e5f4
373c783
 
 
 
 
3d9e5f4
 
373c783
 
3f834bf
3d9e5f4
3f834bf
 
 
373c783
 
 
 
 
 
3d9e5f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373c783
 
3d9e5f4
373c783
 
 
 
 
3d9e5f4
 
373c783
 
3f834bf
3d9e5f4
3f834bf
 
 
 
 
 
 
 
 
 
 
 
 
 
3d9e5f4
3f834bf
 
 
 
 
 
3d9e5f4
3f834bf
 
 
3d9e5f4
 
 
 
 
 
 
 
 
 
3f834bf
3d9e5f4
1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
from deepforest import main
import cv2

def show_trees(img_path):
    try:
        model = main.deepforest()
        model.use_release()
    except Exception as model_error:
        raise gr.Error(f"Error initializing the deepforest model: {model_error}")

    img=cv2.imread(img_path)

    if img is None:
        raise gr.Error(f"Image path is not valid.")

    if img.shape[0]<1000 and img.shape[1]<1000:
        img = model.predict_image(path=img_path, return_plot=True)
    elif img.shape[0]>1000 or img.shape[1]>1000:
        img=model.predict_image(path=img_path,return_plot=True,thickness=2)
    else:
        img = model.predict_image(path=img_path, return_plot=True,thickness=3)

    if img is None:
        raise gr.Error("No predictions were made. Check your test image. Ensure it conists")
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    return img

def show_birds(img_path):
    try:
        model = main.deepforest()
        model.use_bird_release()
    except Exception as model_error:
        raise gr.Error(f"Error initializing the deepforest model: {model_error}")
    img=cv2.imread(img_path)

    if img is None:
        raise gr.Error(f"Image path is not valid.")

    if img.shape[0]<1000 and img.shape[1]<1000:
        img = model.predict_image(path=img_path, return_plot=True)
    elif img.shape[0]>1000 or img.shape[1]>1000:
        img=model.predict_image(path=img_path,return_plot=True,thickness=2)
    else:
        img = model.predict_image(path=img_path, return_plot=True,thickness=3)

    if img is None:
        raise gr.Error("No predictions were made. Check your test image. Ensure it conists")
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    return img

def show_livestock(img_path):
    try:
        model = main.deepforest()
        model.load_model(model_name="weecology/deepforest-livestock")
    except Exception as model_error:
        raise gr.Error(f"Error initializing the deepforest model: {model_error}")
    img=cv2.imread(img_path)

    if img is None:
        raise gr.Error(f"Image path is not valid.")

    if img.shape[0]<1000 and img.shape[1]<1000:
        img = model.predict_image(path=img_path, return_plot=True)
    elif img.shape[0]>1000 or img.shape[1]>1000:
        img=model.predict_image(path=img_path,return_plot=True,thickness=2)
    else:
        img = model.predict_image(path=img_path, return_plot=True,thickness=3)

    if img is None:
        raise gr.Error("No predictions were made. Check your test image. Ensure it conists")
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    return img

with gr.Blocks() as demo:
    gr.Markdown('# Deepforest')
    gr.Markdown('## Tree Detection Model')
    gr.Markdown('### Predict trees')
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(label="Input Image", type="filepath")
        with gr.Column():
            output_image = gr.Image(label="Predicted image")

    submit_button_trees = gr.Button("Predict trees")
    submit_button_trees.click(show_trees, inputs=input_image, outputs=output_image)

    gr.Markdown('### Predict birds')
    with gr.Row():
        with gr.Column():
            input_image=gr.Image(label="Input image",type="filepath")
        with gr.Column():
            output_image=gr.Image(label="Predicted Image")

    submit_button_birds = gr.Button("Predict birds")
    submit_button_birds.click(show_birds,inputs=input_image,outputs=output_image)

    gr.Markdown('### Predict livestock')
    with gr.Row():
        with gr.Column():
            input_image=gr.Image(label="Input image",type="filepath")
        with gr.Column():
            output_image=gr.Image(label="Predicted Image")

    submit_button_livestock = gr.Button("Predict livestock")
    submit_button_livestock.click(show_livestock,inputs=input_image,outputs=output_image)

if __name__ == "__main__":
    demo.launch()