Parthebhan commited on
Commit
cc749dd
·
verified ·
1 Parent(s): 7af1cd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -85
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import spaces
3
  from huggingface_hub import hf_hub_download
4
  import os
5
  import cv2 # Import OpenCV
@@ -14,111 +13,48 @@ def download_models(model_id):
14
  return local_model_path
15
 
16
 
17
- @spaces.GPU
18
-
19
- def yolov9_inference(img_path, model_id, conf_threshold, iou_threshold):
20
  """
21
- Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
22
- the input size and apply test time augmentation.
23
-
24
- :param model_id: Identifier of the YOLOv9 model.
25
- :param conf_threshold: Confidence threshold for NMS.
26
- :param iou_threshold: IoU threshold for NMS.
27
  :param img_path: Path to the image file.
28
  :return: Output image with detections.
29
  """
30
  # Load the image from the file path
31
  image = cv2.imread(img_path) # Use OpenCV to read the image
32
-
33
  # Load the model
34
- model_path = download_models(model_id)
35
- model = yolov9.load(model_path, device="cpu") # Change device if needed
36
-
37
- # Set model parameters
38
- model.conf = conf_threshold
39
- model.iou = iou_threshold
40
-
41
  # Perform inference
42
- results = model(image) # Pass the loaded image instead of the file path
43
 
44
  # Optionally, show detection bounding boxes on image
45
- output = results.render()
46
-
47
- return output[0]
48
-
49
 
50
-
51
 
52
 
53
  def app():
54
  with gr.Blocks():
55
  with gr.Row():
56
  with gr.Column():
57
- img_path = gr.Image(type="filepath", label="Image")
58
- model_path = gr.Dropdown(
59
- label="Model",
60
- choices=[
61
- "gelan-c-seg.pt",
62
- "gelan-e.pt",
63
- "yolov9-c.pt",
64
- "yolov9-e.pt",
65
- ],
66
- value="gelan-e.pt",
67
- )
68
- image_size = gr.Slider(
69
- label="Image Size",
70
- minimum=320,
71
- maximum=1280,
72
- step=32,
73
- value=640,
74
- )
75
- conf_threshold = gr.Slider(
76
- label="Confidence Threshold",
77
- minimum=0.1,
78
- maximum=1.0,
79
- step=0.1,
80
- value=0.4,
81
- )
82
- iou_threshold = gr.Slider(
83
- label="IoU Threshold",
84
- minimum=0.1,
85
- maximum=1.0,
86
- step=0.1,
87
- value=0.5,
88
- )
89
- yolov9_infer = gr.Button(value="Inference")
90
-
91
  with gr.Column():
92
- output_numpy = gr.Image(type="numpy",label="Output")
93
 
94
- yolov9_infer.click(
95
  fn=yolov9_inference,
96
- inputs=[
97
- img_path,
98
- image_size,
99
- conf_threshold,
100
- iou_threshold,
101
- ],
102
- outputs=[output_numpy],
103
  )
104
 
105
- gradio_app = gr.Blocks()
106
- with gradio_app:
107
- gr.HTML(
108
- """
109
- <h1 style='text-align: center'>
110
- YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
111
- </h1>
112
- """)
113
- gr.HTML(
114
- """
115
- <h3 style='text-align: center'>
116
- Follow me for more!
117
- <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>
118
- </h3>
119
- """)
120
- with gr.Row():
121
- with gr.Column():
122
- app()
123
 
 
 
 
 
 
124
  gradio_app.launch(debug=True)
 
1
  import gradio as gr
 
2
  from huggingface_hub import hf_hub_download
3
  import os
4
  import cv2 # Import OpenCV
 
13
  return local_model_path
14
 
15
 
16
+ def yolov9_inference(img_path):
 
 
17
  """
18
+ Perform inference on an image using the YOLOv9 model.
19
+
 
 
 
 
20
  :param img_path: Path to the image file.
21
  :return: Output image with detections.
22
  """
23
  # Load the image from the file path
24
  image = cv2.imread(img_path) # Use OpenCV to read the image
25
+
26
  # Load the model
27
+ model_path = download_models("gelan-c-seg.pt")
28
+ model = cv2.dnn.readNetFromDarknet(model_path)
29
+
 
 
 
 
30
  # Perform inference
31
+ # (Add your inference code here)
32
 
33
  # Optionally, show detection bounding boxes on image
34
+ output_image = image # Placeholder for the output image
 
 
 
35
 
36
+ return output_image
37
 
38
 
39
  def app():
40
  with gr.Blocks():
41
  with gr.Row():
42
  with gr.Column():
43
+ img_path = gr.Image(type="file", label="Upload Image")
44
+ inference_button = gr.Button(label="Run Inference")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  with gr.Column():
46
+ output_image = gr.Image(label="Output Image")
47
 
48
+ inference_button.click(
49
  fn=yolov9_inference,
50
+ inputs=[img_path],
51
+ outputs=[output_image],
 
 
 
 
 
52
  )
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ gradio_app = gr.Interface(
56
+ app,
57
+ title="YOLOv9 Inference",
58
+ description="Perform object detection using the YOLOv9 model.",
59
+ )
60
  gradio_app.launch(debug=True)