Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
from ultralytics import YOLO
|
4 |
+
|
5 |
+
# Load the pretrained YOLOv5 model
|
6 |
+
yolo_model = YOLO("yolov5s.pt") # Load the small version of YOLOv5
|
7 |
+
|
8 |
+
# Function to perform object detection with a configurable confidence threshold
|
9 |
+
def detect_objects(frame, confidence_threshold=0.5):
|
10 |
+
# Convert the frame from BGR (OpenCV) to RGB
|
11 |
+
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
12 |
+
|
13 |
+
# Perform inference with YOLO, passing the confidence threshold
|
14 |
+
results = yolo_model(image, conf=confidence_threshold) # Set confidence threshold
|
15 |
+
|
16 |
+
# Draw bounding boxes and labels on the image
|
17 |
+
annotated_image = results.plot() # This automatically draws boxes and labels
|
18 |
+
|
19 |
+
# Convert the image back to BGR for displaying in Gradio
|
20 |
+
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR)
|
21 |
+
|
22 |
+
return annotated_image
|
23 |
+
|
24 |
+
# Gradio interface to use the webcam for real-time object detection
|
25 |
+
# Added a slider for the confidence threshold
|
26 |
+
iface = gr.Interface(fn=detect_objects,
|
27 |
+
inputs=[
|
28 |
+
gr.Video(source="webcam", type="numpy"), # Webcam input
|
29 |
+
gr.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Confidence Threshold") # Confidence slider
|
30 |
+
],
|
31 |
+
outputs="image") # Show output image with bounding boxes
|
32 |
+
|
33 |
+
iface.launch()
|