Spaces:
Sleeping
Sleeping
rewe
Browse files
app.py
CHANGED
|
@@ -1,26 +1,28 @@
|
|
| 1 |
-
|
| 2 |
import cv2
|
| 3 |
-
from ultralytics import YOLO
|
| 4 |
import numpy as np
|
| 5 |
import torch
|
| 6 |
-
import threading
|
| 7 |
-
from queue import Queue, Empty
|
| 8 |
-
import time
|
| 9 |
import gradio as gr
|
| 10 |
from gradio_webrtc import WebRTC
|
| 11 |
-
from
|
| 12 |
-
from inference import YOLOv10
|
| 13 |
-
|
| 14 |
-
model_file = hf_hub_download(
|
| 15 |
-
repo_id="onnx-community/yolov10n", filename="onnx/model.onnx"
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
model
|
|
|
|
| 19 |
|
| 20 |
def detection(image, conf_threshold=0.3):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
css = """.my-group {max-width: 600px !important; max-height: 600px !important;}
|
| 26 |
.my-column {display: flex !important; justify-content: center !important; align-items: center !important;}"""
|
|
@@ -29,7 +31,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 29 |
gr.HTML(
|
| 30 |
"""
|
| 31 |
<h1 style='text-align: center'>
|
| 32 |
-
|
| 33 |
</h1>
|
| 34 |
"""
|
| 35 |
)
|
|
@@ -55,4 +57,4 @@ if __name__ == "__main__":
|
|
| 55 |
server_port=7860,
|
| 56 |
show_error=True,
|
| 57 |
debug=True
|
| 58 |
-
)
|
|
|
|
|
|
|
| 1 |
import cv2
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from gradio_webrtc import WebRTC
|
| 6 |
+
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load the YOLOv5s-nano model (Make sure it's available or downloaded via CLI or path)
|
| 9 |
+
model = YOLO("yolov5sn.pt") # You can also use a local path
|
| 10 |
|
| 11 |
def detection(image, conf_threshold=0.3):
|
| 12 |
+
# Convert RGB to BGR (OpenCV uses BGR)
|
| 13 |
+
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 14 |
+
|
| 15 |
+
# Inference with confidence threshold
|
| 16 |
+
results = model(image_bgr, conf=conf_threshold)
|
| 17 |
+
|
| 18 |
+
# Draw results on image
|
| 19 |
+
annotated = results[0].plot() # This returns a numpy array with drawings
|
| 20 |
+
|
| 21 |
+
# Convert back to RGB for display
|
| 22 |
+
return cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
|
| 23 |
+
|
| 24 |
+
# Optional: Add your custom RTC config if needed
|
| 25 |
+
rtc_configuration = {}
|
| 26 |
|
| 27 |
css = """.my-group {max-width: 600px !important; max-height: 600px !important;}
|
| 28 |
.my-column {display: flex !important; justify-content: center !important; align-items: center !important;}"""
|
|
|
|
| 31 |
gr.HTML(
|
| 32 |
"""
|
| 33 |
<h1 style='text-align: center'>
|
| 34 |
+
YOLOv5sn Webcam Stream (Powered by WebRTC ⚡️)
|
| 35 |
</h1>
|
| 36 |
"""
|
| 37 |
)
|
|
|
|
| 57 |
server_port=7860,
|
| 58 |
show_error=True,
|
| 59 |
debug=True
|
| 60 |
+
)
|