{"id":377,"date":"2019-09-21T17:08:09","date_gmt":"2019-09-21T17:08:09","guid":{"rendered":"https:\/\/carson.fenimorefamily.com\/?p=377"},"modified":"2019-09-21T17:08:09","modified_gmt":"2019-09-21T17:08:09","slug":"installing-opencv-4-1-1-on-raspberry-pi-4","status":"publish","type":"post","link":"https:\/\/carson.fenimorefamily.com\/?p=377","title":{"rendered":"Installing OpenCV 4.1.1 on Raspberry Pi 4"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Recently I purchased a raspberry pi 4 to see how it performs basic computer vision tasks.  I largely followed this guide on building opencv https:\/\/www.learnopencv.com\/install-opencv-4-on-raspberry-pi\/<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, in the guide there are several missing dependencies on a fresh version of raspbian buster.  There are also some apparent errors in the CMakeLists.txt file which other users already discovered.  After patching these fixes and adding the needed missing dependencies, I now have opencv running on my pi.   Here&#8217;s my complete script below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IMPORTANT: You must READ the script, don&#8217;t just run it!  For example, you should check the version of python you have at the time you run this script.  When I ran it i was at python 3.7.  Also, feel free to bump to a later version (or master) of opencv.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Oh, also during the numpy step it hung and i was too lazy to look into it.  It didn&#8217;t seem to affect my ability to use opencv &#8211; so i didn&#8217;t go back and dig.  My bad.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">#!\/bin\/bash\n\ncvVersion=\"4.1.1\"\npythonVersion=\"3.7\"\nopencvDirRoot=\/home\/pi\/opencv\n\nsudo apt-get -y purge wolfram-engine\nsudo apt-get -y purge libreoffice*\nsudo apt-get -y clean\nsudo apt-get -y autoremove\n\nmkdir -p $opencvDirRoot\ncd $opencvDirRoot\n\n# Clean build directories\nrm -rf opencv\/build\nrm -rf opencv_contrib\/build\n\n# Create directory for installation\nrm -fr installation\nmkdir -p installation\nmkdir installation\/OpenCV-\"$cvVersion\"\n\n\nsudo apt -y update\nsudo apt -y upgrade\nsudo apt-get -y remove x264 libx264-dev\n \n## Install dependencies\nsudo apt-get install libblas-dev liblapack-dev\nsudo apt-get install libeigen3-dev\nsudo apt-get -y install qtbase5-dev qtdeclarative5-dev\nsudo apt-get -y install build-essential checkinstall cmake pkg-config yasm\nsudo apt-get -y install git gfortran\nsudo apt-get -y install libjpeg8-dev libjasper-dev libpng12-dev\n\n \nsudo apt-get -y install libtiff5-dev\n \nsudo apt-get -y install libtiff-dev\n\nsudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev\nsudo apt-get -y install libxine2-dev libv4l-dev\ncd \/usr\/include\/linux\nsudo ln -s -f ..\/libv4l1-videodev.h videodev.h\ncd $opencvDirRoot\n\nsudo apt-get -y install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev\nsudo apt-get -y install libgtk2.0-dev libtbb-dev qt5-default\nsudo apt-get -y install libatlas-base-dev\nsudo apt-get -y install libmp3lame-dev libtheora-dev\nsudo apt-get -y install libvorbis-dev libxvidcore-dev libx264-dev\nsudo apt-get -y install libopencore-amrnb-dev libopencore-amrwb-dev\nsudo apt-get -y install libavresample-dev\nsudo apt-get -y install x264 v4l-utils\nsudo apt-get -y install libmesa-dev\nsudo apt-get -y install freeglut3-dev\n\n\n\n# Optional dependencies\nsudo apt-get -y install libprotobuf-dev protobuf-compiler\nsudo apt-get -y install libgoogle-glog-dev libgflags-dev\nsudo apt-get -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen\n\nsudo apt-get -y install python3-dev python3-pip\nsudo -H pip3 install -U pip numpy\nsudo apt-get -y install python3-testresources\n\ncd $opencvDirRoot\n# Install virtual environment\npython3 -m venv OpenCV-\"$cvVersion\"-py3\necho \"# Virtual Environment Wrapper\" >> ~\/.bashrc\necho \"alias workoncv-$cvVersion=\\\"source $opencvDirRoot\/OpenCV-$cvVersion-py3\/bin\/activate\\\"\" >> ~\/.bashrc\nsource \"$opencvDirRoot\"\/OpenCV-\"$cvVersion\"-py3\/bin\/activate\n#############\n\n############ For Python 3 ############\n# now install python libraries within this virtual environment\nsudo sed -i 's\/CONF_SWAPSIZE=100\/CONF_SWAPSIZE=1024\/g' \/etc\/dphys-swapfile\nsudo \/etc\/init.d\/dphys-swapfile stop\nsudo \/etc\/init.d\/dphys-swapfile start\npip install numpy dlib\n# quit virtual environment\ndeactivate\n\ngit clone https:\/\/github.com\/opencv\/opencv.git\ncd opencv\ngit checkout $cvVersion\ncd ..\n\ngit clone https:\/\/github.com\/opencv\/opencv_contrib.git\ncd opencv_contrib\ngit checkout $cvVersion\ncd ..\n\ncd opencv\nmkdir build\ncd build\n\n\n# Eigen\/Core to eigen3\/Eigen\/Core\nsed -i s,Eigen\/Core,eigen3\/Eigen\/Core\/g ..\/modules\/core\/include\/opencv2\/core\/private.hpp\n\n# Add these to  opencv\/samples\/cpp\/CMakeLists.txt \nfind_package(OpenGL REQUIRED)\nfind_package(GLUT REQUIRED)\n\ncmake .. -D CMAKE_BUILD_TYPE=RELEASE \\\n            -D CMAKE_INSTALL_PREFIX=$opencvDirRoot\/installation\/OpenCV-\"$cvVersion\" \\\n            -D INSTALL_C_EXAMPLES=ON \\\n            -D INSTALL_PYTHON_EXAMPLES=ON \\\n            -D WITH_TBB=ON \\\n            -D WITH_V4L=ON \\\n            -D OPENCV_PYTHON3_INSTALL_PATH=$opencvDirRoot\/OpenCV-$cvVersion-py3\/lib\/python$pythonVersion\/site-packages \\\n        -D WITH_QT=ON \\\n        -D WITH_OPENGL=ON \\\n    -D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \\\n        -D OPENCV_EXTRA_MODULES_PATH=..\/..\/opencv_contrib\/modules \\\n        -D BUILD_EXAMPLES=ON\n\n\nmake -j$(nproc)\nmake install\n\n\nsudo sed -i 's\/CONF_SWAPSIZE=1024\/CONF_SWAPSIZE=100\/g' \/etc\/dphys-swapfile\nsudo \/etc\/init.d\/dphys-swapfile stop\nsudo \/etc\/init.d\/dphys-swapfile start\n\necho \"sudo modprobe bcm2835-v4l2\" >> ~\/.profile\n                                                                                                                                        <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I purchased a raspberry pi 4 to see how it performs basic computer vision tasks. I largely followed this guide on building opencv https:\/\/www.learnopencv.com\/install-opencv-4-on-raspberry-pi\/ However, in the guide there are several missing dependencies on a fresh version of raspbian buster. There are also some apparent errors in the CMakeLists.txt file which other users already [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,19],"tags":[],"class_list":["post-377","post","type-post","status-publish","format-standard","hentry","category-computer-vision","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=377"}],"version-history":[{"count":0,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/posts\/377\/revisions"}],"wp:attachment":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}