OpenCV 4.1.1 errors using webcam

I am using a Logitech C920S webcam with my Jetson Nano. Getting errors using the latest (2019/12/17) SD card image. The errors do NOT occur using the 2019/07/19 SD card image.

Simple source program. (Based on a program in the “AI on the Jetson Nano” youtube series at toptechboy.com.)

import cv2
print(cv2.__version__)
dispW=640
dispH=480
flip=2

cam=cv2.VideoCapture(0)
width=cam.get(cv2.CAP_PROP_FRAME_WIDTH)
height=cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
print("size:",width,height) # opencv 4.1.1 displays 2304.0 1536.0

cam.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)
width=cam.get(cv2.CAP_PROP_FRAME_WIDTH)
height=cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
print("size:",width,height) # same as above; however, does change in the loop
d=cam.get(cv2.CAP_PROP_FPS)
print("frame rate",d) # opencv 4.1.1 displays 2.0

firstIterations=1

while True:
    ret, frame=cam.read()
    if firstIterations<=2:
        firstIterations=firstIterations+1
        width=cam.get(cv2.CAP_PROP_FRAME_WIDTH)
        height=cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
        print("size:",width,height) # opencv 4.1.1 displays 640.0 480.0
        print("frame rate",cam.get(cv2.CAP_PROP_FPS)) # opencv 4.1.1 displays 2.0

    cv2.imshow('webCam',frame)
    cv2.moveWindow('webCam',0,0)
    if cv2.waitKey(1)==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

Output from the program follows for both SD cards. Comments continue below the output.

SD card 2019/07/19 output.

john@jetsonnano:~/pyProg/openCV$ python3 openCVbase.py
3.3.1
size: 640.0 480.0
size: 640.0 480.0
frame rate 30.0
size: 640.0 480.0
frame rate 30.0
size: 640.0 480.0
frame rate 30.0
john@jetsonnano:~/pyProg/openCV$

SD card 2019/12/17 output.

john@jetsonnano:~/pyProg/openCV$ python3 openCVbase.py
4.1.1
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
size: 2304.0 1536.0
size: 2304.0 1536.0
frame rate 2.0
size: 640.0 480.0
frame rate 2.0
size: 640.0 480.0
frame rate 2.0
john@jetsonnano:~/pyProg/openCV$

Comments about the program.

  1. The program displays a window at the upper left corner. Size: 640 x 480.
  2. The first three print statements show the initial width and height of the frame and the frame rate before the loop. The width and height are shown twice – once before setting the values and once after setting the values. (Setting the values was added for a reason. Continue reading to see why.)
  3. The three values are printed within the loop two more times.
  4. The program is gracefully ended by pressing the “q” key.

Comments about the output produced by the program.

  1. The 2019/07/19 print output shows that the initial frame width and height are 640.0 and 480.0, respectively; the initial frame rate is 30.0. No error messages appear in the output. The displayed window behaves correctly.
  2. The 2019/12/17 output contains a warning message (issued by gstreamer?). The print output shows that the initial frame width and height are 2304.0 and 1536.0, respectively; the initial frame rate is 2.0. The print statements executed within the loop show that the frame width and height were changed to 640.0 and 480.0, respectively.
    2.1. Because of these initial values, without the “cv2.set” statements, the displayed window fills the screen.
    2.2. The 2.0 value for the frame rate is obviously incorrect.
    2.3. The displayed frame does appear to work correctly, even with these errors, although one should not have to explicitly set the frame size; the frame rate value is certainly suspect.

Summary.

  1. The 2019/07/19 SD card image, which uses OpenCV 3.3.1, works correctly. Good initial values for the frame width and height, good value for the frame rate. No errors displayed. Setting the frame width and height is not necessary.
  2. The 2019/12/17 SD card image, which uses OpenCV 4.1.1, does not work correctly. A warning message appears, followed by invalid values for the frame width, height and frame rate. In order to get a displayed frame having size 640 x 480 it is necessary to explicitly set the size.

How about firstly clarifying whether this issue comes from different version of openCV?

I am just a hobbyist. It is my opinion that only someone who has direct knowledge of how the SD card images are produced can answer your question.

It’s been a week. Is anyone at nvidia looking into this issue?

Hi,

I just checked the openCV forum and notice similar issue.
https://github.com/opencv/opencv/issues/15074
Please check.

The openCV installed here is also from upstream opencv. If there is a bug, we don’t have ability to fix it. What I can do is point this to opencv forum. Maybe you could downgrade to opencv 3.3.1 on 2019/12/17 sdcard image and see if this issue could be reproduced.

Also, as someone who has some knowledge about the production sdcard image, I could tell you that sdcard image itself does not have openCV either. It is installed through some deb package after sdcard image is flashed.

If you think it is an issue from gstreamer, you could also try direct gsteamer command from below link to open your camera.
https://developer.nvidia.com/embedded/dlc/l4t-accelerated-gstreamer-guide-32-1

If it can run normally, then it has high chance that your issue is an issue in openCV.

I looked at the opencv forum link. The top entry, dated Jul 18, 2019, Detailed description, first bullet item, appears to be the same message I was seeing when using openCV 4.1.1, so there is likely that there is, indeed, a problem with that version of openCV.

Thank you for explaining how openCV gets installed after the SD card is flashed. Someone (I assume not someone at nvidia) created the deb package, so this is not an issue with the SD card image.

I do have a second post here, at

https://devtalk.nvidia.com/default/topic/1070685/jetson-nano/jetson-nano-latest-sd-card-image-r32-3-1-contains-opencv-4-1-1-but-no-quot-py-quot-source/

that describes another problem I had with the 4.1.1 openCV. In that post I talk about “code completion” not working for openCV 4.1.1 when using an IDE to enter openCV related code in a python 3 source module. Code completion does work when using the 2019/07/19 SD card image and openCV 3.3.1. In addition, the issues mentioned in the current post do not appear there. The “code completion” issue is also probably a problem with the deb package for openCV.

Because of the openCV issues, I have switched to the older SD card image.

I do see that you’ve suggested that I downgrade to opencv 3.3.1 on the 2019/12/17 SD card image and see if I still have the problem. Even though I’ve switched to the earlier card image, I still have the newer one and am willing to perform the test. However, I don’t know how to (easily) downgrade to opencv 3.3.1. Can you tell me how? (I did some internet searches. What I found indicates that would probably have to build it from source. It’s not worth it to me to go through that.)

Thanks for the link to the gstreamer guide. Looks like I have some reading to do.

I will check your another post.

For the rest of your question:

. However, I don’t know how to (easily) downgrade to opencv 3.3.1. Can you tell me how?
What I found indicates that would probably have to build it from source. It’s not worth it to me to go through that.)

This question is more related to some basics in linux and ubuntu. Maybe we can change this question to another scenario. There is no need to focus on the sdcard image. sdcard image is just another kind of ubuntu system.
If this issue is on your host PC which is also a ubuntu system, how do you downgrade it?

You should be able to remove current openCV by some apt commands since if it is installed by some deb files. (if it is not, then you have to remove it manually).

Then, how do you install a new one? For ubuntu, it has a stable version of deb file on their server for each release.
https://packages.ubuntu.com/source/bionic/opencv

For example, it is opencv2.4.9 for 16.04 and opencv3.2 for 18.04 and 19.04. Thus, when you use default apt commands to download and install, it will always install these versions for you.

Thus, to install a precise version, it is common that you need to compile it from source. Unless someone prepares a pre-built deb file for you.

Thanks for the information. I do understand about the difference between “SD card image” and the resulting operating system and software that is installed at first boot of the card. I’ve just been referring to the card version to be precise about which operating system I’m using.

I do see that both SD cards result in Ubuntu 18.0.x LTS being the underlying linux on the Jetson Nano, where x=2 for the 2019/07/19 SD card image and x=3 for the 2019/12/17 image. Running either versions, I see that an installation of opencv would result in version 3.2 being installed, not 3.3.1. (Run “sudo apt-get install --dry-run python3-opencv” to do a “dry run” – see what would be installed without doing the install. You will see that 3.2 would be installed.) So, I do not see how to get an “official” version of 3.3.1 installed. This also tells me that “unofficial” versions (deb files) of opencv (3.3.1 and 4.1.1) were somehow provided for subsequent install for both versions of the SD cards. I have no idea how that was done.

Since the underlying operating systems are essentially the same, I strongly suspect that opencv 3.3.1 would work on the newer image just as it does on the older image.

My regular PC runs linux, but it is fedora, not ubuntu. At the present time I have no need to install opencv on it.

I agree that to install a precise version, installing from source is the right way to go. However, I am not willing to go through that exercise right now.

As I said in my last post, I am happy using the environment built using the older SD card.

jganci3,

The openCV3.3.1 installed by sdkmanager is actually a upstream openCV we built from source.