Explorer/RaspberryPi/Visualprocessing/Opencv/Optimisation

From PaparazziUAV
Jump to navigation Jump to search

Improve latency and CPU usage by removing filter "VIDEOCONVERT" and using v4l2h264enc instead of omxh264enc

gst-launch-1.0 shmsrc socket-path=/tmp/camera3 do-timestamp=true \
  ! video/x-raw, format=BGR, width=640, height=480, framerate=30/1, colorimetry=1:1:5:1  \
  ! v4l2h264enc extra-controls="controls,video_bitrate=3000000;" \
  ! rtph264pay name=pay0 pt=96 config-interval=1 \
  ! udpsink host=127.0.0.1 port=5600 &

TO BE UPDATED

#include <opencv2/opencv.hpp>
#define WIDTH 640
#define HEIGHT 480
#define FPS 30
#define SCALE 3/2

using namespace cv;
using namespace std;
int main(int, char**)
{
 unsigned int dataSize = sizeof(unsigned char)*WIDTH*HEIGHT*SCALE;
 Mat imageIn(WIDTH*SCALE, HEIGHT, CV_8UC1);
 Mat imageOut(WIDTH,HEIGHT,CV_8UC3,Scalar(0,0,0));
 //cout << getBuildInformation() << endl;

 string streamInGstStr="shmsrc socket-path=/tmp/camera2 ! video/x-raw,width="+to_string(WIDTH)+
  ",height="+to_string(HEIGHT)+",framerate="+to_string(FPS)+"/1,format=I420 ! appsink sync=true";
 string streamOutGstStr="appsrc ! shmsink socket-path=/tmp/camera3 wait-for-connection=false async=false sync=false";

 VideoCapture streamIn(streamInGstStr,CAP_GSTREAMER);
 VideoWriter  streamOut(streamOutGstStr,0,FPS/1,Size(WIDTH,HEIGHT),true); 

 if (streamIn.isOpened() && streamOut.isOpened()) {
   while (true) {
     streamIn.read(imageIn);
     if (!imageIn.empty()) {
       memcpy(imageOut.data,imageIn.data,dataSize);
       streamOut.write(imageOut);
     }
   }
 }
 return 0;
}
rm /tmp/camera*;\
raspivid -t 0 -w 640 -h 480 -fps 30/1 -b 3000000 -g 5 -vf -hf -cd H264 -n -fl -ih -o -  \
  | gst-launch-1.0 fdsrc  \
  ! h264parse  \
  ! video/x-h264,stream-format=byte-stream  \
  ! tee name=streams \
  ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0 \
  ! udpsink host=127.0.0.1 port=5100 streams. \
  ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0 \
  ! omxh264dec \
  ! shmsink socket-path=/tmp/camera2 wait-for-connection=false sync=false &
sleep 1
~/opencv_test/test &
sleep 1
gst-rtsp-server-1.14.4/examples/test-launch  \
"udpsrc port=5100 do-timestamp=true ! video/x-h264,stream-format=byte-stream,alignment=au ! rtph264pay name=pay0 pt=96 config-interval=1" \
"shmsrc socket-path=/tmp/camera3 do-timestamp=true ! video/x-raw, format=I420, width=640, height=480, framerate=30/1 ! omxh264enc ! video/x-h264,profile=high  ! rtph264pay name=pay0 pt=96 config-interval=1" &

client:
gst-launch-1.0 rtspsrc location=rtsp://RASPBERRYPI_IP:8554/test ! rtph264depay ! avdec_h264 ! xvimagesink sync=false
gst-launch-1.0 rtspsrc location=rtsp://RASPBERRYPI_IP:8554/test ! rtph264depay ! avdec_h264 ! xvimagesink sync=false