3

Right now I am using a web cam and it works perfectly fine with the code below -:

capture = cv2.VideoCapture(0)

Now instead of web cam, I want to use the ip camera(https://192.168.0.60) What would be the easiest way to do it with OpenCV(Python)?

I saw a bunch of posts, but couldn't find a straight answer to this. Can someone help, who already got it running?

Thank you!

1
  • I think the best way is to get the rtsp link from your camera and just pass as the parameter to VideoCapture... ASFAIK you should have either ffmpeg or gstreamer compiled opencv for it to work :) Commented Apr 27, 2018 at 9:01

4 Answers 4

4

Firstly, you must find the exact url for your video stream, and that's best done with a web browser. For example I use IP Webcam app on android (com.pass.webcam) and it's stream will be on:

http://phone-ip-address:port/video

If I visit that url with a web browser, I can see the live stream. Make sure, that what you see is only the video stream, not a html page with the stream. If there is a html page, you can right-click and select Open image in new tab (in Chrome) to get to the stream.

However it looks like OpenCV can only read the video stream if the filename/url has the right suffix. Adding ?type=some.mjpeg worked for me. So the url would be:

http://phone-ip-address:port/video?type=some.mjpeg

Try visiting such an url in the web browser before you go for opencv.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to add my 2 cents, not all cameras have mjpeg (like som dlink models), but the same idea explained here works with rtsp/rtmp ( just remember to change the protocol if this is the case http:// -> rtsp:// or rtmp://. Andddd the protocol is important (or at least it was the last time i checked) even if it is http://
3

Take a look at this example with python, OpenCV, IPCAM and hikvision

import numpy as np 
import cv2

cap = cv2.VideoCapture() 
cap.open("rtsp://USER:PASS@IP:PORT/Streaming/Channels/2")

while(True):
     # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('Salida',frame)

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

# When everything done, release the capture 
cap.release()
cv2.destroyAllWindows()

Image: Get video from IPCAM with python and OpenCV

Comments

0

Here you go,

import numpy as np
import cv2

cap = cv2.VideoCapture('rtsp://<username_of_camera>:<password_of_camera@<ip_address_of_camera')

while(True):

    ret, frame = cap.read()
    cv2.imshow('Stream IP Camera OpenCV',frame)

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

cap.release()
cv2.destroyAllWindows()

For example:

import numpy as np
import cv2

cap = cv2.VideoCapture('rtsp://admin:[email protected]')

while(True):

    ret, frame = cap.read()
    cv2.imshow('Stream IP camera opencv',frame)

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

cap.release()
cv2.destroyAllWindows()

Then save the file as camera.py (.py), go to command prompt or terminal, locate file and type python camera.py or python <file_name>.py enter to run script.
If you want to exit from script windows just press "q" or close cmd.

Hope this helpful.

Comments

0

Here is an example for a Vivotek IP8136W IP Camera. It does not support streaming.

This code continuously grabs still frames, at about 2fps. No observed CPU loading.

import numpy as np
import cv2

# for webcams, request stream only once.
#cap = cv2.VideoCapture(0)
    
while(True):

    # For single image IP cams, request frame every time.
    cap = cv2.VideoCapture("http://root:[email protected]/cgi-bin/viewer/video.jpg")

    ret, frame = cap.read()
   
    # Display the resulting frame
    if ret:
        cv2.imshow('camera',frame)
    else:
        print("error getting frame.")

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

# Done. release the capture
cap.release()
cv2.destroyAllWindows()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.