SakshiRathi77 commited on
Commit
84dcfe3
1 Parent(s): 8429a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -51
app.py CHANGED
@@ -1,15 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import spaces
3
  from huggingface_hub import hf_hub_download
4
- # Import YOLOv9
5
- import yolov9
6
 
7
- # def download_models(model_id):
8
- # hf_hub_download("SakshiRathi77/void-space-detection/weights", filename=f"{model_id}", local_dir=f"./")
9
- # return f"./{model_id}"
10
 
 
 
 
11
 
12
- def yolov9_inference(img_path, image_size, conf_threshold, iou_threshold):
 
13
  """
14
  Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
15
  the input size and apply test time augmentation.
@@ -21,11 +174,12 @@ def yolov9_inference(img_path, image_size, conf_threshold, iou_threshold):
21
  :param size: Optional, input size for inference.
22
  :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
23
  """
24
-
 
25
 
26
  # Load the model
27
- # model_path = download_models()
28
- model = yolov9.load("./best.pt")
29
 
30
  # Set model parameters
31
  model.conf = conf_threshold
@@ -45,6 +199,13 @@ def app():
45
  with gr.Row():
46
  with gr.Column():
47
  img_path = gr.Image(type="filepath", label="Image")
 
 
 
 
 
 
 
48
  image_size = gr.Slider(
49
  label="Image Size",
50
  minimum=320,
@@ -75,7 +236,7 @@ def app():
75
  fn=yolov9_inference,
76
  inputs=[
77
  img_path,
78
- # model_path,
79
  image_size,
80
  conf_threshold,
81
  iou_threshold,
@@ -83,7 +244,7 @@ def app():
83
  outputs=[output_numpy],
84
  )
85
 
86
-
87
 
88
 
89
  gradio_app = gr.Blocks()
@@ -98,6 +259,7 @@ with gradio_app:
98
  """
99
  <h3 style='text-align: center'>
100
  Follow me for more!
 
101
  </h3>
102
  """)
103
  with gr.Row():
@@ -105,43 +267,3 @@ with gradio_app:
105
  app()
106
 
107
  gradio_app.launch(debug=True)
108
-
109
- # make sure you have the following dependencies
110
- # import gradio as gr
111
- # import torch
112
- # from torchvision import transforms
113
- # from PIL import Image
114
-
115
- # # Load the YOLOv9 model
116
- # model_path = "best.pt" # Replace with the path to your YOLOv9 model
117
- # model = torch.load(model_path)
118
-
119
- # # Define preprocessing transforms
120
- # preprocess = transforms.Compose([
121
- # transforms.Resize((640, 640)), # Resize image to model input size
122
- # transforms.ToTensor(), # Convert image to tensor
123
- # ])
124
-
125
- # # Define a function to perform inference
126
- # def detect_void(image):
127
- # # Preprocess the input image
128
- # image = Image.fromarray(image)
129
- # image = preprocess(image).unsqueeze(0) # Add batch dimension
130
-
131
- # # Perform inference
132
- # with torch.no_grad():
133
- # output = model(image)
134
-
135
- # # Post-process the output if needed
136
- # # For example, draw bounding boxes on the image
137
-
138
- # # Convert the image back to numpy array
139
- # # and return the result
140
- # return output.squeeze().numpy()
141
-
142
- # # Define Gradio interface components
143
- # input_image = gr.inputs.Image(shape=(640, 640), label="Input Image")
144
- # output_image = gr.outputs.Image(label="Output Image")
145
-
146
- # # Create Gradio interface
147
- # gr.Interface(fn=detect_void, inputs=input_image, outputs=output_image, title="Void Detection App").launch()
 
1
+ # import gradio as gr
2
+ # import spaces
3
+ # from huggingface_hub import hf_hub_download
4
+ # # Import YOLOv9
5
+ # import yolov9
6
+
7
+ # # def download_models(model_id):
8
+ # # hf_hub_download("SakshiRathi77/void-space-detection/weights", filename=f"{model_id}", local_dir=f"./")
9
+ # # return f"./{model_id}"
10
+
11
+ # def download_models(model_id):
12
+ # hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")
13
+ # return f"./{model_id}"
14
+
15
+ # def yolov9_inference(img_path, image_size, conf_threshold, iou_threshold):
16
+ # """
17
+ # Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
18
+ # the input size and apply test time augmentation.
19
+
20
+ # :param model_path: Path to the YOLOv9 model file.
21
+ # :param conf_threshold: Confidence threshold for NMS.
22
+ # :param iou_threshold: IoU threshold for NMS.
23
+ # :param img_path: Path to the image file.
24
+ # :param size: Optional, input size for inference.
25
+ # :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
26
+ # """
27
+
28
+
29
+ # # Load the model
30
+ # model_path = download_models()
31
+ # # model = yolov9.load("./best.pt")
32
+
33
+ # # Set model parameters
34
+ # model.conf = conf_threshold
35
+ # model.iou = iou_threshold
36
+
37
+ # # Perform inference
38
+ # results = model(img_path, size=image_size)
39
+
40
+ # # Optionally, show detection bounding boxes on image
41
+ # output = results.render()
42
+
43
+ # return output[0]
44
+
45
+
46
+ # def app():
47
+ # with gr.Blocks():
48
+ # with gr.Row():
49
+ # with gr.Column():
50
+ # img_path = gr.Image(type="filepath", label="Image")
51
+ # image_size = gr.Slider(
52
+ # label="Image Size",
53
+ # minimum=320,
54
+ # maximum=1280,
55
+ # step=32,
56
+ # value=640,
57
+ # )
58
+ # conf_threshold = gr.Slider(
59
+ # label="Confidence Threshold",
60
+ # minimum=0.1,
61
+ # maximum=1.0,
62
+ # step=0.1,
63
+ # value=0.4,
64
+ # )
65
+ # iou_threshold = gr.Slider(
66
+ # label="IoU Threshold",
67
+ # minimum=0.1,
68
+ # maximum=1.0,
69
+ # step=0.1,
70
+ # value=0.5,
71
+ # )
72
+ # yolov9_infer = gr.Button(value="Inference")
73
+
74
+ # with gr.Column():
75
+ # output_numpy = gr.Image(type="numpy",label="Output")
76
+
77
+ # yolov9_infer.click(
78
+ # fn=yolov9_inference,
79
+ # inputs=[
80
+ # img_path,
81
+ # # model_path,
82
+ # image_size,
83
+ # conf_threshold,
84
+ # iou_threshold,
85
+ # ],
86
+ # outputs=[output_numpy],
87
+ # )
88
+
89
+
90
+
91
+
92
+ # gradio_app = gr.Blocks()
93
+ # with gradio_app:
94
+ # gr.HTML(
95
+ # """
96
+ # <h1 style='text-align: center'>
97
+ # YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
98
+ # </h1>
99
+ # """)
100
+ # gr.HTML(
101
+ # """
102
+ # <h3 style='text-align: center'>
103
+ # Follow me for more!
104
+ # </h3>
105
+ # """)
106
+ # with gr.Row():
107
+ # with gr.Column():
108
+ # app()
109
+
110
+ # gradio_app.launch(debug=True)
111
+
112
+ # make sure you have the following dependencies
113
+ # import gradio as gr
114
+ # import torch
115
+ # from torchvision import transforms
116
+ # from PIL import Image
117
+
118
+ # # Load the YOLOv9 model
119
+ # model_path = "best.pt" # Replace with the path to your YOLOv9 model
120
+ # model = torch.load(model_path)
121
+
122
+ # # Define preprocessing transforms
123
+ # preprocess = transforms.Compose([
124
+ # transforms.Resize((640, 640)), # Resize image to model input size
125
+ # transforms.ToTensor(), # Convert image to tensor
126
+ # ])
127
+
128
+ # # Define a function to perform inference
129
+ # def detect_void(image):
130
+ # # Preprocess the input image
131
+ # image = Image.fromarray(image)
132
+ # image = preprocess(image).unsqueeze(0) # Add batch dimension
133
+
134
+ # # Perform inference
135
+ # with torch.no_grad():
136
+ # output = model(image)
137
+
138
+ # # Post-process the output if needed
139
+ # # For example, draw bounding boxes on the image
140
+
141
+ # # Convert the image back to numpy array
142
+ # # and return the result
143
+ # return output.squeeze().numpy()
144
+
145
+ # # Define Gradio interface components
146
+ # input_image = gr.inputs.Image(shape=(640, 640), label="Input Image")
147
+ # output_image = gr.outputs.Image(label="Output Image")
148
+
149
+ # # Create Gradio interface
150
+ # gr.Interface(fn=detect_void, inputs=input_image, outputs=output_image, title="Void Detection App").launch()
151
+
152
+
153
+
154
+
155
  import gradio as gr
156
  import spaces
157
  from huggingface_hub import hf_hub_download
 
 
158
 
 
 
 
159
 
160
+ def download_models(model_id):
161
+ hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")
162
+ return f"./{model_id}"
163
 
164
+
165
+ def yolov9_inference(img_path, model_id, image_size, conf_threshold, iou_threshold):
166
  """
167
  Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
168
  the input size and apply test time augmentation.
 
174
  :param size: Optional, input size for inference.
175
  :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
176
  """
177
+ # Import YOLOv9
178
+ import yolov9
179
 
180
  # Load the model
181
+ model_path = download_models(model_id)
182
+ model = yolov9.load(model_path)
183
 
184
  # Set model parameters
185
  model.conf = conf_threshold
 
199
  with gr.Row():
200
  with gr.Column():
201
  img_path = gr.Image(type="filepath", label="Image")
202
+ model_path = gr.Dropdown(
203
+ label="Model",
204
+ choices=[
205
+ "yolov9-c.pt",
206
+ ],
207
+ value="yolov9-c.pt",
208
+ )
209
  image_size = gr.Slider(
210
  label="Image Size",
211
  minimum=320,
 
236
  fn=yolov9_inference,
237
  inputs=[
238
  img_path,
239
+ model_path,
240
  image_size,
241
  conf_threshold,
242
  iou_threshold,
 
244
  outputs=[output_numpy],
245
  )
246
 
247
+
248
 
249
 
250
  gradio_app = gr.Blocks()
 
259
  """
260
  <h3 style='text-align: center'>
261
  Follow me for more!
262
+ <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>
263
  </h3>
264
  """)
265
  with gr.Row():
 
267
  app()
268
 
269
  gradio_app.launch(debug=True)