gst-launch-1.0 -ev nvcamerasrc ! nvvidconv ! ximagesink
Hello, I used the code above to launch the Jetson TX1 camera.
How can I write this instead of cam_index.
CvCapture *cap = cvCaptureFromCAM(cam_index);
Thanks…
gst-launch-1.0 -ev nvcamerasrc ! nvvidconv ! ximagesink
Hello, I used the code above to launch the Jetson TX1 camera.
How can I write this instead of cam_index.
CvCapture *cap = cvCaptureFromCAM(cam_index);
Thanks…
Not sure it works with C API, but with C++ API gstreamer is handled by the constructor for reading from file.
Note that you need an opencv library built with gstreamer support enabled.
Then you have to make a gstreamer pipeline for providing BGR or gray8 to opencv (from opencv-3.3, opencv is able to read more formats, but you may have to convert to BGR anyway for using opencv algorithms).
const char* gst = "nvcamerasrc ! video/x-raw(memory:NVMM), format=(string)I420, width=(int)640, height=(int)480, framerate=(fraction)30/1 ! \
nvvidconv ! video/x-raw, format=(string)BGRx ! \
videoconvert ! video/x-raw, format=(string)BGR ! \
appsink";
cv::VideoCapture cap(gst);
Thank you for your answer. Your code works in C ++. But it can not be integrated into the cvCaptureFromCAM function in C language. Is there a way to do that?
I have very few experience with C API of opencv, not sure if I can really help.
I was suggesting to try with cvCaptureFromFile() that expects a string argument. If it doesn’t work you might consider switching to C++ API.
Opencv is developed in C++ since many years now, and these new features such a gstreamer support may have not been ported back to C API that is deprecated.
OpenCV is open source, you can do that backport to C API yourself, but considering the time it would take it is probably better to use C++ API, as a C++ compiler should be able to handle your C code.
Good news…The code below works on my TX2 with L4T R28.1 and opencv-3.3.0, but for using C API it first requires patching opencv-3.3.0 header file opencv2/core/cvdef.h for adding the following lines at the end (before the last #endif seems ok, at least for this example), otherwise compiler will complain about undefined cvRound() :
#ifndef __cplusplus
#include "opencv2/core/fast_math.hpp"
#endif
File simple_video_c_api.c:
#include <stdio.h>
#include <opencv2/core/core_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/videoio/videoio_c.h>
int main()
{
const char* gst = "nvcamerasrc ! video/x-raw(memory:NVMM), format=I420, width=640, height=480, framerate=30/1 ! "
"nvvidconv ! video/x-raw, format=BGRx ! "
"videoconvert ! video/x-raw, format=BGR ! "
"appsink";
CvCapture *cap = cvCaptureFromFile(gst);
if (!cap) {
fprintf (stderr, "Failed to open camera.\n");
return (-1);
}
cvNamedWindow("MyCameraPreview", CV_WINDOW_AUTOSIZE);
IplImage* frame_in;
while(1)
{
frame_in = cvQueryFrame(cap);
if (!frame_in) {
fprintf (stderr, "Capture read error\n");
break;
}
else {
cvShowImage("MyCameraPreview", frame_in);
cvWaitKey(1);
}
}
cvReleaseCapture(&cap);
return 0;
}
My opencv-3.3.0 library being installed in /usr/local/opencv-3.3.0, I compiled with:
gcc -o simple_video_c_api simple_video_c_api.c -I/usr/local/opencv-3.3.0/include -L/usr/local/opencv-3.3.0/lib -lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lm
and run with:
export LD_LIBRARY_PATH=/usr/local/opencv-3.3.0/lib
./simple_video_c_api
You may however read https://github.com/opencv/opencv/issues/6221, especially noting:
C API is not supported anymore and should not be used. Moreover some “C” calls can raise C++ exceptions.
There is no plan to revive direct C API in OpenCV (you should wait for some FFI C bindings). Use OpenCV from C++ programs only.
Thank you very much for your help. Code works very well …