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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.