Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,125 +2,121 @@ 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 |
-
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="cpu")
|
| 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 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
img_path = gr.Image(type="filepath", label="Image")
|
| 48 |
-
model_path = gr.Dropdown(
|
| 49 |
-
label="Model",
|
| 50 |
-
choices=[
|
| 51 |
-
"gelan-c.pt",
|
| 52 |
-
"gelan-e.pt",
|
| 53 |
-
"yolov9-c.pt",
|
| 54 |
-
"yolov9-e.pt",
|
| 55 |
-
],
|
| 56 |
-
value="gelan-e.pt",
|
| 57 |
-
)
|
| 58 |
-
image_size = gr.Slider(
|
| 59 |
-
label="Image Size",
|
| 60 |
-
minimum=320,
|
| 61 |
-
maximum=1280,
|
| 62 |
-
step=32,
|
| 63 |
-
value=640,
|
| 64 |
-
)
|
| 65 |
-
conf_threshold = gr.Slider(
|
| 66 |
-
label="Confidence Threshold",
|
| 67 |
-
minimum=0.1,
|
| 68 |
-
maximum=1.0,
|
| 69 |
-
step=0.1,
|
| 70 |
-
value=0.4,
|
| 71 |
-
)
|
| 72 |
-
iou_threshold = gr.Slider(
|
| 73 |
-
label="IoU Threshold",
|
| 74 |
-
minimum=0.1,
|
| 75 |
-
maximum=1.0,
|
| 76 |
-
step=0.1,
|
| 77 |
-
value=0.5,
|
| 78 |
-
)
|
| 79 |
-
yolov9_infer = gr.Button(value="Submit")
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
inputs=[
|
| 87 |
-
img_path,
|
| 88 |
-
model_path,
|
| 89 |
-
image_size,
|
| 90 |
-
conf_threshold,
|
| 91 |
-
iou_threshold,
|
| 92 |
-
],
|
| 93 |
-
outputs=[output_numpy],
|
| 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 |
-
# Botão para acionar o processamento da imagem
|
| 121 |
-
process_button = gr.Button("Processar Imagem")
|
| 122 |
-
|
| 123 |
-
# Conectar o botão à função de processamento
|
| 124 |
-
process_button.click(fn=process_image, inputs=image_input, outputs=image_output)
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import spaces
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
+
class YOLOv9App:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.gradio_app = gr.Blocks()
|
| 8 |
|
| 9 |
+
def download_models(self, model_id):
|
| 10 |
+
hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir="./")
|
| 11 |
+
return f"./{model_id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
@spaces.GPU
|
| 14 |
+
def yolov9_inference(self, img_path, model_id, image_size, conf_threshold, iou_threshold):
|
| 15 |
+
"""
|
| 16 |
+
Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
|
| 17 |
+
the input size and apply test time augmentation.
|
| 18 |
+
|
| 19 |
+
:param model_path: Path to the YOLOv9 model file.
|
| 20 |
+
:param conf_threshold: Confidence threshold for NMS.
|
| 21 |
+
:param iou_threshold: IoU threshold for NMS.
|
| 22 |
+
:param img_path: Path to the image file.
|
| 23 |
+
:param size: Optional, input size for inference.
|
| 24 |
+
:return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
|
| 25 |
+
"""
|
| 26 |
+
# Import YOLOv9
|
| 27 |
+
import yolov9
|
| 28 |
+
|
| 29 |
+
# Load the model
|
| 30 |
+
model_path = self.download_models(model_id)
|
| 31 |
+
model = yolov9.load(model_path, device="cpu")
|
| 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 |
+
def app(self):
|
| 46 |
+
with gr.Blocks():
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
img_path = gr.Image(type="filepath", label="Image")
|
| 50 |
+
model_path = gr.Dropdown(
|
| 51 |
+
label="Model",
|
| 52 |
+
choices=[
|
| 53 |
+
"gelan-c.pt",
|
| 54 |
+
"gelan-e.pt",
|
| 55 |
+
"yolov9-c.pt",
|
| 56 |
+
"yolov9-e.pt",
|
| 57 |
+
],
|
| 58 |
+
value="gelan-e.pt",
|
| 59 |
+
)
|
| 60 |
+
image_size = gr.Slider(
|
| 61 |
+
label="Image Size",
|
| 62 |
+
minimum=320,
|
| 63 |
+
maximum=1280,
|
| 64 |
+
step=32,
|
| 65 |
+
value=640,
|
| 66 |
+
)
|
| 67 |
+
conf_threshold = gr.Slider(
|
| 68 |
+
label="Confidence Threshold",
|
| 69 |
+
minimum=0.1,
|
| 70 |
+
maximum=1.0,
|
| 71 |
+
step=0.1,
|
| 72 |
+
value=0.4,
|
| 73 |
+
)
|
| 74 |
+
iou_threshold = gr.Slider(
|
| 75 |
+
label="IoU Threshold",
|
| 76 |
+
minimum=0.1,
|
| 77 |
+
maximum=1.0,
|
| 78 |
+
step=0.1,
|
| 79 |
+
value=0.5,
|
| 80 |
+
)
|
| 81 |
+
yolov9_infer = gr.Button(value="Submit")
|
| 82 |
|
| 83 |
+
with gr.Column():
|
| 84 |
+
output_numpy = gr.Image(type="numpy", label="Output")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
yolov9_infer.click(
|
| 87 |
+
fn=self.yolov9_inference,
|
| 88 |
+
inputs=[
|
| 89 |
+
img_path,
|
| 90 |
+
model_path,
|
| 91 |
+
image_size,
|
| 92 |
+
conf_threshold,
|
| 93 |
+
iou_threshold,
|
| 94 |
+
],
|
| 95 |
+
outputs=[output_numpy],
|
| 96 |
+
)
|
| 97 |
|
| 98 |
+
def launch(self):
|
| 99 |
+
with self.gradio_app:
|
| 100 |
+
gr.HTML(
|
| 101 |
+
"""
|
| 102 |
+
<h1 style='text-align: center'>
|
| 103 |
+
YOLOv9 Base Model
|
| 104 |
+
</h1>
|
| 105 |
+
"""
|
| 106 |
+
)
|
| 107 |
+
gr.HTML(
|
| 108 |
+
"""
|
| 109 |
+
<h3 style='text-align: center'>
|
| 110 |
+
Aplicação para ajudar nos resgates do RS
|
| 111 |
+
</h3>
|
| 112 |
+
"""
|
| 113 |
+
)
|
| 114 |
+
with gr.Row():
|
| 115 |
+
with gr.Column():
|
| 116 |
+
self.app()
|
| 117 |
+
self.gradio_app.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
# Criação e execução da aplicação
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
yolov9_app = YOLOv9App()
|
| 122 |
+
yolov9_app.launch()
|