Difference between revisions of "Explorer/RaspberryPi/Visualprocessing/Opencv"

From PaparazziUAV
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 54: Line 54:


create ~/opencv_test/ptest.py
create ~/opencv_test/ptest.py
chmod ugo+xwr ~/opencv_test/ptest.py
  #!/usr/bin/python3
  #!/usr/bin/python3
  import numpy as np
  import numpy as np
Line 63: Line 64:
  processing_cpt = 0
  processing_cpt = 0
  frame = None
  frame = None
def open_cam():
  return cv2.VideoCapture('shmsrc socket-path=/tmp/camera2 ! '
    'video/x-raw,width=640,height=480,framerate=15/1,format=RGB ! '
    'videoconvert ! '
    'appsink sync=false',cv2.CAP_GSTREAMER)
def open_output():
  return cv2.VideoWriter('appsrc ! queue ! '
    'shmsink socket-path=/tmp/camera3 '
    'wait-for-connection=false async=false sync=false ',
    0, 15.0, (640,480))
   
   
  def data_acquisition(cap,out,condition):
  def data_acquisition(cap,out,condition):
  global acquisition_cpt,frame
  global acquisition_cpt,frame
  print("data_acquisition thread")
  print("data_acquisition thread")
  while True:
  while True:
    grabbed,frame = cap.read()
    grabbed,frame = cap.read()
    if grabbed:
    if grabbed:
      with condition:
      with condition:
        acquisition_cpt = acquisition_cpt + 1
        acquisition_cpt = acquisition_cpt + 1
        print("acquisition_cpt ",acquisition_cpt)
        #print("acquisition_cpt ",acquisition_cpt)
        condition.notify()
        condition.notify()
   
   
  def data_processing(cap,out,condition):
  def data_processing(cap,out,condition):
  global processing_cpt
  global processing_cpt
  print("data_processing thread")
  print("data_processing thread")
  while True:
  while True:
    with condition:
    with condition:
      condition.wait()
      condition.wait()
      img = frame.copy()
      img = frame.copy()
    processing_cpt = processing_cpt + 1
    processing_cpt = processing_cpt + 1
    print("processing_cpt ",processing_cpt)
    #print("processing_cpt ",processing_cpt)
    out.write(img)
    out.write(img)
   
   
  if __name__ == '__main__':
  if __name__ == '__main__':
  cap = open_cam()
  cap = cv2.VideoCapture('udpsrc port=5200 ! h264parse ! omxh264dec ! videoconvert ! video/x-raw, format=(string)BGR ! appsink', cv2.CAP_GSTREAMER);
  out = open_output()
  out = cv2.VideoWriter('appsrc ! videoconvert ! omxh264enc ! video/x-h264,profile=high ! udpsink host=127.0.0.1 port=5210',cv2.CAP_GSTREAMER,0, 30, (640,480), True)
  time.sleep(0.1)
  time.sleep(0.1)
  if not cap.isOpened() or not out.isOpened():
  if not cap.isOpened() or not out.isOpened():
    print("not opened")
    print("not opened")
    quit()
    quit()
  threads = []
  threads = []
  condition = threading.Condition()
  condition = threading.Condition()
  for func in [data_acquisition, data_processing]:
  for func in [data_acquisition, data_processing]:
    threads.append(threading.Thread(target=func, args=(cap,out,condition)))
    threads.append(threading.Thread(target=func, args=(cap,out,condition)))
    threads[-1].start()  
    threads[-1].start()  
  for thread in threads:
  for thread in threads:
    thread.join()
    thread.join()
  cap.release()
  cap.release()


create ~/opencv_test/test.cpp
create ~/opencv_test/test.cpp
Line 120: Line 109:
  #define FPS 30
  #define FPS 30
  #define SCALE 3/2
  #define SCALE 3/2
 
  using namespace cv;
  using namespace cv;
  using namespace std;
  using namespace std;
  int main(int, char**)
  int main(int, char**)
  {
  {
  unsigned int dataSize = sizeof(unsigned char)*WIDTH*HEIGHT*SCALE;
  Mat data;
  Mat imageIn(WIDTH*SCALE, HEIGHT, CV_8UC1);
  string streamInGstStr="udpsrc port=5200 ! h264parse ! omxh264dec ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
  Mat imageOut(WIDTH,HEIGHT,CV_8UC3,Scalar(0,0,0));
  string streamOutGstStr="appsrc ! videoconvert ! omxh264enc ! video/x-h264,profile=high ! udpsink host=127.0.0.1 port=5210";
  cout << getBuildInformation() << endl;
  string streamInGstStr="shmsrc socket-path=/tmp/camera2 ! video/x-raw,width="+to_string(WIDTH)+
  VideoCapture streamIn(streamInGstStr,CAP_GSTREAMER);
  ",height="+to_string(HEIGHT)+",framerate="+to_string(FPS)+"/1,format=I420 ! appsink sync=true";
  VideoWriter  streamOut(streamOutGstStr,0,FPS/1,Size(WIDTH,HEIGHT),true);
  string streamOutGstStr="appsrc ! shmsink socket-path=/tmp/camera3 wait-for-connection=false async=false sync=false";
  VideoCapture streamIn(streamInGstStr,CAP_GSTREAMER);
  if (streamIn.isOpened() && streamOut.isOpened()) {
  VideoWriter  streamOut(streamOutGstStr,0,FPS/1,Size(WIDTH,HEIGHT),true);
    while (true) {
  if (streamIn.isOpened() && streamOut.isOpened()) {
      streamIn.read(data);
    while (true) {
      if (!data.empty()) {
      streamIn.read(imageIn);
        streamOut.write(data);
      if (!imageIn.empty()) {
      }
        memcpy(imageOut.data,imageIn.data,dataSize);
    }
        streamOut.write(imageOut);
  }
      }
  return 0;
    }
  }
  return 0;
  }
  }


  g++ -g ~/opencv_test/test.cpp -o ~/opencv_test/test `pkg-config --cflags --libs opencv4`  
  g++ -g ~/opencv_test/test.cpp -o ~/opencv_test/test `pkg-config --cflags --libs opencv4`  


Run foolowing script with test and ptest
Run following script with ~/opencv_test/test and ~/opencv_test/ptest.py


#!/bin/bash
sudo killall raspivid gst-launch test-launch test ptest.py \
  rm /tmp/camera*;\
  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 -  \
  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  \
  | gst-launch-1.0 fdsrc  \
  ! h264parse  \
    ! h264parse  \
  ! video/x-h264,stream-format=byte-stream  \
    ! video/x-h264,stream-format=byte-stream  \
  ! tee name=streams \
    ! tee name=streams \
  ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0 \
    ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0 \
  ! udpsink host=127.0.0.1 port=5100 streams. \
    ! udpsink host=127.0.0.1 port=5100 streams. \
  ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0 \
    ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0 \
  ! omxh264dec \
    ! udpsink host=127.0.0.1 port=5200 &
  ! shmsink socket-path=/tmp/camera2 wait-for-connection=false sync=false &
sleep 1
#~/opencv_test/ptest.py &
  ~/opencv_test/test &
  ~/opencv_test/test &
sleep 1
  gst-rtsp-server-1.14.4/examples/test-launch \
  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 \" \
"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\"" &
"udpsrc port=5210 do-timestamp=true ! video/x-h264,stream-format=byte-stream,alignment=au ! rtph264pay name=pay0 pt=96 config-interval=1"  


client:
client:
Line 169: Line 160:


  gst-launch-1.0 rtspsrc location=rtsp://RASPBERRYPI_IP:8554/test2 ! rtph264depay ! avdec_h264 !  xvimagesink sync=false
  gst-launch-1.0 rtspsrc location=rtsp://RASPBERRYPI_IP:8554/test2 ! rtph264depay ! avdec_h264 !  xvimagesink sync=false
[[Explorer/RaspberryPi/Visualprocessing/Opencv/Optimisation]]

Latest revision as of 22:24, 27 June 2020

  • Installation

PIZero installation > 10 hours
PI3 installation > 4 hours

sudo apt-get install -y cmake

sudo apt-get install -y python-numpy python3-numpy libpython-dev libpython3-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libtiff-dev zlib1g-dev libjpeg-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

sudo apt-get install -y libatlas-base-dev gfortran
for raspberry pi3
sudo apt-get install -y libgtk2.0-dev
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git 
cd ~/opencv 
git checkout 4.3.0
cd ~/opencv_contrib
git checkout 4.3.0
mkdir ~/opencv/build
cd ~/opencv/build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D INSTALL_PYTHON_EXAMPLES=OFF \
   -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
   -D OPENCV_GENERATE_PKGCONFIG=YES \
   -D BUILD_TESTS=OFF \
   -D BUILD_PERF_TESTS=OFF \
   -D BUILD_EXAMPLES=OFF ..

=> GStreamer: YES (1.14.4)

/etc/dphys-swapfile
CONF_SWAPSIZE=2048

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
make & 
for raspberrypi3: make -j4&
sudo make install
sudo ldconfig -v
  • Check installation
python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.3.0'

create ~/opencv_test/ptest.py chmod ugo+xwr ~/opencv_test/ptest.py

#!/usr/bin/python3
import numpy as np
import threading
import cv2
import time

acquisition_cpt = 0
processing_cpt = 0
frame = None

def data_acquisition(cap,out,condition):
  global acquisition_cpt,frame
  print("data_acquisition thread")
  while True:
    grabbed,frame = cap.read()
    if grabbed:
      with condition:
        acquisition_cpt = acquisition_cpt + 1
        #print("acquisition_cpt ",acquisition_cpt)
        condition.notify()

def data_processing(cap,out,condition):
  global processing_cpt
  print("data_processing thread")
  while True:
    with condition:
      condition.wait()
      img = frame.copy()
    processing_cpt = processing_cpt + 1
    #print("processing_cpt ",processing_cpt)
    out.write(img)

if __name__ == '__main__':
  cap =  cv2.VideoCapture('udpsrc port=5200 ! h264parse ! omxh264dec ! videoconvert ! video/x-raw, format=(string)BGR ! appsink', cv2.CAP_GSTREAMER);
  out =  cv2.VideoWriter('appsrc ! videoconvert ! omxh264enc ! video/x-h264,profile=high ! udpsink host=127.0.0.1 port=5210',cv2.CAP_GSTREAMER,0, 30, (640,480), True)
  time.sleep(0.1)
  if not cap.isOpened() or not out.isOpened():
    print("not opened")
    quit()
  threads = []
  condition = threading.Condition()
  for func in [data_acquisition, data_processing]:
    threads.append(threading.Thread(target=func, args=(cap,out,condition)))
    threads[-1].start() 
  for thread in threads:
    thread.join()
  cap.release()

create ~/opencv_test/test.cpp

#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**)
{
  Mat data;
  string streamInGstStr="udpsrc port=5200 ! h264parse ! omxh264dec ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
  string streamOutGstStr="appsrc ! videoconvert ! omxh264enc ! video/x-h264,profile=high ! udpsink host=127.0.0.1 port=5210";

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

  if (streamIn.isOpened() && streamOut.isOpened()) {
    while (true) {
      streamIn.read(data);
      if (!data.empty()) {
        streamOut.write(data);
      }
    }
  }
  return 0;
}
g++ -g ~/opencv_test/test.cpp -o ~/opencv_test/test `pkg-config --cflags --libs opencv4` 

Run following script with ~/opencv_test/test and ~/opencv_test/ptest.py

#!/bin/bash
sudo killall raspivid gst-launch test-launch test ptest.py \
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 \
   ! udpsink host=127.0.0.1 port=5200 &
sleep 1
#~/opencv_test/ptest.py &
~/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" \
"udpsrc port=5210 do-timestamp=true ! video/x-h264,stream-format=byte-stream,alignment=au ! 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/test2 ! rtph264depay ! avdec_h264 !  xvimagesink sync=false

Explorer/RaspberryPi/Visualprocessing/Opencv/Optimisation