Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,141 +1,177 @@
|
|
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 |
-
|
8 |
-
|
9 |
|
10 |
-
@spaces.GPU
|
11 |
-
def yolov9_inference(img_path, model_id, image_size, conf_threshold, iou_threshold):
|
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 |
-
def app():
|
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 |
-
gradio_app = gr.Blocks()
|
124 |
-
with gradio_app:
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
gradio_app.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", filename=f"{model_id}", local_dir=f"./")
|
8 |
+
# return f"./{model_id}"
|
9 |
|
10 |
+
# @spaces.GPU
|
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(model_id)
|
28 |
+
# model = yolov9.load(model_path, 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 = gr.Dropdown(
|
49 |
+
# label="Model",
|
50 |
+
# choices=[
|
51 |
+
# "state_dict.pt"
|
52 |
+
# ],
|
53 |
+
# value="state_dict.pt",
|
54 |
+
# )
|
55 |
+
# image_size = gr.Slider(
|
56 |
+
# label="Image Size",
|
57 |
+
# minimum=320,
|
58 |
+
# maximum=1280,
|
59 |
+
# step=32,
|
60 |
+
# value=640,
|
61 |
+
# )
|
62 |
+
# conf_threshold = gr.Slider(
|
63 |
+
# label="Confidence Threshold",
|
64 |
+
# minimum=0.1,
|
65 |
+
# maximum=1.0,
|
66 |
+
# step=0.1,
|
67 |
+
# value=0.4,
|
68 |
+
# )
|
69 |
+
# iou_threshold = gr.Slider(
|
70 |
+
# label="IoU Threshold",
|
71 |
+
# minimum=0.1,
|
72 |
+
# maximum=1.0,
|
73 |
+
# step=0.1,
|
74 |
+
# value=0.5,
|
75 |
+
# )
|
76 |
+
# yolov9_infer = gr.Button(value="Inference")
|
77 |
+
|
78 |
+
# with gr.Column():
|
79 |
+
# output_numpy = gr.Image(type="numpy",label="Output")
|
80 |
+
|
81 |
+
# yolov9_infer.click(
|
82 |
+
# fn=yolov9_inference,
|
83 |
+
# inputs=[
|
84 |
+
# img_path,
|
85 |
+
# model_path,
|
86 |
+
# image_size,
|
87 |
+
# conf_threshold,
|
88 |
+
# iou_threshold,
|
89 |
+
# ],
|
90 |
+
# outputs=[output_numpy],
|
91 |
+
# )
|
92 |
|
93 |
+
# # gr.Examples(
|
94 |
+
# # examples=[
|
95 |
+
# # [
|
96 |
+
# # "data/zidane.jpg",
|
97 |
+
# # "gelan-e.pt",
|
98 |
+
# # 640,
|
99 |
+
# # 0.4,
|
100 |
+
# # 0.5,
|
101 |
+
# # ],
|
102 |
+
# # [
|
103 |
+
# # "data/huggingface.jpg",
|
104 |
+
# # "yolov9-c.pt",
|
105 |
+
# # 640,
|
106 |
+
# # 0.4,
|
107 |
+
# # 0.5,
|
108 |
+
# # ],
|
109 |
+
# # ],
|
110 |
+
# # fn=yolov9_inference,
|
111 |
+
# # inputs=[
|
112 |
+
# # img_path,
|
113 |
+
# # model_path,
|
114 |
+
# # image_size,
|
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 torch
|
145 |
+
import numpy as np
|
146 |
+
from models.common import DetectMultiBackend
|
147 |
+
from utils.general import non_max_suppression, scale_boxes
|
148 |
+
from utils.torch_utils import select_device, smart_inference_mode
|
149 |
+
from utils.augmentations import letterbox
|
150 |
+
import PIL.Image
|
151 |
+
from huggingface_hub import hf_hub_download
|
152 |
+
hf_hub_download("SakshiRathi77/void-space-detection", filename="weights/best.pt", local_dir="./")
|
153 |
+
|
154 |
+
|
155 |
+
@smart_inference_mode()
|
156 |
+
def predict(image_path, weights='best.pt', imgsz=640, conf_thres=0.1, iou_thres=0.45):
|
157 |
+
# Initialize
|
158 |
+
device = select_device('0')
|
159 |
+
# model = DetectMultiBackend(weights='best.pt', device="0", fp16=False, data='data/coco.yaml')
|
160 |
+
model = DetectMultiBackend(weights='best.pt', device="0", fp16=False)
|
161 |
+
stride, names, pt = model.stride, model.names, model.pt
|
162 |
+
|
163 |
+
# Load image
|
164 |
+
image = np.array(PIL.Image.open(image_path))
|
165 |
+
img = letterbox(img0, imgsz, stride=stride, auto=True)[0]
|
166 |
+
img = img[:, :, ::-1].transpose(2, 0, 1)
|
167 |
+
img = np.ascontiguousarray(img)
|
168 |
+
img = torch.from_numpy(img).to(device).float()
|
169 |
+
img /= 255.0
|
170 |
+
if img.ndimension() == 3:
|
171 |
+
img = img.unsqueeze(0)
|
172 |
+
|
173 |
+
# Inference
|
174 |
+
# pred = model(img, augment=False, visualize=False)
|
175 |
+
pred = model(img)
|
176 |
+
# Apply NMS
|
177 |
+
pred = non_max_suppression(pred[0][0], conf_thres, iou_thres, classes=None, max_det=1000)
|