Access IP Camera In Python OpenCV


Answer :

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:password@192.168.1.64/1') 

This works with my IP camera:

import cv2  #print("Before URL") cap = cv2.VideoCapture('rtsp://admin:123456@192.168.1.216/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@)


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('http://192.168.18.37:8090/video')  while(True):     ret, frame = cap.read()     cv2.imshow('frame',frame)     if cv2.waitKey(1) & 0xFF == ord('q'):         cv2.destroyAllWindows()         break 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable