TroglodyteDerivations commited on
Commit
cecd987
1 Parent(s): a245734

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ import yolov9
4
+
5
+ class Inference_Nascent_Spawning_Deriving_From_YOLOv9:
6
+ def __init__(self):
7
+ self.model = None
8
+ self.model_path = None
9
+ self.image_size = None
10
+ self.conf_threshold = None
11
+ self.iou_threshold = None
12
+
13
+ # Object behavior / Method -> 1
14
+ def download_models(self, model_id):
15
+ hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")
16
+ return f"./{model_id}"
17
+
18
+ # Object behavior / Method -> 2
19
+ def load_model(self, model_id):
20
+ self.model_path = self.download_models(model_id)
21
+ self.model = yolov9.load(self.model_path, device="cuda:0")
22
+
23
+ # Object behavior / Method -> 3
24
+ def configure_model(self, conf_threshold, iou_threshold):
25
+ self.conf_threshold = conf_threshold
26
+ self.iou_threshold = iou_threshold
27
+ self.model.conf = self.conf_threshold
28
+ self.model.iou = self.iou_threshold
29
+
30
+ # Object behavior / Method -> 4
31
+ def perform_inference(self, img_path, image_size):
32
+ self.image_size = image_size
33
+ results = self.model(img_path, size=self.image_size)
34
+ output = results.render()
35
+ return output[0]
36
+
37
+ # Object behavior / Method -> 5
38
+ # Note: 5 is a method deriving from within the class with the name
39
+ # Inference_Nascent_Spawning_Deriving_From_YOLOv9
40
+ # One can also declare outside of the OOP as a function, which in turn,
41
+ # calls the methods inside of the OOP leveraging the functionality
42
+ # fostering from each unique Object behavior / Method
43
+ # Personal preferecnce -> This instantiation from within OOP
44
+ def launch_gradio_app(self):
45
+ with gr.Blocks() as gradio_app:
46
+ with gr.Row():
47
+ with gr.Column():
48
+ img_path = gr.Image(type='filepath', label='Image')
49
+ model_id = gr.Dropdown(
50
+ label="Model",
51
+ choices=[
52
+ "gelan-c.pt",
53
+ "gelan-e.pt",
54
+ "yolov9-c.pt",
55
+ "yolov9-e.pt",
56
+ ],
57
+ value="gelan-e.pt",
58
+ )
59
+ image_size = gr.Slider(
60
+ label="Image Size",
61
+ minimum=320,
62
+ maximum=1280,
63
+ step=32,
64
+ value=640, # Default value of 640 foments higher percentage obverse the image detection
65
+ )
66
+ conf_threshold = gr.Slider(
67
+ label="Confidence Threshold",
68
+ minimum=0.1,
69
+ maximum=1.0,
70
+ step=0.1,
71
+ value=0.4,
72
+ )
73
+ iou_threshold = gr.Slider(
74
+ label="IoU Threshold",
75
+ minimum=0.1,
76
+ maximum=1.0,
77
+ step=0.1,
78
+ value=0.5,
79
+ )
80
+ yolov9_infer = gr.Button(value="Inference")
81
+
82
+ with gr.Column():
83
+ output_numpy = gr.Image(type="numpy", label="Output")
84
+
85
+ # yolov9_infer leveraging click functionality
86
+ # Resembles iface = gr.Interface(
87
+ #fn=...
88
+ #inputs=[],
89
+ #outputs=[],
90
+ yolov9_infer.click(
91
+ fn=self.perform_inference,
92
+ inputs=[
93
+ img_path,
94
+ model_id,
95
+ image_size,
96
+ conf_threshold,
97
+ iou_threshold,
98
+ ],
99
+ outputs=[output_numpy],
100
+ )
101
+
102
+ gr.Examples(
103
+ examples=[
104
+ ["cow.jpeg", "gelan-e.pt", 640, 0.4, 0.5],
105
+ ["techengue_GTA.png", "yolov9-c.pt", 640, 0.4, 0.5],
106
+ ],
107
+ fn=self.perform_inference,
108
+ inputs=[
109
+ img_path,
110
+ model_id,
111
+ image_size,
112
+ conf_threshold,
113
+ iou_threshold,
114
+ ].
115
+ outputs=[output_numpy],
116
+ cache_examples=True,
117
+ )
118
+
119
+ gr.HTML(
120
+ """
121
+ <h1 style='text-align: center'>
122
+ YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
123
+ </h1>
124
+ """
125
+ )
126
+
127
+ gr.HTML(
128
+ """
129
+ <h3 style='text-align: center'>
130
+ Expound further notions regarding this topic at:
131
+ https://doi.org/10.48550/arXiv.2402.13616
132
+ """)
133
+
134
+ gradio_app.launch(debug=True)
135
+
136
+ # Instantiate the class and launch the Gradio app
137
+ yolo_inference = Inference_Nascent_Spawning_Deriving_From_YOLOv9()
138
+ yolo_inference.launch_gradio_app()