praysimanjuntak's picture
Update app.py
8caebbe verified
raw
history blame
2.8 kB
import gradio as gr
import spaces
from huggingface_hub import hf_hub_download
@spaces.GPU
def yolov9_inference(img_path, 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 conf_threshold: Confidence threshold for NMS.
:param iou_threshold: IoU threshold for NMS.
:param img_path: Path to the image file.
: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 = "yolov9-plant.pt" # model
model = yolov9.load(model_path, device="cuda:0")
# Set model parameters
model.conf = conf_threshold
model.iou = iou_threshold
# Perform inference
results = model(img_path, size=640)
# 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")
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,
conf_threshold,
iou_threshold,
],
outputs=[output_numpy],
)
gr.Examples(
examples=[
[
"data/apple_d1.jpg",
0.4,
0.5,
],
],
fn=yolov9_inference,
inputs=[
img_path,
conf_threshold,
iou_threshold,
],
outputs=[output_numpy],
cache_examples=True,
)
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>
""")
with gr.Row():
with gr.Column():
app()
gradio_app.launch(debug=True)