praysimanjuntak commited on
Commit
b37c435
1 Parent(s): b8f8a1d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from huggingface_hub import hf_hub_download
4
+
5
+ @spaces.GPU
6
+ def yolov9_inference(img_path, model_id, image_size, conf_threshold, iou_threshold):
7
+ """
8
+ Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
9
+ the input size and apply test time augmentation.
10
+
11
+ :param model_path: Path to the YOLOv9 model file.
12
+ :param conf_threshold: Confidence threshold for NMS.
13
+ :param iou_threshold: IoU threshold for NMS.
14
+ :param img_path: Path to the image file.
15
+ :param size: Optional, input size for inference.
16
+ :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
17
+ """
18
+ # Import YOLOv9
19
+ import yolov9
20
+
21
+ # Load the model
22
+ model_path = "yolov9-plant.pt"
23
+ model = yolov9.load(model_path, device="cpu")
24
+
25
+ # Set model parameters
26
+ model.conf = conf_threshold
27
+ model.iou = iou_threshold
28
+
29
+ # Perform inference
30
+ results = model(img_path, size=image_size)
31
+
32
+ # Optionally, show detection bounding boxes on image
33
+ output = results.render()
34
+
35
+ return output[0]
36
+
37
+
38
+ def app():
39
+ with gr.Blocks():
40
+ with gr.Row():
41
+ with gr.Column():
42
+ img_path = gr.Image(type="filepath", label="Image")
43
+ model_path = gr.Dropdown(
44
+ label="Model",
45
+ choices=[
46
+ "gelan-c.pt",
47
+ "gelan-e.pt",
48
+ "yolov9-c.pt",
49
+ "yolov9-e.pt",
50
+ ],
51
+ value="gelan-e.pt",
52
+ )
53
+ image_size = gr.Slider(
54
+ label="Image Size",
55
+ minimum=320,
56
+ maximum=1280,
57
+ step=32,
58
+ value=640,
59
+ )
60
+ conf_threshold = gr.Slider(
61
+ label="Confidence Threshold",
62
+ minimum=0.1,
63
+ maximum=1.0,
64
+ step=0.1,
65
+ value=0.4,
66
+ )
67
+ iou_threshold = gr.Slider(
68
+ label="IoU Threshold",
69
+ minimum=0.1,
70
+ maximum=1.0,
71
+ step=0.1,
72
+ value=0.5,
73
+ )
74
+ yolov9_infer = gr.Button(value="Inference")
75
+
76
+ with gr.Column():
77
+ output_numpy = gr.Image(type="numpy",label="Output")
78
+
79
+ yolov9_infer.click(
80
+ fn=yolov9_inference,
81
+ inputs=[
82
+ img_path,
83
+ model_path,
84
+ image_size,
85
+ conf_threshold,
86
+ iou_threshold,
87
+ ],
88
+ outputs=[output_numpy],
89
+ )
90
+
91
+ gr.Examples(
92
+ examples=[
93
+ [
94
+ "data/zidane.jpg",
95
+ "gelan-e.pt",
96
+ 640,
97
+ 0.4,
98
+ 0.5,
99
+ ],
100
+ [
101
+ "data/huggingface.jpg",
102
+ "yolov9-c.pt",
103
+ 640,
104
+ 0.4,
105
+ 0.5,
106
+ ],
107
+ ],
108
+ fn=yolov9_inference,
109
+ inputs=[
110
+ img_path,
111
+ model_path,
112
+ image_size,
113
+ conf_threshold,
114
+ iou_threshold,
115
+ ],
116
+ outputs=[output_numpy],
117
+ cache_examples=True,
118
+ )
119
+
120
+
121
+ gradio_app = gr.Blocks()
122
+ with gradio_app:
123
+ gr.HTML(
124
+ """
125
+ <h1 style='text-align: center'>
126
+ YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
127
+ </h1>
128
+ """)
129
+ gr.HTML(
130
+ """
131
+ <h3 style='text-align: center'>
132
+ Follow me for more!
133
+ <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a> | <a href='https://www.huggingface.co/kadirnar/' target='_blank'>HuggingFace</a>
134
+ </h3>
135
+ """)
136
+ with gr.Row():
137
+ with gr.Column():
138
+ app()
139
+
140
+ gradio_app.launch(debug=True)