cv::VideoCapture::open VIDEOIO(DSHOW) - python

I have been working on this error for a while but can't resolve it.
This is my code
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(0)
time.sleep(2)
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
And this is the error it is giving me.
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (193) cv::VideoCapture::open VIDEOIO(DSHOW): raised unknown C++ exception!
Traceback (most recent call last):
File "C:\Users\VVA\Desktop\test.py", line 10, in
cv2.imshow('webcam', frame)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

just use different index number

On my macbook the reason this happens seem to be that the internal webcam is index number 1, whereas the index number 0 seems to be the "camtwist" webcam driver.
The answer "just use a different index number" is correct. However it's not very satisfying - if you want to write portable code, what is the way to determine the correct index number of the internal webcam of the device? Better do a loop through all devices?

Related

Python: cv2 can't open USB camera. "error: (-215:Assertion failed)"

I'd like to use cv2 with a Desktop PC that I build myself. I've bought a USB webcamera and successufuly installed it since it works smoothly when I access it. My probem is that it seems that cv2 is not able to open my camera. This is the error I'm getting:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor
So I've tried using various index (from -1 to 5) in this line of code:
cap = cv2.VideoCapture(0)
But nothing changed, I've also tried to use:
cd /dev
ls video
But this is the error I'm getting:
ls: cannot access 'video': No such file or directory
Is there a way to fix this problem?
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
Before this line of code, did you also write something like cv2.imread(...)? I experienced the error exactly the same with yours when I mistakenly put a wrong image address in the cv2.imread(), so my advice is to double check if you pass a correct image address if there is any. Best:)

Error while trying to access phone camera with opencv python

I expected to receive a video feed in my laptop from my phone. In my opinion, the code is correct. The same code is running on other devices. But when I tried doing the same in my laptop, it displayed the following error:
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap.cpp (142) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): http://192.168.1.3:8080/video in function 'cv::icvExtractPattern'
Since I am a newbie to the world of opencv, I could'nt understand what caused the error. Hence, I did'nt try anything worthwhile other than changing a few lines of code.
My code is:
import cv2
url= 'http://192.168.1.3:8080/video'
cap=cv2.VideoCapture(url)
cap.set(3,240)
cap.set(4,240)
while(True):
ret,frame=cap.read()
if frame is not None:
cv2.imshow('frame',frame)
q=cv2.waitKey(1)
if q==ord("q"):
break
cv2.destroyAllWindows()
cap.release()
I had encountered the same issue but with a a saved video file as the input.
The file path I had entered did no exist and I got the same error.
I would suggest to check if your have given the right permissions to for accessing your phone's camera and if the feed is live and is returning images in the correct format.

Error: (-215:Assertion failed) !ssize.empty() in function 'resize'

I am getting the error:
Traceback (most recent call last):
File "Gesture Control Video Player using OepnCV.py", line 51, in <module>
frame = cv2.resize(frame,(500,700))
cv2.error: OpenCV(3.4.11) /tmp/pip-req-build-a3rwegmg/opencv/modules/imgproc/src/resize.cpp:3929: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
How can I fix this?
This assertion error in OpenCV means that frame variable is empty. If you are displaying an image, this can be caused by the file path to the image being incorrect or the file being corrupted. If you are displaying a video input, you might have setup the frame capture from the camera incorrectly. There are other ways to getting this assertion error, but these are the most likely. Make sure that they aren't occurring in your code.

Second video source /dev/video1 not working in python ubuntu

I am trying to use epiphan SDI2USB with ubuntu 18.04. I downloaded the correct driver from their support site and the driver is correctly installed. My kernel version is 4.15.0.
If I try to run vlc and i go to media menu --> Open Capture Device --> video device name --> Here I see both my /dev/video0 (integrated camera) and /dev/video1 (epiphan video) When I select /dev/video1 and click play, I can see it works fine.
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()
However when I write a simple python program from internet (i changed source from 0 to 1) and run it, I get an error
compass-admin#Alienware-13-R3:~$ sudo python3 cam.py
Insufficient buffer memory on /dev/video1 – decreasing buffers
Insufficient buffer memory on /dev/video1 – decreasing buffers
mmap: Invalid argument
munmap: Invalid argument
VIDEOIO ERROR: V4L: can’t open camera by index 1
munmap: Invalid argument
Traceback (most recent call last):
File “/home/compass-admin/.local/lib/python3.6/site-packages/numpy/lib/shape_base.py”, line 843, in split
len(indices_or_sections)
TypeError: object of type ‘int’ has no len()
I dont know if it is because of permissions or if you have to use a specific function in opencv. No matter what I do, I get the same error.
I changed permissions of /dev/video1 to 777, I also added the current user to video group but no luck. I also tried video source in opencv as 0, 1, -1 but no luck. Any help please.

Error in reading image through OpenCV in Python

I am getting issues in reading an image using opencv in Python.Can someone please help??
What I had done??
Installed opencv in Anaconda and using jupyter notebook for running the code.I am using python3. Below is the code:
img =cv2.IMREAD("C:/Users/u266772/Desktop/Video/maddy.jpg",cv2.IMREAD.GRAYSCALE)
cv2.imshow ('image',img)
cv2.waitkey(0)
cv2.destroyAllWindows()
The error:
AttributeError Traceback (most recent call
last) in ()
----> 1 img = cv2.IMREAD("C:/Users/u266772/Desktop/Video/maddy.jpg",cv2.IMREAD.GRAYSCALE)
2
3 cv2.imshow ('image',img)
4 cv2.waitkey(0)
5 cv2.destroyAllWindows()
AttributeError: module 'cv2' has no attribute 'IMREAD'
The correct usage is
img=cv2.imread('4.jpg',cv2.IMREAD_GRAYSCALE)
the imread function is lowercase. And its IMREAD_GRAYSCALE not '.'
Python is a case sensitive language.you used IMREAD function which is not a part of opencv. But for reading images you should use imread function.because of this you got that error.
AttributeError: module 'cv2' has no attribute 'IMREAD'.
And the last thing always see the type of error.and google it .after that you should post questions on forums to get an answer.good luck!!!!
For 64 bit machines, cv2.waitkey(0) will throw an error.
Instead, you should use
cv2.waitKey(0) & 0xFF
The below code is working fine now:
img = cv2.imread("C:/Users/u266772/Desktop/Video/photo1.jpg",1)
cv2.imshow ('image',img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()

Categories

Resources