Search code examples
pythonopencvip-camera

Access IP Camera in Python OpenCV


How do I access my IP Camera stream?

Code for displaying a standard webcam stream is

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

How do I do the same exact thing but with the IP Camera?

My system:

  • Python 2.7.14
  • OpenCV 2.4.9
  • Teledyne Dalsa Genie Nano XL Camera

You can use video capture Object as

camera = cv2.VideoCapture("IP:PORT/video")

Solution

  • I answer my own question reporting what therefore seems to be the most comprehensive overall procedure to Access IP Camera in Python OpenCV.

    Given an IP camera:

    • Find your camera IP address
    • Find the port where the IP address is accessed
    • Find the protocol (HTTP/RTSP etc.) specified by the camera provider

    Then, if your camera is protected go ahead and find out:

    • your username
    • your password

    Then use your data to run the following script:

    """Access IP Camera in Python OpenCV"""
    
    import cv2
    
    stream = cv2.VideoCapture('protocol://IP:port/1')
    
    # Use the next line if your camera has a username and password
    # stream = cv2.VideoCapture('protocol://username:password@IP:port/1')  
    
    while True:
    
        r, f = stream.read()
        cv2.imshow('IP Camera stream',f)
    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cv2.destroyAllWindows()
    

    NOTE: In my original question I specify to being working with Teledyne Dalsa Genie Nano XL Camera. Unfortunately for this kind of cameras this normal way of accessing the IP Camera video stream does not work and the Sapera SDK must be employed in order to grab frames from the device.