use cv2.VideoCapture to capture a picture - python

I want to use my laptop webcam to capture a picture using the code below:
import cv2
cap = cv2.VideoCapture(1)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
but it throws this error:
cv2.imshow('frame', frame) cv2.error: OpenCV(4.0.0)
C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350:
error: (-215:Assertion failed) size.width>0 && size.height>0 in
function 'cv::imshow'
How can I fix this error?

When OpenCV has problem to get frame from camera or stream then it doesn't raise error but it return False in ret (return status) so you should check it. It also return None in frame and imshow has problem to display None - it has no width and height - so you get error with size.width>0 && size.height>0
As I know mostly laptop webcame has number 0, not 1
This works with my laptop webcam
import cv2
cap = cv2.VideoCapture(0) # zero instead of one
while True:
ret, frame = cap.read()
if not ret: # exit loop if there was problem to get frame to display
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
EDIT: as said Dave W. Smith in comment: some laptops may need time to send correct image then here version which doesn't exit loop
while True:
ret, frame = cap.read()
if ret: # display only if status (ret) is True and there is frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

import numpy as np
import cv2
cap = cv2.VideoCapture(0) #it can be one also...but generally zero
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('Capture', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Try this one...it works for mine...make sure numpy is installed

Related

I think my code is correct But it is not working it is with opencv ,i trying to get my camera

import cv2
vid=cv2.VideoCapture(0)
while True:
ret,frame=vid.read()
cv2.imshow('frame',frame)
vid.release()
cv2.destroyAllWindows()
error: (-215:Assertion failed) !_src.empty() in
function 'cv::cvtColor'
Every time you loop this line of code, the loop starts the camera and stops the camera:
vid.release()
cv2.destroyAllWindows()
You should try this code:
import cv2
cap = cv2.VideoCapture()
# The device number might be 0 or 1 depending on the device and the webcam
cap.open(1, cv2.CAP_DSHOW)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

when i try to run this code it collects first image and then camera turns off and frame is not responding

for label in labels:
!mkdir {'Tensorflow\workspace\images\collectedimages\\'+label}
cap = cv2.VideoCapture(0)
print('Collecting images for {}'.format(label))
time.sleep(5)
for imgnum in range(number_imgs):
ret, frame = cap.read()
imgname = os.path.join(IMAGES_PATH, label, label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
cv2.imwrite(imgname, frame)
cv2.imshow('frame', frame)
time.sleep(2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
error : OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
I also faced the same problem.
I fixed it by updating the correct id of video capturing device
cap = cv2.VideoCapture(0)
for me the video caputure device no. was 1

Facecam Video does not open with cv2.VideoCapture() and cannot be read

I'm new to OpenCV and just started learning
I am using macOS, python version 3.8.1
My code:
import cv2
# takes facecam video
cap = cv2.VideoCapture(-1)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if cap.isOpened() == True:
# 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
else:
cap.open(-1)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
This works for me
import cv2
cap = cv2.VideoCapture(0)
while True:
ne, frame = cap.read()
cv2.imshow("Webcam Capture", frame)
if cv2.waitKey(1) & 0xFF == ord("A"):
break
cap.release()
cv2.destroyAllWindows()

Does anyone know how to code for getting a frame using your webcam?

I'm trying to get a frame from my webcam but I don't know where the frame is located.
this is my code:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
if frame is None:
break
cv2.imshow('app', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
In the code that you posted, the frame is the image that is captured from the webcam at that point in time.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
if frame is None:
break
cv2.imshow('app', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
In your program indentation problem in if condition
If you press "q" then the frame is captured, and the frame stored in "frame" variable

I am trying to read a video frame by frame using python , to execute some processes on frames

This is my code
import cv2
video_capture = cv2.VideoCapture("test.mpeg")
cv2.convertMaps
while True:
# get frame by frame
ret, frame = video_capture.read()
cv2.imwrite('pic.png',frame)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
I'm getting the following error:
None
Traceback (most recent call last):
File "D:/Itellingence Transportation Systems/Material-lab8/home_work8.py", line 12, in <module>
cv2.imshow('Video', frame)
error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow
What is the problem?
Simple answer, I believe. Your loop does nothing to detect when the video is done. Eventually the video_capture object will return False when trying to read. You should check for that condition in your loop for a graceful exit.
import cv2
video_capture = cv2.VideoCapture("test.mpeg")
while True:
# get frame by frame
ret, frame = video_capture.read()
if not ret:
break
cv2.imwrite('pic.png',frame)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

Categories