File size: 8,216 Bytes
84dcfe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fea6fc
 
 
611a237
 
84dcfe3
 
 
2fea6fc
84dcfe3
 
2fea6fc
 
 
611a237
2fea6fc
 
 
 
 
 
 
84dcfe3
 
611a237
2fea6fc
84dcfe3
 
611a237
2fea6fc
 
 
611a237
2fea6fc
 
611a237
2fea6fc
 
611a237
2fea6fc
 
 
 
 
 
 
 
84dcfe3
 
 
47c345b
84dcfe3
47c345b
84dcfe3
2fea6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84dcfe3
2fea6fc
 
 
 
 
 
611a237
84dcfe3
2fea6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
84dcfe3
2fea6fc
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# import gradio as gr
# import spaces
# from huggingface_hub import hf_hub_download
# # Import YOLOv9
# import yolov9

# # def download_models(model_id):
# #     hf_hub_download("SakshiRathi77/void-space-detection/weights", filename=f"{model_id}", local_dir=f"./")
# #     return f"./{model_id}"

# def download_models(model_id):
#     hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")
#     return f"./{model_id}"

# def yolov9_inference(img_path, image_size, conf_threshold, iou_threshold):
#     """
#     Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust 
#     the input size and apply test time augmentation.
    
#     :param model_path: Path to the YOLOv9 model file.
#     :param conf_threshold: Confidence threshold for NMS.
#     :param iou_threshold: IoU threshold for NMS.
#     :param img_path: Path to the image file.
#     :param size: Optional, input size for inference.
#     :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
#     """

    
#     # Load the model
#     model_path = download_models()
#     # model = yolov9.load("./best.pt")
    
#     # Set model parameters
#     model.conf = conf_threshold
#     model.iou = iou_threshold
    
#     # Perform inference
#     results = model(img_path, size=image_size)

#     # Optionally, show detection bounding boxes on image
#     output = results.render()
    
#     return output[0]


# def app():
#     with gr.Blocks():
#         with gr.Row():
#             with gr.Column():
#                 img_path = gr.Image(type="filepath", label="Image")
#                 image_size = gr.Slider(
#                     label="Image Size",
#                     minimum=320,
#                     maximum=1280,
#                     step=32,
#                     value=640,
#                 )
#                 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.click(
#             fn=yolov9_inference,
#             inputs=[
#                 img_path,
#                 # model_path,
#                 image_size,
#                 conf_threshold,
#                 iou_threshold,
#             ],
#             outputs=[output_numpy],
#         )
        
  


# gradio_app = gr.Blocks()
# with gradio_app:
#     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'>
#         Follow me for more!
#         </h3>
#         """)
#     with gr.Row():
#         with gr.Column():
#             app()

# gradio_app.launch(debug=True)

# make sure you have the following dependencies
# import gradio as gr
# import torch
# from torchvision import transforms
# from PIL import Image

# # Load the YOLOv9 model
# model_path = "best.pt"  # Replace with the path to your YOLOv9 model
# model = torch.load(model_path)

# # Define preprocessing transforms
# preprocess = transforms.Compose([
#     transforms.Resize((640, 640)),  # Resize image to model input size
#     transforms.ToTensor(),           # Convert image to tensor
# ])

# # Define a function to perform inference
# def detect_void(image):
#     # Preprocess the input image
#     image = Image.fromarray(image)
#     image = preprocess(image).unsqueeze(0)  # Add batch dimension

#     # Perform inference
#     with torch.no_grad():
#         output = model(image)

#     # Post-process the output if needed
#     # For example, draw bounding boxes on the image

#     # Convert the image back to numpy array
#     # and return the result
#     return output.squeeze().numpy()

# # Define Gradio interface components
# input_image = gr.inputs.Image(shape=(640, 640), label="Input Image")
# output_image = gr.outputs.Image(label="Output Image")

# # Create Gradio interface
# gr.Interface(fn=detect_void, inputs=input_image, outputs=output_image, title="Void Detection App").launch()




import gradio as gr
import spaces
from huggingface_hub import hf_hub_download


def download_models(model_id):
    hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")
    return f"./{model_id}"


def yolov9_inference(img_path, model_id, image_size, conf_threshold, iou_threshold):
    """
    Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust 
    the input size and apply test time augmentation.
    
    :param model_path: Path to the YOLOv9 model file.
    :param conf_threshold: Confidence threshold for NMS.
    :param iou_threshold: IoU threshold for NMS.
    :param img_path: Path to the image file.
    :param size: Optional, input size for inference.
    :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
    """
    # Import YOLOv9
    import yolov9
    
    # Load the model
    model_path = download_models(model_id)
    model = yolov9.load(model_path)
    
    # Set model parameters
    model.conf = conf_threshold
    model.iou = iou_threshold
    
    # Perform inference
    results = model(img_path, size=image_size)

    # Optionally, show detection bounding boxes on image
    output = results.render()
    
    return output[0]


def app():
    with gr.Blocks():
        with gr.Row():
            with gr.Column():
                img_path = gr.Image(type="filepath", label="Image")
                model_path = gr.Dropdown(
                    label="Model",
                    choices=[
                        "best.pt",
                    ],
                    value="best.pt",
                )
                image_size = gr.Slider(
                    label="Image Size",
                    minimum=320,
                    maximum=1280,
                    step=32,
                    value=640,
                )
                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.click(
            fn=yolov9_inference,
            inputs=[
                img_path,
                model_path,
                image_size,
                conf_threshold,
                iou_threshold,
            ],
            outputs=[output_numpy],
        )
        



gradio_app = gr.Blocks()
with gradio_app:
    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'>
        Follow me for more!
        <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a>  | <a href='https://www.huggingface.co/kadirnar/' target='_blank'>HuggingFace</a>
        </h3>
        """)
    with gr.Row():
        with gr.Column():
            app()

gradio_app.launch(debug=True)