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