Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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") 
like image 363
Employee Avatar asked Apr 23 '18 10:04

Employee


People also ask

How do I access my IP camera in Python?

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.


2 Answers

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:

rtsp://192.168.1.64/1

It can be opened with OpenCV like this:

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

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:[email protected]/1') 
like image 148
sgarizvi Avatar answered Sep 16 '22 19:09

sgarizvi


This works with my IP camera:

import cv2  #print("Before URL") cap = cv2.VideoCapture('rtsp://admin:[email protected]/H264?ch=1&subtype=0') #print("After URL")  while True:      #print('About to start the Read command')     ret, frame = cap.read()     #print('About to show frame of Video.')     cv2.imshow("Capturing",frame)     #print('Running..')      if cv2.waitKey(1) & 0xFF == ord('q'):         break  cap.release() cv2.destroyAllWindows() 

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

like image 20
John Harris Avatar answered Sep 19 '22 19:09

John Harris