webcam streamer

This commit is contained in:
Siwat Sirichai 2025-02-15 22:28:47 +07:00
parent 827b77011d
commit b12e4ccb7f
2 changed files with 64 additions and 10 deletions

23
app.py
View file

@ -66,25 +66,28 @@ async def detect(websocket: WebSocket):
# Save data you want to persist across frames in the persistent_data dictionary # 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): async def handle_detection(camera_id, stream, frame, websocket, model: YOLO, persistent_data):
try: try:
boxes = [] highest_conf_box = None
max_conf = -1
for r in model.track(frame, stream=False, persist=True): for r in model.track(frame, stream=False, persist=True):
for box in r.boxes: 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() box_cpu = box.cpu()
boxes.append({ conf = float(box_cpu.conf[0])
"class": model.names[int(box_cpu.cls[0])], if conf > max_conf and hasattr(box, "id") and box.id is not None:
"confidence": float(box_cpu.conf[0]), max_conf = conf
"id": track_id, highest_conf_box = {
}) "class": model.names[int(box_cpu.cls[0])],
"confidence": conf,
"id": box.id.item(),
}
# Broadcast to all subscribers of this URL # Broadcast to all subscribers of this URL
detection_data = { detection_data = {
"type": "imageDetection", "type": "imageDetection",
"cameraIdentifier": camera_id, "cameraIdentifier": camera_id,
"timestamp": time.time(), "timestamp": time.time(),
"data": { "data": {
"detections": boxes, "detections": highest_conf_box if highest_conf_box else None,
"modelId": stream['modelId'], "modelId": stream['modelId'],
"modelName": stream['modelName'] "modelName": stream['modelName']
} }

51
debug/rtsp_webcam.py Normal file
View file

@ -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)