OpenCV RTSP capture is not opening - python

I trying create capture RTSP stream by my ip camera. It is good working in VLC player, but not work in python 3.6 with OpenCV package.
I run this code:
import cv2
sUrl = 'rtsp://admin:*****#**.***.***.***:554/onvif1'
vcap = cv2.VideoCapture(sUrl)
while(1):
ret, frame = vcap.read()
cv2.imshow('frame', frame)
cv2.waitKey(1)
and get this error:
error
Traceback (most recent call last)
in ()
8 while(1):
9 ret, frame = vcap.read()
---> 10 cv2.imshow('frame', frame)
11 cv2.waitKey(1)
error: OpenCV(3.4.1)
C:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\highgui\src\window.cpp:356:
error: (-215) size.width>0 && size.height>0 in function cv::imshow
after, I check capture
vcap.isOpened()
and I get False value
Python version - 3.6
OS - Windows 10
OpenCV version - 2.4.12
Thank you!

Related

OpenCV cannot find webcam but Cheese can

My opencv installation recently stopped working for reasons I'm not sure of. I have two scripts that all give different errors:
Script A:
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
gives me this error:
select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
File "camera.py", line 19, in <module>
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.error: OpenCV(4.0.1-dev) /home/me/Packages/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
And Script B:
import cv2
def show_webcam(mirror=False):
cam = cv2.VideoCapture(0)
while True:
ret_val, img = cam.read()
if mirror:
img = cv2.flip(img, 1)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
def main():
show_webcam(mirror=True)
gives me this error:
select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
File "camera3.py", line 26, in <module>
main()
File "camera3.py", line 22, in main
show_webcam(mirror=True)
File "camera3.py", line 15, in show_webcam
cv2.imshow('my webcam', img)
cv2.error: OpenCV(4.0.1-dev) /home/david/Packages/opencv/modules/highgui/src/window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
Here's the main issue: I've used OpenCV before and both of those errors usually occur when opencv can't find the webcam. But I do have a webcam attached, and when I open Cheese Webcam Booth it works fine, and takes pictures fine. Is there a way to repair this without reinstalling OpenCV?
I'm on Ubuntu 18.04.
I would review the OpenCV installation, since Script A works just fine by me, on the same version of the library and similar OS (Mint 19).
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
cv2.waitKey(10)
The installation through
pip install opencv-python
is known for having "problems" with 3rd party modules (https://github.com/opencv/opencv/issues/8471).
Try building and installing from source, this way the interface modules will be built and linked as well (V4L, FFMpeg, etc).
Two possible solutions: 1) set the correct fps; 2) upgrade to the newer version of opencv for python.
in both cases, the camera was activated but is now occupied. So now, in the main "while" loop try putting
while(True):
ret_val, img = cap.read()
if(ret_val==False)
cap.open(0)
continue
...

Opencv "select timeout" error with camera_capture on vm Ubuntu 16.04

I am using Ubuntu 16.04 on vm player. I have written a script to connect laptop's webcam. But, I got select timeout error with ret, frame = video_capture.read(), although I selected camera from "removable devices" section.
When I tried to run script on windows, it was successful.
code: read_video.py
import numpy as np
import cv2
video_capture = cv2.VideoCapture(0)
while(True):
ret, frame = video_capture.read()
cv2.imshow("Frame",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
error:
read_video.py
select timeout
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != __null) in Mat, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/include/opencv2/core/mat.inl.hpp, line 500
Traceback (most recent call last):
File "read_video.py", line 9, in <module>
ret, frame = video_capture.read()
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/include/opencv2/core/mat.inl.hpp:500: error: (-215) total() == 0 || data != __null in function Mat
How can I solve it?

Resizing video resolution in OpenCV python

I want to reduce the resolution of video that I am getting from my webcam to half(i.e from 640x480 to 320x240) but I am getting error.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,320)
cap.set(4,240)
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('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
The error that I am getting is
Traceback (most recent call last): File
"C:\Users\Nemi\Desktop\bbb.py", line 17, in
cv2.imshow('frame',frame) error: ......\opencv-2.4.13.2\modules\highgui\src\window.cpp:269: error:
(-215) size.width>0 && size.height>0 in function cv::imshow
I am new to this and could not find the solution. Could someone please tell me what am I doing wrong?

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow

I am running the following code on Raspberry Pi with pi camera, I have the broadcom drivers for it and all, but I am getting an error. Perhaps something to do with the dimensions of the video feed, but I do not know how to set it on Linux.
Code:
import cv2
import numpy as np
cap = cv2.VideoCapture()
while True:
ret, img = cap.read()
cv2.imshow('img', img)
if cv2.waitKey(0) & 0xFF == ord('q):
break
Error:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow,
file /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp, line 325
Traceback (most recent call last):
File "check_picam_with_opencv.py", line 10, in <module>
cv2.imshow('img', img)
cv2.error: /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp:325: error:
(-215) size.width>0 && size.height>0 in function imshow
Provide an id to VideoCapture.
cap = cv2.VideoCapture(0)
Also check the value of ret, see if it's TRUE or FALSE
print (ret)
Edit:
To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera.
cap = cv2.VideoCapture(0)
To check whether the cap has been initialized or not, you can use cap.isOpened() function, which returns True for successful initialization and False for failure.
if cap.isOpened() == False:
print ("VideoCapture failed")
cap.read() returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.
ret, frame = cap.read()
if ret == False:
print("Frame is empty")
Further reading here.

VideoCapture doesn't seem to work on Debian - opencv 3.2

Videocapture demo
import numpy as np import cv2
cap = cv2.VideoCapture(0)
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('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
The video capture demo ( opencv 3.2.0 ), on my Debian computer, does not work, and I do not understand how to fix it. I tried it on windows and it worked perfectly.
I tried both with VideoCapture (-1) VideoCapture (0)
But it doesn't seem to work..
How can I fix it? thank you
OpenCV Error: Assertion failed (scn ==
3 || scn == 4) in cvtColor, file
/io/opencv/modules/imgproc/src/color.cpp,
line 9748 Traceback (most recent call
last): File "ok.py", line 11, in
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error:
/io/opencv/modules/imgproc/src/color.cpp:9748:
error: (-215) scn == 3 || scn == 4 in
function cvtColor
$ v4l2-ctl --list-devices
UVC Camera (046d:0809)(usb-0000:00:14.0-3): /dev/video1
USB2.0 HD UVC WebCam (usb-0000:00:14.0-5): /dev/video0
i tried
ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 output.mkv
and webcam works..
Why is it VideoCapture(1)? Do you have two cameras on your system?
You tested
ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 output.mkv
on /dev/video0 so it should be VideoCapture(0).

Categories