6

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?

greenoldman
  • 16,144
  • 25
  • 114
  • 178
  • 1
    Are you sure the usb camera is camera 1, i've done this before and had to use cv2.VideoCapture(0) – Stanley Aug 26 '18 at 19:01
  • @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? – greenoldman Aug 26 '18 at 19:08
  • 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! :) – Stanley Aug 26 '18 at 19:23

5 Answers5

7

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.
THess
  • 993
  • 1
  • 10
  • 21
user1753356
  • 71
  • 1
  • 2
4

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

Stanley
  • 2,043
  • 14
  • 23
3

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.

Slayahh
  • 165
  • 10
0

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

garlicFrancium
  • 1,700
  • 12
  • 21
0

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.

Caridorc
  • 3,691
  • 27
  • 40