4

I want to stream the video from IP camera by using RTSP. But I am having a problem. I have installed prerequirements and also, my RTSP link works on VlC player. But when I tried it in the editor and run it, it says Camera could not be found.
Here is my code.

import cv2
import numpy as np
cap = cv2.VideoCapture("rtsp://admin:admin@xxx.xxx.xxx.xxx:xxx/media/video1/video")

while True:
    ret, img = cap.read()
    if ret == True:
    cv2.imshow('video output', img)
    k = cv2.waitKey(10)& 0xff
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()
evanhutomo
  • 627
  • 1
  • 11
  • 24
Muhammet PARLAK
  • 81
  • 2
  • 2
  • 9
  • We're you able to play videos that are local to your computer? Or just from a webcam? – Shawn Mathew Jul 04 '17 at 13:08
  • @ShawnMathew I want to stream it from Ip camera by RTSP stream – Muhammet PARLAK Jul 04 '17 at 13:11
  • I understand. But I asked whether you can play local videos to ensure that your opencv installation has ffmpeg bindings. If it doesn't, then you won't be able to play any videos and will need to reinstall python using the correct methods – Shawn Mathew Jul 04 '17 at 13:16
  • @ShawnMathew Ive reinstalled python but I didnt check about ffmpeg bindings. Can you advice me something about this binding?Thank you. – Muhammet PARLAK Jul 04 '17 at 13:26
  • @ShawnMathew I dont wanna play videos. All I wanna do is to get the screen what camera sees.So now I dont need ffmpeg right? I tried many times reinstalling python with correct methods but Dont know wheres the problem. I dont get screen or any error. :S – Muhammet PARLAK Jul 04 '17 at 14:35
  • Possible duplicate of [RTSP stream and OpenCV (Python)](https://stackoverflow.com/questions/20891936/rtsp-stream-and-opencv-python) – Ollie Graham Oct 24 '19 at 05:10

2 Answers2

3

Do check your installation of opencv has the capability to open videos. for this try

cap=cv2.VideoCapture(r"path/to/video/file")
ret,img=cap.read()
print ret

If ret is True then your opencv install has the codecs necessary to handle videos and then confirm that the RTSP address is correct.

If ret is False then reinstall opencv using the steps here. I would recommend building opencv from source. But try the pre-built libraries first.

Shawn Mathew
  • 2,198
  • 1
  • 14
  • 31
2

I was able to solve opening an RTSP stream with OpenCV (built with FFMPEG) in Python by setting the following environment variable:

import os
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"

FFMPEG defaults to TCP transport, but some RTSP feeds are UDP, so this sets the correct mode for FFMPEG.

Then use:

cv2.VideoCapture(<stream URI>, cv2.CAP_FFMPEG)

ret, frame = cap.read()

while ret:
    cv2.imshow('frame', frame)
    # do other processing on frame...

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

cap.release()
cv2.destroyAllWindows()

as usual.

Ollie Graham
  • 124
  • 1
  • 8