4. Reading IP Cameras: Handling RTSP and Latency
Connecting to an IP camera requires handling network protocols and potential stream disconnections.
4.1 The Connection String
Most industrial cameras follow this URL format. Replace username, password, and IP with your actual credentials.
rtsp://username:[email protected]:554/stream_path
4.2 The "Latency Trap"
OpenCV's default buffer often causes the video to lag behind real-time. To fix this, we often need to clear the buffer manually or use a separate thread for grabbing frames.
💡 Optimization: Use
cv2.CAP_FFMPEG as the backend for better compatibility with H.264/H.265 streams common in IP cameras.
cap = cv2.VideoCapture("rtsp://...", cv2.CAP_FFMPEG)
We will cover multi-threaded acquisition in Chapter 24 to completely eliminate this lag.