├── .gitignore ├── LICENSE ├── README.md ├── build_information.txt ├── debs ├── OpenCV-4.4.0-armv7l-dev.deb ├── OpenCV-4.4.0-armv7l-libs.deb ├── OpenCV-4.4.0-armv7l-licenses.deb ├── OpenCV-4.4.0-armv7l-main.deb ├── OpenCV-4.4.0-armv7l-python.deb └── OpenCV-4.4.0-armv7l-scripts.deb └── tests ├── cpp_opencv_test ├── CMakeLists.txt ├── main.cpp └── tetris_blocks.png └── python_opencv_test ├── test.py └── tetris_blocks.png /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeLists.txt.user 2 | .clang-format 3 | build/ 4 | .idea/ 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2020, Dario Limongi 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Faster OpenCV for Raspberry Pi 2 | Leverage all your CPUs power in OpenCV by using **TBB**, **Neon** and **VFPV3** libraries. 3 | 4 | Since I've already compiled this on my own Raspberry Pi I made it available on GitHub. 5 | 6 | Save countless of compile time by just installing these debs. Enjoy! 7 | 8 | ## What is this? 9 | * A pre-compiled **OpenCV 4.4.0** for **Raspberry Pi** optimized for deep learning / computer vision applications (**NEON**, **VFPV3**, **TBB** turned on). 10 | * Bindings for **Python 2** and **Python 3** are also included. 11 | * For detailed build informations click [here](build_information.txt). 12 | * Created with OpenCV cpack targets. 13 | * Tested on **Raspberry Pi 3** using **Raspbian Buster (Debian 10)** (for Raspbian Stretch click [here](https://github.com/dlime/Faster_OpenCV_4_Raspberry_Pi/releases/tag/stretch_410)). 14 | 15 | ## How much faster? 16 | Performance tests have been made in this [great blog article](https://www.pyimagesearch.com/2017/10/09/optimizing-opencv-on-the-raspberry-pi/) which led to an approximate **30%** increase in speed and of over **48%** when applied strictly to DNN module. 17 | 18 | Another performance test is available [here](https://www.theimpossiblecode.com/blog/faster-opencv-smiles-tbb/) which also led to about **30%** increase in speed. 19 | 20 | ## How to use it? 21 | Install OpenCV library prerequisites on your Raspberry Pi. 22 | ``` 23 | sudo apt-get update && sudo apt-get upgrade -y 24 | sudo apt-get install -y \ 25 | libjpeg-dev libpng-dev libtiff-dev libgtk-3-dev \ 26 | libavcodec-extra libavformat-dev libswscale-dev libv4l-dev \ 27 | libxvidcore-dev libx264-dev libjasper1 libjasper-dev \ 28 | libatlas-base-dev gfortran libeigen3-dev libtbb-dev 29 | ``` 30 | 31 | Install **numpy** based on your target Python version: 32 | ``` 33 | sudo apt-get install -y python3-dev python3-numpy 34 | ``` 35 | or: 36 | ``` 37 | sudo apt-get install -y python-dev python-numpy 38 | ``` 39 | 40 | ### How to install? 41 | Clone the repo into your Raspberry Pi and install all debs: 42 | ``` 43 | git clone https://github.com/dlime/Faster_OpenCV_4_Raspberry_Pi.git 44 | cd Faster_OpenCV_4_Raspberry_Pi/debs 45 | sudo dpkg -i OpenCV*.deb 46 | sudo ldconfig 47 | ``` 48 | 49 | ### How to test? 50 | 51 | #### C++ 52 | Test the installation by going to tests cpp test folder, build it and launch the executable: 53 | ``` 54 | cd Faster_OpenCV_4_Raspberry_Pi/tests/cpp_opencv_test 55 | mkdir build && cd build 56 | cmake .. 57 | make -j`cat /proc/cpuinfo | grep -c 'processor'` 58 | ./cpp_opencv_test 59 | ``` 60 | 61 | #### Python 62 | Test the installation by going to tests folder and launch the test.py file: 63 | ``` 64 | cd Faster_OpenCV_4_Raspberry_Pi/tests/python_opencv_test 65 | python test.py 66 | ``` 67 | 68 | You shouldn't see any error messages in console and an image with tetris blocks with contours drawed should appear. 69 | 70 | ### How to uninstall? 71 | Run the following command in your Raspberry Pi terminal: 72 | ``` 73 | sudo apt purge opencv-* 74 | ``` 75 | -------------------------------------------------------------------------------- /build_information.txt: -------------------------------------------------------------------------------- 1 | General configuration for OpenCV 4.4.0 ===================================== 2 | Version control: 4.4.0 3 | 4 | Extra modules: 5 | Location (extra): /home/pi/opencv-build/opencv_contrib-4.4.0/modules 6 | Version control (extra): unknown 7 | 8 | Platform: 9 | Timestamp: 2020-09-19T10:38:03Z 10 | Host: Linux 5.4.51-v7+ armv7l 11 | CMake: 3.13.4 12 | CMake generator: Ninja 13 | CMake build tool: /usr/bin/ninja 14 | Configuration: Release 15 | 16 | CPU/HW features: 17 | Baseline: VFPV3 NEON 18 | requested: DETECT 19 | required: VFPV3 NEON 20 | 21 | C/C++: 22 | Built as dynamic libs?: YES 23 | C++ standard: 11 24 | C++ Compiler: /usr/bin/c++ (ver 8.3.0) 25 | C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG 26 | C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG 27 | C Compiler: /usr/bin/cc 28 | C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG 29 | C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG 30 | Linker flags (Release): -Wl,--gc-sections -Wl,--as-needed 31 | Linker flags (Debug): -Wl,--gc-sections -Wl,--as-needed 32 | ccache: YES 33 | Precompiled headers: NO 34 | Extra dependencies: dl m pthread rt 35 | 3rdparty dependencies: 36 | 37 | OpenCV modules: 38 | To be built: alphamat aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab world xfeatures2d ximgproc xobjdetect xphoto 39 | Disabled: - 40 | Disabled by dependency: - 41 | Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv hdf java js julia matlab ovis sfm ts viz 42 | Applications: apps 43 | Documentation: NO 44 | Non-free algorithms: YES 45 | 46 | GUI: 47 | GTK+: YES (ver 3.24.5) 48 | GThread : YES (ver 2.58.3) 49 | GtkGlExt: NO 50 | VTK support: NO 51 | 52 | Media I/O: 53 | ZLib: /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.11) 54 | JPEG: /usr/lib/arm-linux-gnueabihf/libjpeg.so (ver 62) 55 | WEBP: build (ver encoder: 0x020f) 56 | PNG: /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.6.36) 57 | TIFF: /usr/lib/arm-linux-gnueabihf/libtiff.so (ver 42 / 4.1.0) 58 | JPEG 2000: /usr/lib/arm-linux-gnueabihf/libjasper.so (ver 1.900.1) 59 | OpenEXR: build (ver 2.3.0) 60 | HDR: YES 61 | SUNRASTER: YES 62 | PXM: YES 63 | PFM: YES 64 | 65 | Video I/O: 66 | DC1394: NO 67 | FFMPEG: YES 68 | avcodec: YES (58.35.100) 69 | avformat: YES (58.20.100) 70 | avutil: YES (56.22.100) 71 | swscale: YES (5.3.100) 72 | avresample: NO 73 | GStreamer: NO 74 | v4l/v4l2: YES (linux/videodev2.h) 75 | 76 | Parallel framework: TBB (ver 2018.0 interface 10006) 77 | 78 | Trace: YES (with Intel ITT) 79 | 80 | Other third-party libraries: 81 | Lapack: NO 82 | Eigen: YES (ver 3.3.7) 83 | Custom HAL: YES (carotene (ver 0.0.1)) 84 | Protobuf: build (3.5.1) 85 | 86 | OpenCL: YES (no extra features) 87 | Include path: /home/pi/opencv-build/opencv-4.4.0/3rdparty/include/opencl/1.2 88 | Link libraries: Dynamic load 89 | 90 | Python 2: 91 | Interpreter: /usr/bin/python2.7 (ver 2.7.16) 92 | Libraries: /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.16) 93 | numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.2) 94 | install path: lib/python2.7/dist-packages/cv2/python-2.7 95 | 96 | Python 3: 97 | Interpreter: /usr/bin/python3 (ver 3.7.3) 98 | Libraries: /usr/lib/arm-linux-gnueabihf/libpython3.7m.so (ver 3.7.3) 99 | numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.16.2) 100 | install path: lib/python3.7/dist-packages/cv2/python-3.7 101 | 102 | Python (for build): /usr/bin/python2.7 103 | 104 | Java: 105 | ant: NO 106 | JNI: NO 107 | Java wrappers: NO 108 | Java tests: NO 109 | 110 | Install to: /usr/local 111 | ----------------------------------------------------------------- 112 | 113 | -------------------------------------------------------------------------------- /debs/OpenCV-4.4.0-armv7l-dev.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/debs/OpenCV-4.4.0-armv7l-dev.deb -------------------------------------------------------------------------------- /debs/OpenCV-4.4.0-armv7l-libs.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/debs/OpenCV-4.4.0-armv7l-libs.deb -------------------------------------------------------------------------------- /debs/OpenCV-4.4.0-armv7l-licenses.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/debs/OpenCV-4.4.0-armv7l-licenses.deb -------------------------------------------------------------------------------- /debs/OpenCV-4.4.0-armv7l-main.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/debs/OpenCV-4.4.0-armv7l-main.deb -------------------------------------------------------------------------------- /debs/OpenCV-4.4.0-armv7l-python.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/debs/OpenCV-4.4.0-armv7l-python.deb -------------------------------------------------------------------------------- /debs/OpenCV-4.4.0-armv7l-scripts.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/debs/OpenCV-4.4.0-armv7l-scripts.deb -------------------------------------------------------------------------------- /tests/cpp_opencv_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | set(CMAKE_CXX_STANDARD 11) 4 | set(CMAKE_CXX_STANDARD_REQUIRED TRUE) 5 | 6 | project(cpp_opencv_test) 7 | 8 | find_package(OpenCV REQUIRED) 9 | 10 | message(STATUS "OpenCV library status:") 11 | message(STATUS " version: ${OpenCV_VERSION}") 12 | message(STATUS " config: ${OpenCV_DIR}") 13 | message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") 14 | 15 | file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tetris_blocks.png" DESTINATION "${CMAKE_BINARY_DIR}") 16 | add_executable(cpp_opencv_test main.cpp) 17 | target_link_libraries(cpp_opencv_test ${OpenCV_LIBS}) 18 | -------------------------------------------------------------------------------- /tests/cpp_opencv_test/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: Dario Limongi 3 | * License: BSD-2 (please check LICENSE file in repo root.) 4 | * Adapted from: https://www.pyimagesearch.com/2015/08/10/checking-your-opencv-version-using-python/ 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | namespace 12 | { 13 | const int CURRENT_OPENCV_BUILD_MAJOR{ 4 }; 14 | const int CURRENT_OPENCV_BUILD_MINOR{ 4 }; 15 | const int CURRENT_OPENCV_BUILD_REVISION = { 0 }; 16 | const std::string IMAGE{ "tetris_blocks.png" }; 17 | } 18 | 19 | bool 20 | check_build_info( ) 21 | { 22 | bool error_found{ false }; 23 | 24 | std::cout << "OpenCV version: " << cv::getVersionMajor( ) << "." << cv::getVersionMinor( ) << "." 25 | << cv::getVersionRevision( ) << std::endl; 26 | if ( cv::getVersionMajor( ) != CURRENT_OPENCV_BUILD_MAJOR || cv::getVersionMinor( ) != CURRENT_OPENCV_BUILD_MINOR || 27 | cv::getVersionRevision( ) != CURRENT_OPENCV_BUILD_REVISION ) { 28 | std::cout << "ERROR: OpenCV version is different than the expected." << std::endl; 29 | error_found = true; 30 | } 31 | 32 | std::cout << "Available CPUs: " << cv::getNumberOfCPUs( ) << std::endl; 33 | std::cout << "Available threads: " << cv::getNumThreads( ) << std::endl; 34 | if ( cv::getNumThreads( ) < cv::getNumberOfCPUs( ) ) { 35 | std::cout << "ERROR: TBB is not enabled." << std::endl; 36 | error_found = true; 37 | } 38 | 39 | std::cout << "Cpu NEON support: " << cv::checkHardwareSupport( CV_CPU_NEON ) << std::endl; 40 | if ( cv::checkHardwareSupport( CV_CPU_NEON ) == 0 ) { 41 | std::cout << "ERROR: NEON is not enabled." << std::endl; 42 | error_found = true; 43 | } 44 | 45 | return error_found; 46 | } 47 | 48 | int 49 | main( ) 50 | { 51 | if ( !check_build_info( ) ) { 52 | std::cout << "SUCCESS: You are using latest OpenCV version, with NEON/TBB support enabled." << std::endl; 53 | } else { 54 | std::cout << "FAIL: You are using wrong OpenCV version or without NEON/TBB support enabled." << std::endl; 55 | return 1; 56 | } 57 | 58 | auto image = cv::imread( IMAGE ); 59 | if ( !image.data ) { 60 | std::cout << "ERROR: Could not open or find the image" << std::endl; 61 | return 1; 62 | } 63 | 64 | cv::Mat gray_image, thresholded_image; 65 | cv::cvtColor( image, gray_image, cv::COLOR_BGR2GRAY ); 66 | cv::threshold( gray_image, thresholded_image, 225, 255, cv::THRESH_BINARY_INV ); 67 | 68 | std::vector< std::vector< cv::Point > > contours; 69 | cv::findContours( thresholded_image, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE ); 70 | cv::drawContours( image, contours, -1, cv::Scalar( 240, 0, 159 ), 3 ); 71 | 72 | cv::imshow( "Image", image ); 73 | cv::waitKey( 0 ); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /tests/cpp_opencv_test/tetris_blocks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/tests/cpp_opencv_test/tetris_blocks.png -------------------------------------------------------------------------------- /tests/python_opencv_test/test.py: -------------------------------------------------------------------------------- 1 | # Author: Dario Limongi 2 | # License: BSD-2 (please check LICENSE file in repo root.) 3 | # Adapted from: https://www.pyimagesearch.com/2015/08/10/checking-your-opencv-version-using-python/ 4 | 5 | from __future__ import print_function 6 | import cv2 7 | import sys 8 | 9 | CURRENT_OPENCV_BUILD_VERSION = [4, 4, 0] 10 | 11 | 12 | def check_build_info(): 13 | success = True 14 | 15 | print("OpenCV Version: {}".format(cv2.__version__)) 16 | if (cv2.getVersionMajor() != CURRENT_OPENCV_BUILD_VERSION[0]) and ( 17 | cv2.getVersionMinor() != CURRENT_OPENCV_BUILD_VERSION[1]) and ( 18 | cv2.getVersionRevision() != CURRENT_OPENCV_BUILD_VERSION[2]): 19 | print("ERROR: OpenCV version is different than the expected.") 20 | success = False 21 | 22 | print("Available CPUs: ", cv2.getNumberOfCPUs()) 23 | print("Available threads: ", cv2.getNumThreads()) 24 | if cv2.getNumThreads() < cv2.getNumberOfCPUs(): 25 | print("ERROR: TBB is not enabled.") 26 | success = False 27 | 28 | cv2.CPU_NEON = 100 # Value taken from OpenCV doc. CPU labels don't work correctly in Python 29 | print("Cpu NEON support: ", cv2.checkHardwareSupport(cv2.CPU_NEON)) 30 | if not cv2.checkHardwareSupport(cv2.CPU_NEON): 31 | print("ERROR: NEON is not enabled.") 32 | success = False 33 | 34 | return success 35 | 36 | 37 | def main(): 38 | if not check_build_info(): 39 | print("FAIL: You are using OpenCV without NEON/TBB support enabled.") 40 | sys.exit(1) 41 | print("SUCCESS: You are using latest OpenCV version, with NEON/TBB support enabled.") 42 | 43 | image = cv2.imread("tetris_blocks.png") 44 | if not image.data: 45 | print("ERROR: Could not open or find the image") 46 | sys.exit(1) 47 | gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 48 | threshold = cv2.threshold(gray_image, 225, 255, cv2.THRESH_BINARY_INV)[1] 49 | (contours, _) = cv2.findContours(threshold.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 50 | 51 | # draw the contours on the image 52 | cv2.drawContours(image, contours, -1, (240, 0, 159), 3) 53 | cv2.imshow("Image", image) 54 | cv2.waitKey(0) 55 | 56 | 57 | if __name__ == '__main__': 58 | main() 59 | -------------------------------------------------------------------------------- /tests/python_opencv_test/tetris_blocks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dlime/Faster_OpenCV_4_Raspberry_Pi/a083a955421a87ac01072794034e1153687f8468/tests/python_opencv_test/tetris_blocks.png --------------------------------------------------------------------------------