Access IP Camera in Python OpenCV

Posted on

Question :

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)

    ret, frame =
    if cv2.waitKey(1) & 0xFF == ord('q'):


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

Help will be highly appreciated

You can use video capture Object as

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


Answer #1:

An IP camera can be accessed in opencv by providing the streaming URL of the camera in the constructor of cv2.VideoCapture.

Usually, RTSP or HTTP protocol is used by the camera to stream video.
An example of IP camera streaming URL is as follows:


It can be opened with OpenCV like this:

capture = cv2.VideoCapture('rtsp://')

Most of the IP cameras have a username and password to access the video. In such case, the credentials have to be provided in the streaming URL as follows:

capture = cv2.VideoCapture('rtsp://username:password@')
Answered By: sgarizvi

Answer #2:

This works with my IP camera:

import cv2

#print("Before URL")
cap = cv2.VideoCapture('rtsp://admin:123456@')
#print("After URL")

while True:

    #print('About to start the Read command')
    ret, frame =
    #print('About to show frame of Video.')

    if cv2.waitKey(1) & 0xFF == ord('q'):


I found the Stream URL in the Camera’s Setup screen:
IP Camera Setup Screen

Note that I added the Username (admin) and Password (123456) of the camera and ended it with an @ symbol before the IP address in the URL (admin:123456@)

Answered By: John Harris

Answer #3:

The easiest way to stream video via IP Camera !

I just edit your example. You must replace your IP and add /video on your link. And go ahead with your project

import cv2

cap = cv2.VideoCapture('')

    ret, frame =
    if cv2.waitKey(1) & 0xFF == ord('q'):
Answered By: ToanJunifer

Answer #4:

First find out your IP camera’s streaming url, like whether it’s RTSP/HTTP etc.

Code changes will be as follows:

cap = cv2.VideoCapture("ipcam_streaming_url")

For example:

cap = cv2.VideoCapture("")

Answer #5:

To access an Ip Camera, first, I recommend you to install it like you are going to use for the standard application, without any code, using normal software.

After this, you have to know that for different cameras, we have different codes. There is a website where you can see what code you can use to access them:

But be careful, for my camera (Intelbras S3020) it does not work. The right way is to ask the company of your camera, and if they are a good company they will provide it.

When you know your code just add it like:

cap = cv2.VideoCapture("http://LOGIN:PASSWORD@IP/cgi-bin/mjpg/video.cgi?&subtype=1")

Instead LOGIN you will put your login, and instead PASSWORD you will put your password.

To find out camera’s IP address there is many softwares that you can download and provide the Ip address to you. I use the software from Intelbras, but I also recommend EseeCloud because they work for almost all cameras that I’ve bought:

In this example, it shows the protocol http to access the Ip camera, but you can also use rstp, it depends on the camera, as I said.

If you have any further questions just let me know.

Answered By: Gustavo GeoDrones

Answer #6:

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 =
    cv2.imshow('IP Camera stream',f)

    if cv2.waitKey(1) & 0xFF == ord('q'):


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.

Answered By: Employee

Answer #7:

You can access most IP cameras using the method below.

import cv2 

# insert the HTTP(S)/RSTP feed from the camera
url = "http://username:password@your_ip:your_port/tmpfs/auto.jpg"

# open the feed
cap = cv2.VideoCapture(url)

while True:
    # read next frame
     ret, frame =
    # show frame to user
     cv2.imshow('frame', frame)
    # if user presses q quit program
     if cv2.waitKey(1) & 0xFF == ord("q"):

# close the connection and close all windows
Answered By: Dinusha Dilinka

Answer #8:

For getting the IP Camera video link:

  1. Open the IP Camera with given IP and PORT in browser
  2. Right click the video and select “copy image address”
  3. Use that address to capture video
Answered By: Garry

Leave a Reply

Your email address will not be published. Required fields are marked *