I'm trying to capture a video from my pixy2 camera.
I wrote this code:
import cv2 as cv
vid = cv.VideoCapture(1, cv.CAP_DSHOW)
while (True):
ret, frame = vid.read()
cv.imshow('frame', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv.destroyAllWindows()
and I'm getting this error:
"C:\Users\User\PycharmProjects\OpenCV tutorial\venv\Scripts\python.exe" "C:/Users/User/PycharmProjects/OpenCV tutorial/OpenCV_1.py"
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\OpenCV tutorial\OpenCV_1.py", line 14, in <module>
cv.imshow('frame', frame)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
Process finished with exit code 1
Any help, please!!
actually this error doesn't base on your videocapture operation. -As properly u set the capture parameter as 1 -
Here are two tips that will you help to determine the reason behind this error;
First use this line right after vid = cv.VideoCapture(1, cv.CAP_DSHOW) this line on your code:
vid not cap.isOpened(): print("Cannot open camera") exit()
This code helps you to understand whether your external camera is on or off.
The second tip is about the reading stage.
if not ret: print("Can't receive frame (stream end?). Exiting ...") break
You will get the error at 1.st step or 2.nd step then, yes!
By the way Welcome :)
Related
I am developing a simple program which loads an external camera through USB connection. When I run the program for the first time, it loads the camera and executes the rest of the code successfully. But if I stop the execution and try to re-run the program, it doesn't load the camera. Yet, if I remove the USB cable and plug it again, the program runs perfectly (The error still occurs if I re-run the program)
Below is my implementation,
import cv2 as cv
import pytesseract
import imutils
capture = cv.VideoCapture(0)
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
temperature = 0
tot = 0
for i in range(10):
_, frame = capture.read()
frame = imutils.resize(image=frame, width=500)
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
frame_gray = frame_gray[:50, :88]
cv.imshow('frame', frame)
try:
temperature = float(pytesseract.image_to_string(frame_gray))
tot = tot + temperature
except ValueError:
print('except')
cv.imshow('Frame', frame_gray)
if cv.waitKey(20) & 0xFF == ord('q'):
break
print(tot / 10)
capture.release()
cv.destroyAllWindows()
And below is the generated error when I run the program for the second time
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-5rb_9df3\opencv\modules\videoio\src\cap_msmf.cpp (372) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -2147023901
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-5rb_9df3\opencv\modules\videoio\src\cap_msmf.cpp (384) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -2147023901
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-5rb_9df3\opencv\modules\videoio\src\cap_msmf.cpp (912) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147023901
Traceback (most recent call last):
File "D:\Studies\OpenCV\Lab07\Lab.py", line 15, in <module>
frame = imutils.resize(image=frame, width=500)
File "C:\Python39\lib\site-packages\imutils\convenience.py", line 69, in resize
(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-5rb_9df3\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
So basically I have to plug out and plug in the USB cable in order to re-run the program. How can I solve this issue? Thanks in advance!
in first time try to use the method cv.VideoCapture.open and check that it returns True or in the same principle use cv.VideoCapture.isOpened
using open() method call release() before opening a new device
capture = cv.VideoCapture()
capture.open(0)
if not capture.IsOpened() :
...
...
capture.release()
warning be sure to call release() before exit the script
the problem can also come from your camera, check with another one
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
...
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?
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?
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.