11

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,895
  • 26
  • 119
  • 185
  • 2
    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
  • 1
    @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
  • 1
    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

6 Answers6

12

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
  • 1,003
  • 1
  • 13
  • 21
user1753356
  • 131
  • 1
  • 3
  • 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. – Code Doggo Sep 23 '22 at 21:26
6

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

Stanley
  • 2,434
  • 18
  • 28
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.

Slayahh
  • 373
  • 4
  • 14
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.

Caridorc
  • 6,222
  • 2
  • 31
  • 46
1

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
  • 2,013
  • 14
  • 23
-1

when you are using a usb webcam as a video capture, just try cv2.VideoCapture(0) , it works for me to using eksternal webcam

GigaTera
  • 782
  • 8
  • 9