sushantsharma13981 commited on
Commit
9f23460
·
verified ·
1 Parent(s): f1329b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -1,33 +1,40 @@
1
  import gradio as gr
2
  from ultralytics import YOLO
3
- import cv2
4
  import numpy as np
5
 
6
- # Load your YOLO11n model
7
  model = YOLO("yolo11n.pt")
8
 
9
- def detect_obstacles(image):
10
- """
11
- Run YOLO11n model on the uploaded image and return detections.
12
- """
13
- # Convert PIL image to numpy
14
  img = np.array(image)
15
 
16
- # Run inference
17
- results = model.predict(img)
18
 
19
- # Draw detections on image
20
- annotated_img = results[0].plot() # Ultralytics provides a plot() method
21
 
22
- return annotated_img
 
 
 
 
 
 
 
23
 
24
- # Gradio interface
25
  demo = gr.Interface(
26
  fn=detect_obstacles,
27
- inputs=gr.Image(type="pil", label="Upload an Image"),
28
- outputs=gr.Image(type="numpy", label="Detected Obstacles"),
 
 
 
 
 
 
29
  title="YOLO11n Obstacle Detection",
30
- description="Upload an image and the YOLO11n model will detect obstacles."
31
  )
32
 
33
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from ultralytics import YOLO
 
3
  import numpy as np
4
 
5
+ # Load YOLO11n model
6
  model = YOLO("yolo11n.pt")
7
 
8
+ def detect_obstacles(image, conf_threshold):
 
 
 
 
9
  img = np.array(image)
10
 
11
+ # Run inference with confidence threshold
12
+ results = model.predict(img, conf=conf_threshold)
13
 
14
+ # Annotated image
15
+ annotated_img = results[0].plot()
16
 
17
+ # Extract labels for debugging
18
+ labels = []
19
+ for box in results[0].boxes:
20
+ cls_id = int(box.cls[0])
21
+ conf = float(box.conf[0])
22
+ labels.append(f"{model.names[cls_id]} ({conf:.2f})")
23
+
24
+ return annotated_img, "\n".join(labels)
25
 
 
26
  demo = gr.Interface(
27
  fn=detect_obstacles,
28
+ inputs=[
29
+ gr.Image(type="pil", label="Upload Image"),
30
+ gr.Slider(0, 1, value=0.5, label="Confidence Threshold")
31
+ ],
32
+ outputs=[
33
+ gr.Image(type="numpy", label="Detected Obstacles"),
34
+ gr.Textbox(label="Detections")
35
+ ],
36
  title="YOLO11n Obstacle Detection",
37
+ description="Upload an image. Adjust confidence threshold to filter detections."
38
  )
39
 
40
  if __name__ == "__main__":