From 63ad36654d880d0ec8e0a241885bc59b6aa20e84 Mon Sep 17 00:00:00 2001 From: diegohce Date: Mon, 28 Oct 2024 15:45:11 -0300 Subject: [PATCH] GStreamer VideoWriter example --- Makefile | 2 +- cmd/gstreamer-writer/main.go | 66 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 cmd/gstreamer-writer/main.go diff --git a/Makefile b/Makefile index ccc035d6..6469ea5b 100644 --- a/Makefile +++ b/Makefile @@ -214,7 +214,7 @@ build_cuda: mkdir build cd build rm -rf * - cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} -D OPENCV_EXTRA_MODULES_PATH=$(TMP_DIR)opencv/opencv_contrib-$(OPENCV_VERSION)/modules -D BUILD_DOCS=OFF -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=ON -D BUILD_opencv_java=NO -D BUILD_opencv_python=NO -D BUILD_opencv_python2=NO -D BUILD_opencv_python3=NO -D WITH_JASPER=OFF -D WITH_TBB=ON -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_CUDA=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=ON -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/ -DBUILD_opencv_cudacodec=OFF -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_GENERATION=Auto -DOPENCV_ENABLE_NONFREE=ON -D WITH_GSTREAMER=OFF .. + cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} -D OPENCV_EXTRA_MODULES_PATH=$(TMP_DIR)opencv/opencv_contrib-$(OPENCV_VERSION)/modules -D BUILD_DOCS=OFF -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=ON -D BUILD_opencv_java=NO -D BUILD_opencv_python=NO -D BUILD_opencv_python2=NO -D BUILD_opencv_python3=NO -D WITH_JASPER=OFF -D WITH_TBB=ON -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_CUDA=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=ON -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/ -DBUILD_opencv_cudacodec=OFF -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_GENERATION=Auto -DOPENCV_ENABLE_NONFREE=ON -D WITH_GSTREAMER=ON .. $(MAKE) -j $(shell nproc --all --ignore 1) $(MAKE) preinstall cd - diff --git a/cmd/gstreamer-writer/main.go b/cmd/gstreamer-writer/main.go new file mode 100644 index 00000000..c70baa53 --- /dev/null +++ b/cmd/gstreamer-writer/main.go @@ -0,0 +1,66 @@ +/* + GStreamer Video Writer + +What it does: + + Captures video from webcam and outputs the frames to a gocv VideoWriter + +How to run: + + go run main.go + +To receive the video feed run in a terminal: + + gst-launch-1.0 udpsrc port=5000 ! application/x-rtp, encoding-name=MJPG ! rtpjpegdepay ! jpegdec ! videoconvert ! xvimagesink +*/ +package main + +import ( + "fmt" + + "gocv.io/x/gocv" +) + +const ( + gstreamerPipe = "appsrc ! videoconvert ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! jpegenc ! rtpjpegpay ! udpsink host=127.0.0.1 port=5000" +) + +func main() { + + cap, err := gocv.VideoCaptureDevice(0) + if err != nil { + panic(err) + } + defer cap.Close() + cap.Set(gocv.VideoCaptureFrameWidth, 640.0) + cap.Set(gocv.VideoCaptureFrameHeight, 480.0) + + wrt, err := gocv.VideoWriterFileWithAPI(gstreamerPipe, + gocv.VideoCaptureGstreamer, "YUY2", 30.0, 640, 480, true) + if err != nil { + panic(err) + } + defer wrt.Close() + + if !wrt.IsOpened() { + panic("wrt is not opened") + } + + frame := gocv.NewMat() + defer frame.Close() + + for { + cap.Read(&frame) + + if frame.Empty() { + fmt.Println("empty frame") + continue + } + + if err := wrt.Write(frame); err != nil { + fmt.Println(err) + } + + } + +}