File size: 5,425 Bytes
cecd987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc8c91
 
cecd987
 
 
 
 
 
 
 
 
2153b39
cecd987
35760d1
 
cecd987
 
 
 
 
 
 
 
 
 
7109c38
cecd987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41a6271
cecd987
 
 
 
 
 
 
 
 
 
 
 
 
 
41a6271
cecd987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41a6271
 
cecd987
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import gradio as gr
from huggingface_hub import hf_hub_download
import yolov9

class Inference_Nascent_Spawning_Deriving_From_YOLOv9:
    def __init__(self):
        self.model = None
        self.model_path = None
        self.image_size = None 
        self.conf_threshold = None 
        self.iou_threshold = None 
        
    # Object behavior / Method -> 1
    def download_models(self, model_id):
        hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")
        return f"./{model_id}"
    
    # Object behavior / Method -> 2
    def load_model(self, model_id):
        self.model_path = self.download_models(model_id)
        self.model = yolov9.load(self.model_path, device="cpu") # Inference generated from CPU 
        #self.model = yolov9.load(self.model_path, device="cuda:0")
        
    # Object behavior / Method -> 3
    def configure_model(self, conf_threshold, iou_threshold):
        self.conf_threshold = conf_threshold 
        self.iou_threshold = iou_threshold 
        self.model.conf = self.conf_threshold 
        self.model.iou = self.iou_threshold 
        
    # Object behavior / Method -> 4
    def perform_inference(self, img_path,model_id,image_size, conf_threshold, iou_threshold):
        self.image_size = image_size
        self.load_model(model_id) # Load the model before performing inference
        self.configure_model(conf_threshold, iou_threshold)
        results = self.model(img_path, size=self.image_size)
        output = results.render()
        return output[0]
    
    # Object behavior / Method -> 5 
    # Note: 5 is a method deriving from within the class with the name 
    # Inference_Nascent_Spawning_Deriving_From_YOLOv9
    # One can also declare outside of the OOP as a function, which in turn,
    # calls the methods inside of the OOP leveraging the functionality
    # fostering from each unique Object behavior / Method
    # Personal preference -> This instantiation from within OOP
    def launch_gradio_app(self):
        with gr.Blocks() as gradio_app: 
            with gr.Row():
                with gr.Column():
                    img_path = gr.Image(type='filepath', label='Image')
                    model_id = gr.Dropdown(
                        label="Model",
                        choices=[
                            "gelan-c.pt",
                            "gelan-e.pt",
                            "yolov9-c.pt",
                            "yolov9-e.pt",
                        ],
                        value="gelan-e.pt",
                    )
                    image_size = gr.Slider(
                        label="Image Size",
                        minimum=320,
                        maximum=1280,
                        step=32,
                        value=640, # Default value of 640 foments higher percentage obverse the image detection
                    )
                    conf_threshold = gr.Slider(
                        label="Confidence Threshold",
                        minimum=0.1,
                        maximum=1.0,
                        step=0.1,
                        value=0.4,
                    )
                    iou_threshold = gr.Slider(
                        label="IoU Threshold",
                        minimum=0.1,
                        maximum=1.0,
                        step=0.1,
                        value=0.5,
                    )
                    yolov9_infer = gr.Button(value="Inference")
                    
                with gr.Column():
                    output_numpy = gr.Image(type="numpy", label="Output")
                    
            # yolov9_infer leveraging click functionality
            # Resembles iface = gr.Interface( 
            #fn=... 
            #inputs=[], 
            #outputs=[],
            yolov9_infer.click(
                fn=self.perform_inference,
                inputs=[
                    img_path,
                    model_id,
                    image_size,
                    conf_threshold,
                    iou_threshold,
                ],
                outputs=[output_numpy],
             
            )
            
            gr.Examples(
                examples=[
                    ["cow.jpeg", "gelan-e.pt", 640, 0.4, 0.5], 
                    ["techengue_GTA.png", "yolov9-c.pt", 640, 0.4, 0.5],
                ],
                fn=self.perform_inference,
                inputs=[
                    img_path,
                    model_id,
                    image_size,
                    conf_threshold,
                    iou_threshold,
                ],
                outputs=[output_numpy],
                cache_examples=True,
            )
            
            gr.HTML(
                """
                <h1 style='text-align: center'>
                YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
                </h1>
                """
            )
            
            gr.HTML(
            """
            <h3 style='text-align: center'>
            Expound further notions regarding this topic at: 
            https://doi.org/10.48550/arXiv.2402.13616
            """
            )
            
        gradio_app.launch(debug=True)
        
# Instantiate the class and launch the Gradio app
yolo_inference = Inference_Nascent_Spawning_Deriving_From_YOLOv9()
yolo_inference.launch_gradio_app()