MobileNetV2-SSD Lite's pre-processing, post-processing, etc. are partly converted into a C ++ shared library and called from Python.

  1. Introduction This is my own development memo. Make a note of the results of learning how to call a C ++ shared library from Python to partially accelerate the pre- and post-processing of your Raspberry Pi 4 deep learning program. This time we will use a library called ** boost_python3 ** to create a shared library for running Python. It took me a while to find out the dependencies between the header file include and the shared libraries, but once I knew what I was doing, it was really easy to do.

  2. Environment

  1. Procedure 3-1. Tutorial implementation Preparing the environment

Install_boost-python


$ sudo apt-get update
$ sudo apt-get install libboost-all-dev python3-dev

Creating a tasting program

python


$ nano CModule.cpp

CModule.cpp


#include <boost/python.hpp>

std::string hello() {
    return "hello world";
}

BOOST_PYTHON_MODULE(CModule) {
    using namespace boost::python;
    def("hello", &hello);
}

Compiling the tasting program

compile


$ g++ -I/usr/include/aarch64-linux-gnu/python3.7m \
      -I/usr/include/python3.7m \
      -DPIC \
      -shared \
      -fPIC \
      -o CModule.so \
      CModule.cpp \
      -lboost_python3

Call test of shared library for tasting (CModule.so) from Python

test


$ python3
>>> import CModule
>>> CModule.hello()
'hello world'

3-2. Pre-processing test implementation and operation verification Simple implementation of preprocessing program in C ++

Edit


$ nano preprocessing.cpp

preprocessing.cpp


#include <string>
#include <stdio.h>
#include <boost/python.hpp>
#include <opencv2/opencv.hpp>

void resize_and_normalize(std::string image_file, int width, int height) {
    cv::Mat image = cv::imread(image_file, 1), prepimage;
    cv::resize(image, prepimage, cv::Size(width, height));
    cv::imshow("InputImage", prepimage);
    cv::waitKey(0);
}

BOOST_PYTHON_MODULE(preprocessing) {
    using namespace boost::python;
    def("resize_and_normalize", &resize_and_normalize);
}

Compile the created preprocessing program A shared library (.so) is generated

compile_ubuntu1910_aarch64


$ g++ -I/usr/include/aarch64-linux-gnu/python3.7m \
      -I/usr/include/python3.7m \
      -I/usr/local/include/opencv4 \
      -DPIC \
      -shared \
      -fPIC \
      -o preprocessing.so \
      preprocessing.cpp \
      -lboost_python3 \
      -L/usr/local/lib \
      -lopencv_core \
      -lopencv_imgcodecs \
      -lopencv_highgui

compile_ubuntu1804_x86_64


$ g++ -I/usr/include/x86_64-linux-gnu/python3.6m/ \
      -I/usr/include/python3.6m \
      -I/opt/intel/openvino_2019.3.376/opencv/include \
      -DPIC \
      -shared \
      -fPIC \
      -o preprocessing.so \
      preprocessing.cpp \
      -lboost_python3 \
      -L/opt/intel/openvino_2019.3.376/opencv/lib \
      -lopencv_core \
      -lopencv_imgcodecs \
      -lopencv_highgui

Screenshot 2020-02-06 18:08:23.png

Creating a test Python program

Edit


$ nano test.py

test.py


import preprocessing
preprocessing.resize_and_normalize("dog.jpg ", 300, 300)

Execution


$ python3 test.py

Screenshot 2020-02-06 18:05:15.png

  1. Reference articles

  2. ** Publish C ++ to python3 on ubuntu --Qiita --mink0212 **

  3. ** Create a library for Python in C ++ **

  4. ** Python Tips: I want to find out the location of library modules **

  5. ** A brief summary of Linux shared libraries **

  6. https://stackoverflow.com/questions/51308292/swig-linker-undefined-symbol-zn2cv8fastfreeepv-cvfastfreevoid

  7. ** Basic processing of cv :: Mat **

  8. CV_Assert, CV_DbgAssert, CV_StaticAssert

  9. ** Do not write double loops in image processing as much as possible --Qiita --nonbiri15 **

Recommended Posts

MobileNetV2-SSD Lite's pre-processing, post-processing, etc. are partly converted into a C ++ shared library and called from Python.
Call your own C language shared library from Python using ctypes