Reputation: 21082
I copied code from https://stackoverflow.com/a/34588758/210342 and used with default (built-in) camera, it worked. Then I attached USB camera, tested it with VLC and changed the code to open camera 1
:
cam = cv2.VideoCapture(1)
I check whether the camera is open cam.isOpened()
-- it is -- but the camera is not enabled (its hardware indicator, LED, is off) and indeed all I see on the screen is black frame.
Is there some extra special code to add in order to enable USB camera?
Upvotes: 14
Views: 60127
Reputation: 57
I just tried cv2.VideoCapture(n) with n from 0 to 4. For me worked out with cv2.VideoCapture(4)
I realized from the previous posts that by iterating in a small range you probably will get the correct id in a few attempts
Upvotes: 0
Reputation: 1438
In my case the LED indicator turned on, but there was read returned none. Somehow the solution was to call call cv2.VideoCapture again.
cap = cv2.VideoCapture(1)
_ = cv2.VideoCapture(1)
Upvotes: 0
Reputation: 6661
I do not know why but on my laptop (Acer Aspire 3) the usb webcam works with python opencv only if I plug it in the right side usb of my laptop and NOT if I plug it in the left side usb. So try plugging the webcam on all the usb ports you have. (I also had to use cam = cv2.VideoCapture(2)
as @Slayahh suggested.
Upvotes: 2
Reputation: 2279
in accordance to the accepted answer and this https://stackoverflow.com/a/60603969/4451944
i realized cv2.VideoCapture(4)
the parameter 4 is directly proportional to the file suffix of /dev/video4
Upvotes: 0
Reputation: 393
I ran into the same problem, turns out sometimes the webcam can take both slots 0 and 1.
So cam = cv2.VideoCapture(2)
worked for me. This was found using the cd /dev
-method above.
Upvotes: 7
Reputation: 171
You can also refer this link here
Here he changes the line below to
cap = cv2.VideoCapture("/dev/video1") # check this
Before plugging in the camera, go to your terminal home
cd /dev
ls video
and then press tab, if you find only result as video0
, that means only webcam is present.video1
or video2
when you repeat the steps. Upvotes: 16
Reputation: 2814
Are you sure the usb camera is camera 1, i've done this before and had to use cv2.VideoCapture(0)
Upvotes: 6