Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 267
greenoldman Avatar asked Aug 26 '18 18:08

greenoldman


People also ask

How use OpenCV external camera?

Compile and install: The following sample OpenCV python code explain how to open the device video node, set the resolution, grab the frame and then display the frame in preview window. # Check whether user selected camera is opened successfully. Release the camera, then close all of the imshow() windows.

What cameras work with OpenCV?

Pretty much any logitech camera is great for openCV, the most common one being the C270. You can get wider-angle cameras but they are nearly $100, so it's usually easier to just glue a phone camera lens on for a wider FOV. The Pixy is really popular, and it has a bunch of code and libraries available online.


3 Answers

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.
like image 55
user1753356 Avatar answered Oct 23 '22 12:10

user1753356


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

like image 22
Stanley Avatar answered Oct 23 '22 14:10

Stanley


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.

like image 26
Slayahh Avatar answered Oct 23 '22 13:10

Slayahh