Mediapipe needs OpenCV and FFmpeg libraries. You can install both of them from official raspberry pi repository :
sudo apt install ffmpeg python3-opencv
But these are built with less optimisations. These can be built with the supported optimisations as explained here.
First, Install Bazel by following the steps here.
Install the required dependencies :
sudo apt install python3-dev protobuf-compiler
sudo apt install libopenexr-dev libopenexr23 libdc1394-22 libdc1394-22-dev libeigen3-dev
sudo apt remove libavcodec-dev libavutil-dev libavformat-dev libswscale-dev libavutil56
Remove exception statements from ImathVec.h
& ImathMatrix.h
present in /usr/include/OpenEXR/
to avoid error: ISO C++17 does not allow dynamic exception specifications
sudo sed -i "s/throw (IEX_NAMESPACE::MathExc)/ /g" /usr/include/OpenEXR/ImathVec.h
sudo sed -i "s/throw (IEX_NAMESPACE::MathExc)/ /g" /usr/include/OpenEXR/ImathMatrix.h
Download the source code
wget https://github.com/google/mediapipe/archive/refs/tags/v0.8.8.tar.gz -O mediapipe-0.8.8.tar.gz
tar xvf mediapipe-0.8.8.tar.gz
cd mediapipe-0.8.8
Skip this step if mediapipe version > 0.8.6.
Make the changes in opencv_linux.BUILD
& ffmpeg_linux.BUILD
by pointing to opencv & ffmpeg libraries
sed -i "s/x86_64-linux-gnu/arm-linux-gnueabihf/g" third_party/opencv_linux.BUILD
sed -i "s/x86_64-linux-gnu/arm-linux-gnueabihf/g" third_party/ffmpeg_linux.BUILD
In third_party/opencv_linux.BUILD
comment "include/opencv2/**/*.h*",
& uncomment "include/opencv4/opencv2/**/*.h*"
under hdrs
. Similarly comment "include/",
& uncomment "include/opencv4/",
under includes
.
Add the following in third_party/BUILD
after "WITH_WEBP": "OFF",
"CMAKE_CXX_FLAGS": "-march=armv8-a+crc+simd -mcpu=cortex-a72 -mfpu=neon-fp-armv8 -mtune=cortex-a72 -mfloat-abi=hard -O3",
"ENABLE_NEON": "ON",
"WITH_TENGINE": "ON",
"ENABLE_VFPV4": "ON",
"WITH_CAROTENE": "OFF",
"CMAKE_CXX_FLAGS": "-march=armv8-a+crc -mfpu=neon-vfpv4 -mtune=cortex-a53 -ftree-vectorize -mfloat-abi=hard -O3",
"ENABLE_NEON": "ON",
"WITH_TENGINE": "ON",
"ENABLE_VFPV4": "ON",
"WITH_CAROTENE": "OFF",
Add these flags to enable neon and optimisations in setup.py
after '--compilation_mode=opt',
as posted by arron2003 here.
Note: Add them in function bazel_build
inside class BuildBazelExtension
after '--compilation_mode=opt'
.
'--copt=-march=armv7-a',
'--copt=-mfpu=neon-vfpv3',
'--copt=-mcpu=cortex-a72',
'--copt=-mtune=cortex-a72',
'--copt=-mfloat-abi=hard',
'--copt=-O3',
'--copt=-march=armv7-a',
'--copt=-mfpu=neon-vfpv3',
'--copt=-mtune=cortex-a53',
'--copt=-ftree-vectorize',
'--copt=-mfloat-abi=hard',
'--copt=-O3',
Build the package
python3 setup.py gen_protos && python3 setup.py bdist_wheel
Install required python libraries
sudo python3 -m pip install absl-py attrs>=19.1.0 numpy protobuf>=3.11.4 six wheel matplotlib
Install it
cd dist; sudo python3 -m pip install mediapipe-0.8-cp37-cp37m-linux_armv7l.whl --no-deps
I recommend you to build the packages yourself. In case, you want to skip it. You can download pre-built packages.
sudo apt install libxcb-shm0 libcdio-paranoia-dev libsdl2-2.0-0 libxv1 libtheora0 libva-drm2 libva-x11-2 libvdpau1 libharfbuzz0b libbluray2
sudo apt install python3-pip libatlas-base-dev libhdf5-103 libgtk-3-0 libdc1394-22 libopenexr23
sudo python3 -m pip install absl-py attrs>=19.1.0 numpy protobuf>=3.11.4 six wheel matplotlib
sudo dpkg -i fdk-aac_2.0.2-1_armhf.deb libass_0.15.1-1_armhf.deb mp3lame_3.100-1_armhf.deb x264_0.163-1_armhf.deb
sudo dpkg -i ffmpeg_4.4.1-1_armhf.deb opencv_4.5.4-1_armhf.deb
sudo python3 -m pip install mediapipe-0.8.8-cp37-cp37m-linux_armv7l.whl --no-deps
sudo apt remove fdk-aac mp3lame libass x264 ffmpeg opencv
sudo python3 -m pip uninstall mediapipe