অধ্যায়Phase 7 · প্রোডাকশন ও ডিপ্লয়মেন্ট
7.6 20 মিনিট পড়া

Streaming Pipelines

RTSP, GStreamer, WebRTC।

🎬 গল্প দিয়ে শুরু
২০টি CCTV camera, প্রতিটি 25 FPS, একসাথে detection ও alerting। ফাইল-ভিত্তিক approach ভেঙে পড়বে। দরকার streaming pipeline — RTSP, GStreamer, WebRTC দিয়ে।

Protocol overview

  • RTSP — IP camera-এর default (TCP/UDP, low setup overhead)।
  • RTMP — legacy live streaming (YouTube/Twitch ingest)।
  • HLS / DASH — HTTP chunk, browser-friendly, 5–30 s latency।
  • WebRTC — sub-second, peer-to-peer, browser native।
  • SRT — RTMP-এর modern বিকল্প, lossy network-এ robust।

OpenCV দিয়ে RTSP ingest

python
import cv2
url = "rtsp://user:pass@192.168.1.50:554/stream1"
cap = cv2.VideoCapture(url, cv2.CAP_FFMPEG)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)        # latency কমান
while True:
    ok, f = cap.read()
    if not ok: break
    # detection ...
OpenCV-র সীমা
OpenCV decode CPU-তে করে — ১০+ stream হলে CPU বসে যায়। তখন GStreamer বা DeepStream ব্যবহার করুন (NVDEC দিয়ে GPU decode)।

GStreamer pipeline (GPU decode)

bash
gst-launch-1.0 rtspsrc location=rtsp://... latency=100 ! \
    rtph264depay ! h264parse ! nvv4l2decoder ! \
    nvvideoconvert ! 'video/x-raw,format=BGRx' ! \
    appsink
python
pipe = ("rtspsrc location={} latency=100 ! rtph264depay ! "
        "h264parse ! nvv4l2decoder ! nvvideoconvert ! "
        "video/x-raw,format=BGRx ! videoconvert ! "
        "video/x-raw,format=BGR ! appsink").format(url)
cap = cv2.VideoCapture(pipe, cv2.CAP_GSTREAMER)

NVIDIA DeepStream — multi-stream-এর powerhouse

  • এক Jetson-এ ৩০+ 1080p stream একসাথে।
  • Decode → preprocess → inference → tracker → OSD → encode — সব GPU-তে।
  • Config-driven (no-code) ও Python/C++ binding।
  • Built-in TensorRT integration।

Producer-Consumer architecture

python
import threading, queue, cv2
Q = queue.Queue(maxsize=2)

def producer():
    cap = cv2.VideoCapture(url)
    while True:
        ok, f = cap.read()
        if not ok: break
        if Q.full(): Q.get_nowait()   # drop old frame
        Q.put(f)

def consumer():
    while True:
        f = Q.get()
        run_inference(f)

threading.Thread(target=producer, daemon=True).start()
consumer()
Frame drop strategy
Real-time-এ পুরনো frame fresh-এর চেয়ে কম মূল্যবান। Queue full হলে নতুন রাখুন, পুরনো ফেলে দিন।

Output — browser-এ live দেখানো

  • MJPEG over HTTP — সহজ, FastAPI StreamingResponse (7.1)।
  • HLS — ffmpeg দিয়ে .m3u8 + .ts chunk, CDN-friendly।
  • WebRTC — aiortc / mediasoup, sub-second latency।
  • RTMP push → YouTube Live, Facebook Live।
bash
# Processed frames → RTMP server
ffmpeg -f rawvideo -pix_fmt bgr24 -s 1280x720 -r 25 -i - \
       -c:v libx264 -preset veryfast -tune zerolatency \
       -f flv rtmp://server/live/stream

Production checklist

  • Camera reconnect logic — network drop-এ auto retry।
  • Watchdog — frozen stream detect (timestamp gap)।
  • Metrics — Prometheus + Grafana (FPS, latency, drop rate)।
  • Storage — event-triggered clip save (S3/MinIO)।
  • Alert — Kafka/Redis pub-sub → notification service।

Phase 7 শেষ — কী শিখলেন?

  • FastAPI দিয়ে CV model serving।
  • Docker — CPU ও GPU container, multi-stage, compose।
  • ONNX — framework-independent format ও runtime।
  • TensorRT — production-grade GPU inference।
  • CUDA / AMP / torch.compile — GPU optimization।
  • Streaming pipeline — RTSP, GStreamer, WebRTC, DeepStream।
পরবর্তী — Phase 8: Real-World Projects
সব tools আপনার হাতে। এখন end-to-end industry-grade ৬টি project বানাবো — face attendance থেকে smart CCTV পর্যন্ত।
প্র্যাকটিস টাস্ক
  1. RTSP IP camera (বা VLC RTSP server) থেকে stream নিয়ে detection চালান।
  2. GStreamer GPU decode + YOLO pipeline-এ ৪টি stream একসাথে চালান।
  3. Processed video FastAPI দিয়ে browser-এ MJPEG live দেখান।