onboard camera issue with opencv

Hi!

I got an error when running opencv with onboard camera on Jetson TX2.

Whenever I run opencv example codes including cap.read(), the system shows the following error.

error: (-215) scn == 3 || scn == 4 in function cvtColor

Could you help?

Thanks!

Maybe providing more details may help (which opencv, if using gstreamer which pipeline, …).
I’d suggest you read this thread: https://devtalk.nvidia.com/default/topic/1001696/jetson-tx1/failed-to-open-tx1-on-board-camera/post/5117370/#5117370

When I was using opencv with python,

I used following code to open a camera.

import cv2

cap = cv2.VideoCapture(“nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink”)

and then, I used this cap.read() to capture frame.

But, it did not get any frames from the camera on TX2 and showed that error.

error: (-215) scn == 3 || scn == 4 in function cvtColor

Which opencv version are you using ?
For this you need a gstreamer-1.0 enabled opencv. I don’t think that opencv4tegra-2.4.13 in L4T does support that, so I would suggest you build opencv-3.2.0 with gstreamer and python support, install it on your TX2 and try the example against new version. Be sure to set -I and -L for include and library paths, as opencv4tegra is in standard path /usr for C++. For python, I cannot tell, but you may have to adjust paths as well.

For python, you could also try this syntax:

gst = "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"

cap = cv2.VideoCapture(gst)

Which camera are you using ?
Check it supports the requested mode.
You can use

gst-device-monitor-1.0

and look for devices with class : Video/Source.

However, I agree that your pipeline seems to be ok with onboard camera from gst-launch:

gst-launch-1.0  nvcamerasrc ! 'video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1' ! nvvidconv flip-method=2 ! 'video/x-raw, format=(string)I420' ! videoconvert ! 'video/x-raw, format=(string)BGR' ! fakesink

And it works also in a C++ opencv-3.2.0 sample code, so I’d suspect your opencv library or paths settings.

Given that the device claims to support RGB, perhaps a better option would be to just request RGB directly, without the additional commands to convert the format?

I am also unable to get that pipeline working, for me in c++ OpenCV 3.2. When I try to create the VideoCapture object, I get the following errors:

Available Sensor modes :
2592 x 1944 FR=30.000000 CF=0x1109208a10 SensorModeType=4 CSIPixelBitDepth=10 DynPixelBitDepth=10
2592 x 1458 FR=30.000000 CF=0x1109208a10 SensorModeType=4 CSIPixelBitDepth=10 DynPixelBitDepth=10
1280 x 720 FR=120.000000 CF=0x1109208a10 SensorModeType=4 CSIPixelBitDepth=10 DynPixelBitDepth=10

NvCameraSrc: Trying To Set Default Camera Resolution. Selected 640x480 FrameRate = 30.000000 ...

Available Sensor modes :
2592 x 1944 FR=30.000000 CF=0x1109208a10 SensorModeType=4 CSIPixelBitDepth=10 DynPixelBitDepth=10
2592 x 1458 FR=30.000000 CF=0x1109208a10 SensorModeType=4 CSIPixelBitDepth=10 DynPixelBitDepth=10
1280 x 720 FR=120.000000 CF=0x1109208a10 SensorModeType=4 CSIPixelBitDepth=10 DynPixelBitDepth=10

NvCameraSrc: Trying To Set Default Camera Resolution. Selected 640x480 FrameRate = 30.000000 ...

GStreamer Plugin: Embedded video playback halted; module nvcamerasrc0 reported: Internal data flow error.
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline
) in icvStartPipeline, file /home/nvidia/opencv/modules/videoio/src/cap_gstreamer.cpp, line 407
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/nvidia/opencv/modules/videoio/src/cap_gstreamer.cpp:407: error: (-2) GStreamer: unable to start pipeline
 in function icvStartPipeline

However, If I run the exact same pipeline with gst-launch-1.0 it works just fine. Any ideas?

I apologize, I realized my error was due to the fact that I was (inadvertently) attempting to change the resolution of the VideoCapture object after it had been opened. Apparently this is not possible with a gstreamer pipeline implementation of the VideoCapture class.