python webcam access opencv

Viewed 45
import mediapipe as mp
import time
import cv2
cap=cv2.VideoCapture(1)
while True:
    SUCCESS, img = cap.read()
    cv2.imshow("Image",img)
    cv2.waitKey(1)
python -u "/home/luca/hopopa/ai.py"
[ WARN:0@0.358] global /io/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
Traceback (most recent call last):
  File "/home/luca/hopopa/ai.py", line 8, in <module>
    cv2.imshow("Image",img)
cv2.error: OpenCV(4.6.0) /io/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
1 Answers

It seems like you're attempting to read from a webcam that doesn't exist, try changing cv2.VideoCapture(1) to cv2.VideoCapture(0) As explained in the docs the int parameter (index) represents the "id of the video capturing device to open. To open default camera using default backend just pass 0."

Related