From b12e4ccb7f11a54a427f47b28c5f35a6ec20cd03 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 15 Feb 2025 22:28:47 +0700 Subject: [PATCH] webcam streamer --- app.py | 23 +++++++++++--------- debug/rtsp_webcam.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 debug/rtsp_webcam.py diff --git a/app.py b/app.py index a6844cb..f0c8266 100644 --- a/app.py +++ b/app.py @@ -66,25 +66,28 @@ async def detect(websocket: WebSocket): # Save data you want to persist across frames in the persistent_data dictionary async def handle_detection(camera_id, stream, frame, websocket, model: YOLO, persistent_data): try: - boxes = [] + highest_conf_box = None + max_conf = -1 + for r in model.track(frame, stream=False, persist=True): for box in r.boxes: - track_id = None - if hasattr(box, "id") and box.id is not None: - track_id = box.id.item() box_cpu = box.cpu() - boxes.append({ - "class": model.names[int(box_cpu.cls[0])], - "confidence": float(box_cpu.conf[0]), - "id": track_id, - }) + conf = float(box_cpu.conf[0]) + if conf > max_conf and hasattr(box, "id") and box.id is not None: + max_conf = conf + highest_conf_box = { + "class": model.names[int(box_cpu.cls[0])], + "confidence": conf, + "id": box.id.item(), + } + # Broadcast to all subscribers of this URL detection_data = { "type": "imageDetection", "cameraIdentifier": camera_id, "timestamp": time.time(), "data": { - "detections": boxes, + "detections": highest_conf_box if highest_conf_box else None, "modelId": stream['modelId'], "modelName": stream['modelName'] } diff --git a/debug/rtsp_webcam.py b/debug/rtsp_webcam.py new file mode 100644 index 0000000..4d9f3ae --- /dev/null +++ b/debug/rtsp_webcam.py @@ -0,0 +1,51 @@ +import cv2 +import gi +import time + +gi.require_version('Gst', '1.0') +from gi.repository import Gst + +# Initialize GStreamer +Gst.init(None) + +# Open the default webcam +cap = cv2.VideoCapture(0) + +# Define the RTSP pipeline using GStreamer +rtsp_pipeline = ( + "appsrc ! videoconvert ! video/x-raw,format=I420 ! x264enc tune=zerolatency bitrate=2048 speed-preset=ultrafast " + "! rtph264pay config-interval=1 pt=96 ! udpsink host=127.0.0.1 port=8554" +) + +# Create GStreamer pipeline +pipeline = Gst.parse_launch(rtsp_pipeline) +appsrc = pipeline.get_by_name("appsrc") + +# Start streaming +pipeline.set_state(Gst.State.PLAYING) +time.sleep(1) + +while cap.isOpened(): + ret, frame = cap.read() + if not ret: + break + + # Convert frame to I420 format (YUV420) + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV_I420) + data = frame.tobytes() + + # Push frame to GStreamer pipeline + buf = Gst.Buffer.new_allocate(None, len(data), None) + buf.fill(0, data) + appsrc.emit("push-buffer", buf) + + # Display frame locally (optional) + cv2.imshow("RTSP Streaming", frame) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +# Cleanup +cap.release() +cv2.destroyAllWindows() +pipeline.set_state(Gst.State.NULL)