How to make USB camera work with OpenCV?
Asked Answered
C

7

14

I copied code from https://mcmap.net/q/325821/-python-how-to-capture-image-from-webcam-on-click-using-opencv 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?

Coonhound answered 26/8, 2018 at 18:37 Comment(3)
Are you sure the usb camera is camera 1, i've done this before and had to use cv2.VideoCapture(0)Teeny
@Stanley, big THANK YOU! :-) I could spend no matter how much time and I wouldn't guess the numbers are shuffled after attaching USB camera. Could you please post your comment as regular answer?Coonhound
Thank you, it's almost an "honour" for me as a self taught programmer to be able to help experienced programmers! goes to show that everyone can learn something from anyone. Again, thanks! :)Teeny
T
6

Are you sure the usb camera is camera 1, i've done this before and had to use cv2.VideoCapture(0)

Teeny answered 26/8, 2018 at 19:21 Comment(0)
S
16

You can also refer this link here

https://devtalk.nvidia.com/default/topic/1027250/how-to-use-usb-webcam-in-jetson-tx2-with-python-and-opencv-/

Here he changes the line below to

cap = cv2.VideoCapture("/dev/video1") # check this

Before plugging in the camera, go to your terminal home

  1. Type cd /dev
  2. Type ls video and then press tab, if you find only result as video0, that means only webcam is present.
  3. Now repeat 1 to 2 with USB webcam plugged in. You should find video1 or video2 when you repeat the steps.
Sacchariferous answered 9/3, 2020 at 15:43 Comment(1)
The post above specifically calls out that this is for Windows (via the tag). The path /dev/video1 is a Linux device path. This doesn't exist in the same way on Windows.Fellmonger
T
6

Are you sure the usb camera is camera 1, i've done this before and had to use cv2.VideoCapture(0)

Teeny answered 26/8, 2018 at 19:21 Comment(0)
G
6

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.

Gallon answered 12/5, 2021 at 9:21 Comment(0)
H
2

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.

Homochromatic answered 6/12, 2021 at 23:54 Comment(0)
E
0

in accordance to the accepted answer and this https://mcmap.net/q/800294/-how-to-make-usb-camera-work-with-opencv

i realized cv2.VideoCapture(4) the parameter 4 is directly proportional to the file suffix of /dev/video4

Eure answered 19/11, 2021 at 2:17 Comment(0)
C
0

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)
Casuistry answered 31/10, 2023 at 15:0 Comment(0)
S
0

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

Stolzer answered 18/4, 2024 at 1:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.