File size: 2,044 Bytes
f1c31b8
55311cc
f1c31b8
55311cc
a6972d6
f1c31b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8458b85
f1c31b8
 
 
 
 
 
a6972d6
f1c31b8
 
aec3a67
a8a7793
ed335e7
 
 
a8a7793
 
00700b7
f1c31b8
 
 
d325ab8
f1c31b8
 
 
 
 
 
 
 
a8a7793
f1c31b8
a8a7793
f1c31b8
 
 
 
d325ab8
f1c31b8
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
import gradio as gr
import spaces

@spaces.GPU
def yolov9_inference(img_path, model_path,image_size, 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 model_path: Path to the YOLOv9 model file.
    :param conf_threshold: Confidence threshold for NMS.
    :param iou_threshold: IoU threshold for NMS.
    :param img_path: Path to the image file.
    :param size: Optional, input size for inference.
    :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 = 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=image_size)

    # Optionally, show detection bounding boxes on image
    save_path = 'output/'
    results.save(labels=True, save_dir=save_path, exist_ok=True)
    print("save_path:",save_path)
    print("img_path:",img_path)

    output_path = save_path + img_path
    print(f"Output image saved to {output_path}")
    return output_path


inputs = [
    gr.Image(type="filepath", label="Input Image"),
    gr.Dropdown(
        label="Model",
        choices=[
            "gelan-c.pt",
            "gelan-e.pt",
            "yolov9-c.pt",
            "yolov9-e.pt",
        ],
        value="gelan-c.pt",
    ),
    gr.Slider(minimum=320, maximum=1280, value=640, step=32, label="Image Size"),
    gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"),
    gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
]

outputs = gr.Image(label="Output Image")
title = "YOLOv9"

demo_app = gr.Interface(
    fn=yolov9_inference,
    inputs=inputs,
    outputs=outputs,
    title=title,
)
demo_app.launch(debug=True)