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.
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
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
Reference articles
** Python Tips: I want to find out the location of library modules **
https://stackoverflow.com/questions/51308292/swig-linker-undefined-symbol-zn2cv8fastfreeepv-cvfastfreevoid
** Do not write double loops in image processing as much as possible --Qiita --nonbiri15 **