Hello.
I am writing motion detection code in jetson nano. The motion detection process is written in cuda.
I have confirmed that we can run the sample video. However, live streaming using the camera cannot be performed with the following warning.
[ WARN:0] global /home/jetson/workspace/opencv-4.5.0/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
I looked into it and found #332 , changed the options and rebuilt OpenCV. However, the result did not change.
How do I get my webcam to work? The source code and OpenCV build information, Camera Information are included below.
Thank you.
MotionGPU.cpp
#include <opencv2/opencv.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <vector>
#include "Kernel.h"
int main() {
std::string src = "v4l2src device=/dev/video0 ! video/x-raw,framerate=30/1,width=640,height=480, format=YUY2 ! videoconvert ! appsink";
cv::VideoCapture cap(src);
//cap.open("vtest.avi");
cv::Mat frame;
cv::Mat mono;
if(cap.isOpened()){return -1;}
while(cap.read(frame)){
frame.convertTo(frame, CV_32FC3, 1/255.0);
cv::Mat mono = cv::Mat::zeros(frame.size(), CV_32FC1);
cudaKernel(frame, mono);
cv::imshow("gpu", mono);
const int key = cv::waitKey(1);
if(key == 'q'){
break;
}
}
cv::destroyAllWindows();
return 0;
}
Kernel.h
#pragma once
#include <opencv2/core.hpp>
#include <opencv2/core/cuda.hpp>
void cudaKernel(cv::Mat &src, cv::Mat &dst);
Kernel.cu
#include <opencv2/cudev.hpp>
#include <iostream>
#include <cuda_runtime.h>
#include <helper_cuda.h>
#include <device_launch_parameters.h>
#include <cmath>
#define CEIL(a, b) (a + (b - 1)) / b
enum thresh_type{
thresh_binary
};
//average
float* p_avg = NULL;
__global__ void invertNP(float* src, float* dst, int cols, int rows, int channels) {
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x < cols && y < rows){
for(int i = 0; i < channels; i++){
dst[y * cols * channels + x * channels + i] = 1.0 - src[y * cols * channels + x * channels + i];
}
}
}
__global__ void convertMono(float* src, float* dst, int cols, int rows, int channels){
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x < cols && y < rows){
float Y = 0.0;
//Y = 0.299R + 0.587G + 0.114B
//B
Y += 0.114 * src[y * cols * channels + x * channels + 0];
//G
Y += 0.587 * src[y * cols * channels + x * channels + 1];
//R
Y += 0.299 * src[y * cols * channels + x * channels + 2];
dst[y * cols + x] = Y;
}
}
__global__ void accumulateWeighted(float* src, float* dst, int cols, int rows, double alpha){
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x < cols && y < rows){
dst[y * cols + x] = (1.0 - alpha) * dst[y * cols + x] + alpha * src[y * cols + x];
}
}
__global__ void convertScaleAbs(float* src, float* dst, int cols, int rows, double alpha, double beta){
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x < cols && y < rows){
dst[y * cols + x] = abs((float)(alpha * src[y * cols + x] + beta));
}
}
__global__ void absDiff(float* src1, float* src2, float* dst, int cols, int rows){
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x < cols && y < rows){
dst[y * cols + x] = abs(src1[y * cols + x] - src2[y * cols + x]);
}
}
__global__ void threshold(float* src, float* dst, int cols, int rows ,float thresh, float maxval, int type){
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x < cols && y < rows){
if(type == thresh_binary){
dst[y * cols + x] = (src[y * cols + x] > thresh) ? maxval : 0;
}
}
}
void cudaKernel(cv::Mat &src, cv::Mat &dst)
{
float* p_src;
float* p_mono;
float* p_temp;
float* p_framedata;
float* p_thresh;
float* p_cpudst;
cudaMalloc((void **)&p_src, sizeof(float) * src.cols * src.rows * src.channels());
cudaMalloc((void **)&p_mono, sizeof(float) * dst.cols * dst.rows * dst.channels());
cudaMalloc((void **)&p_temp, sizeof(float) * dst.cols * dst.rows * dst.channels());
cudaMalloc((void **)&p_framedata, sizeof(float) * dst.cols * dst.rows * dst.channels());
cudaMalloc((void **)&p_thresh, sizeof(float) * dst.cols * dst.rows * dst.channels());
cudaMallocHost((void **)&p_cpudst,sizeof(float) * dst.cols * dst.rows * dst.channels());
cudaMemcpy(p_src, src.data, sizeof(float) * src.cols * src.rows * src.channels(), cudaMemcpyHostToDevice);
cudaMemcpy(p_mono, dst.data, sizeof(float) * dst.cols * dst.rows * dst.channels(), cudaMemcpyHostToDevice);
dim3 dimBlock(32, 32);
dim3 dimGrid(CEIL(src.cols, 32), CEIL(src.rows, 32));
//Convert mono
convertMono<<<dimGrid, dimBlock>>>(p_src, p_mono, src.cols, src.rows, src.channels());
if(!p_avg){
cudaMalloc((void **)&p_avg, sizeof(float) * src.cols * src.rows * 1);
//initial value
cudaMemcpy(p_avg, p_mono, sizeof(float) * src.cols * src.rows * 1, cudaMemcpyDeviceToDevice);
return;
}
//AccumulateWeighted
accumulateWeighted<<<dimGrid, dimBlock>>>(p_mono, p_avg, src.cols, src.rows, 0.6);
convertScaleAbs<<<dimGrid, dimBlock>>>(p_avg, p_temp, src.cols, src.rows, 1.0, 0.0);
absDiff<<<dimGrid, dimBlock>>>(p_mono, p_temp, p_framedata, src.cols, src.rows);
//threshold
threshold<<<dimGrid, dimBlock>>>(p_framedata, p_thresh, src.cols, src.rows, 3.0/255.0, 1.0, thresh_binary);
cudaMemcpy(p_cpudst, p_thresh, sizeof(float) * dst.cols * dst.rows * dst.channels(), cudaMemcpyDeviceToHost);
dst = cv::Mat(dst.rows, dst.cols, CV_32FC1, p_cpudst);
cudaFree(p_src);
cudaFree(p_mono);
cudaFree(p_temp);
cudaFree(p_framedata);
checkCudaErrors(cudaGetLastError());
checkCudaErrors(cudaDeviceSynchronize());
}
OpenCV Build Information
General configuration for OpenCV 4.5.0 =====================================
Version control: unknown
Extra modules:
Location (extra): /home/jetson/workspace/opencv_contrib-4.5.0/modules
Version control (extra): unknown
Platform:
Timestamp: 2021-07-09T05:18:31Z
Host: Linux 4.9.201-tegra aarch64
CMake: 3.10.2
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: RELEASE
CPU/HW features:
Baseline: NEON FP16
required: NEON
disabled: VFPV3
C/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: /usr/bin/c++ (ver 7.5.0)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,--gc-sections -Wl,--as-needed
Linker flags (Debug): -Wl,--gc-sections -Wl,--as-needed
ccache: NO
Precompiled headers: NO
Extra dependencies: m pthread cudart_static dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu
3rdparty dependencies:
OpenCV modules:
To be built: alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
Disabled: world
Disabled by dependency: -
Unavailable: cnn_3dobj cvv hdf java js julia matlab ovis sfm ts viz
Applications: apps
Documentation: NO
Non-free algorithms: NO
GUI:
GTK+: YES (ver 3.22.30)
GThread : YES (ver 2.56.4)
GtkGlExt: NO
VTK support: NO
Media I/O:
ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
WEBP: build (ver encoder: 0x020f)
PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)
TIFF: /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
JPEG 2000: build (ver 2.3.1)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES
Video I/O:
GStreamer: YES (1.14.5)
v4l/v4l2: YES (linux/videodev2.h)
Parallel framework: pthreads
Trace: YES (with Intel ITT)
Other third-party libraries:
Lapack: NO
Eigen: YES (ver 3.3.4)
Custom HAL: YES (carotene (ver 0.0.1))
Protobuf: build (3.5.1)
NVIDIA CUDA: YES (ver 10.2, CUFFT CUBLAS)
NVIDIA GPU arch: 53 62 72
NVIDIA PTX archs:
cuDNN: YES (ver 8.0.0)
OpenCL: YES (no extra features)
Include path: /home/jetson/workspace/opencv-4.5.0/3rdparty/include/opencl/1.2
Link libraries: Dynamic load
Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.17)
Libraries: /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.17)
numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
install path: lib/python2.7/dist-packages/cv2/python-2.7
Python 3:
Interpreter: /usr/bin/python3 (ver 3.6.9)
Libraries: /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.9)
numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3)
install path: lib/python3.6/dist-packages/cv2/python-3.6
Python (for build): /usr/bin/python2.7
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Install to: /usr/local
-----------------------------------------------------------------
Camera Information
Driver Info (not using libv4l2):
Driver name : uvcvideo
Card type : HD Pro Webcam C920
Bus info : usb-70090000.xusb-2.1
Driver version: 4.9.201
Capabilities : 0x84200001
Video Capture
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04200001
Video Capture
Streaming
Extended Pix Format
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUYV 4:2:2
Index : 1
Type : Video Capture
Pixel Format: 'H264' (compressed)
Name : H.264
Index : 2
Type : Video Capture
Pixel Format: 'MJPG' (compressed)
Name : Motion-JPEG
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUYV 4:2:2
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 160x90
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 160x120
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 176x144
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 320x180
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 352x288
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 432x240
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 800x448
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 864x480
Interval: Discrete 0.042s (24.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 960x720
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1024x576
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1600x896
Interval: Discrete 0.133s (7.500 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 2304x1296
Interval: Discrete 0.500s (2.000 fps)
Size: Discrete 2304x1536
Interval: Discrete 0.500s (2.000 fps)