greenoldman
greenoldman

Reputation: 21082

How to make USB camera work with OpenCV?

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

Answers (7)

noosh
noosh

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

PawZaw
PawZaw

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

Caridorc
Caridorc

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

garlicFrancium
garlicFrancium

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

Slayahh
Slayahh

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

user1753356
user1753356

Reputation: 171

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.

Upvotes: 16

Stanley
Stanley

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

Related Questions