How to use USB webcam in Jetson TX2 with Python and OpenCV?

Here is simple Python Code provided by OpenCV https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html

import numpy as np
import cv2

cap = cv2.VideoCapture("/dev/video1") # check this
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

[b] this code works as charm

Regards
[/b]

3 Likes

import numpy as np
import cv2

cap = cv2.VideoCapture(“/dev/video1”) # check this
cap.isOpened()

the result is false,why?

my opencv is tegraopencv ,jetsonpack3.1

Did you plug the USB cam into USB 3.1 Port? if so Change it to the USB 2.0 ( Converted from micro one) and open your terminal and try this

ls /dev/video*

and let me see the result!

I have installed opencv3.3,the USB cam is OK.

thanks

Hello! I also cannot access USB webcam via OpenCV Python wrapper.
But to be honest, next code is working on MacBook Pro (because it’s camera is embedded)

# OpenCV_test_3.py

# this program tracks a red ball
# (no motor control is performed to move the camera, we will get to that later in the tutorial)

import os

import cv2
import numpy as np


###################################################################################################
def main():
    capWebcam = cv2.VideoCapture(0)  # declare a VideoCapture object and associate to webcam, 0 => use 1st webcam

    # show original resolution
    print("default resolution = " + str(capWebcam.get(cv2.CAP_PROP_FRAME_WIDTH)) + "x" + str(
        capWebcam.get(cv2.CAP_PROP_FRAME_HEIGHT)))

    capWebcam.set(cv2.CAP_PROP_FRAME_WIDTH, 320.0)  # change resolution to 320x240 for faster processing
    capWebcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 240.0)

    # show updated resolution
    print("updated resolution = " + str(capWebcam.get(cv2.CAP_PROP_FRAME_WIDTH)) + "x" + str(
        capWebcam.get(cv2.CAP_PROP_FRAME_HEIGHT)))

    if capWebcam.isOpened() == False:  # check if VideoCapture object was associated to webcam successfully
        print("error: capWebcam not accessed successfully\n\n")  # if not, print error message to std out
        os.system("pause")  # pause until user presses a key so user can see error message
        return  # and exit function (which exits program)
    # end if

    while cv2.waitKey(1) != 27 and capWebcam.isOpened():  # until the Esc key is pressed or webcam connection is lost
        blnFrameReadSuccessfully, imgOriginal = capWebcam.read()  # read next frame

        if not blnFrameReadSuccessfully or imgOriginal is None:  # if frame was not read successfully
            print("error: frame not read from webcam\n")  # print error message to std out
            os.system("pause")  # pause until user presses a key so user can see error message
            break  # exit while loop (which exits program)
        # end if

        imgHSV = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2HSV)

        imgThreshLow = cv2.inRange(imgHSV, np.array([0, 135, 135]), np.array([18, 255, 255]))
        imgThreshHigh = cv2.inRange(imgHSV, np.array([165, 135, 135]), np.array([179, 255, 255]))

        imgThresh = cv2.add(imgThreshLow, imgThreshHigh)

        imgThresh = cv2.GaussianBlur(imgThresh, (3, 3), 2)

        imgThresh = cv2.dilate(imgThresh, np.ones((5, 5), np.uint8))
        imgThresh = cv2.erode(imgThresh, np.ones((5, 5), np.uint8))

        intRows, intColumns = imgThresh.shape

        circles = cv2.HoughCircles(imgThresh, cv2.HOUGH_GRADIENT, 5,
                                   intRows / 4)  # fill variable circles with all circles in the processed image

        if circles is not None:  # this line is necessary to keep program from crashing on next line if no circles were found
            for circle in circles[0]:  # for each circle
                x, y, radius = circle  # break out x, y, and radius
                print("ball position x = " + str(x) + ", y = " + str(y) + ", radius = " + str(
                    radius))  # print ball position and radius
                cv2.circle(imgOriginal, (x, y), 3, (0, 255, 0),
                           -1)  # draw small green circle at center of detected object
                cv2.circle(imgOriginal, (x, y), radius, (0, 0, 255), 3)  # draw red circle around the detected object
                # end for
        # end if

        cv2.namedWindow("imgOriginal", cv2.WINDOW_AUTOSIZE)  # create windows, use WINDOW_AUTOSIZE for a fixed window size
        cv2.namedWindow("imgThresh", cv2.WINDOW_AUTOSIZE)  # or use WINDOW_NORMAL to allow window resizing

        cv2.imshow("imgOriginal", imgOriginal)  # show windows
        cv2.imshow("imgThresh", imgThresh)
    # end while

    cv2.destroyAllWindows()  # remove windows from memory

    return


if __name__ == '__main__':
    main()

What is your OpenCV Version? You can find it by using

cv2.__version __

it’s 3.3.0

Are you sure your usb cam is /dev/video0 ? Usually, if the Jetson has the onboard camera, it gets /dev/video0, so the first USB webcam would be /dev/video1 and you would use cv2.VideoCapture(1).

Otherwise, check that your opencv library has been built with python support.

Are you able to use your embeded USB Cam?

yes, it’s working on my PC with Ubuntu.

Thank you so much @isrugeek,

I tried your guide: “Did you plug the USB cam into USB 3.1 Port? if so Change it to the USB 2.0 ( Converted from micro one) and open your terminal and try this”

and my usb camera worked! :)