├── .gitattributes ├── .gitignore ├── Readme.md ├── cv3-install-menu.conf ├── cv3-install-menu.sh ├── dphys-swapfile.1024 ├── menu.png └── setup.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | ## Raspberry Pi Menu Driven OpenCV 3 Compile from Source Script 2 | #### Whiptail menu enabled script to help compile opencv3 from source 3 | 4 | ![cv3-install-menu](https://github.com/pageauc/opencv3-setup/blob/master/menu.png) 5 | 6 | ## Quick Install 7 | For system requirements See [***Prerequisites***](https://github.com/pageauc/opencv3-setup#prerequisites) 8 | 9 | ***Step1*** - Use mouse to highlight command below, Then right click copy on highlighted command 10 | ***Step2*** - On a logged in RPI SSH terminal session right click paste then Enter to Run 11 | [***setup.sh***](https://github.com/pageauc/opencv3-setup/blob/master/setup.sh) script 12 | 13 | curl -L https://raw.github.com/pageauc/opencv3-setup/master/setup.sh | bash 14 | 15 | The curl comand will run the GitHub [***setup.sh***](https://github.com/pageauc/opencv3-setup/blob/master/setup.sh) 16 | script that will install files and configure into the ***~/opencv3-setup*** folder. 17 | If you want to review code before running see [***Manual Install***](https://github.com/pageauc/opencv3-setup#manual-install) 18 | 19 | ## How To Run Menu 20 | 21 | cd ~/opencv3-setup 22 | ./cv3-install-menu.sh 23 | 24 | Start at Step ***1 UPDATE*** menu pick and follow instructions. You will be prompted to optionally reboot. 25 | For details see [***How to Run Menu Picks***](https://github.com/pageauc/opencv3-setup#how-to-run-menu-picks) 26 | To change cv3_tmp folder location or opencv version see 27 | [***How to Change Configuration***](https://github.com/pageauc/opencv3-setup#how-to-change-configuration) 28 | 29 | ## Operation 30 | After update/upgrade and optional reboot is complete, Select **DEP** menu pick. If there are no problems, 31 | you will be prompted with a simple y/n to continue without having to return to the main menu. If there are 32 | issues that need to be resolved you can continue by selecting the appropriate main menu pick. 33 | 34 | The [***cv3-install-menu.sh***](https://github.com/pageauc/opencv3-setup/blob/master/cv3-install-menu.sh) script will 35 | 36 | * Online validate that ***OPENCV_VER*** variable setting is correct 37 | * Check if ***INSTALL_DIR*** variable exists and points to a Non Fat File System. 38 | * Creates ***cv3-log.txt*** file If It Does Not Exist. Records system information, date/times of steps including execution time. 39 | * ***UPDATE*** menu pick to update/upgrade Raspbian. Prompts for optional reboot. 40 | * ***DEP*** menu pick to Install build dependencies, Download opencv3 source zip files and unzip 41 | * Auto Detect RPI3 and set NEON compile directive for cmake to enhance cv3 performance. Off for non RPI 3's 42 | * ***COMPILE*** menu pick will Run ***cmake*** to check and configure build 43 | * Temporarily Increase Swap memory to 1024 MB During make. Returned to original state after make. 44 | * Auto Detect Total RAM memory and set compile cores. -j2 for 1 GB, -j1 for the Rest 45 | * Run ***make*** to Compile opencv3 source code 46 | * ***INSTALL*** Menu pick will Run ***make install*** to install new opencv python ***.so*** files to production. 47 | * Optional run ***make clean*** to clear build directory to force full recompile. 48 | * ***DELETE*** menu pick to optionally recover disk space by deleting the ***tmp_cv3*** folder containing 49 | opencv source, build files and folders. 50 | * ***SETTINGS*** menu pick to nano edit the [***cv3-install-menu.conf***](https://github.com/pageauc/opencv3-setup/blob/master/cv3-install-menu.conf) file 51 | 52 | ## Prerequisites 53 | * Basic knowledge of unix terminal commands. 54 | There are some optional configuration steps that 55 | must be done manually using nano. 56 | * Patience since this will take a few hours 57 | * Recent Jessie or Stretch Raspbian Release 58 | * Working RPI 59 | * Working Internet connected to RPI WIFI or RJ45 network cable 60 | * Recommended min 16GB SD card with at least 6 GB Free. 61 | If Free disk space is low or You have a smaller system SD card. 62 | You can mount Non fat USB stick or hard disk see 63 | [***How to Change Location of Temporary Working Folder***](https://github.com/pageauc/opencv3-setup#how-to-change-location-of-temporary-working-folder) 64 | 65 | ## Manual Install 66 | From a logged in RPI SSH session or console terminal perform the following. 67 | This will allow you to review the code before installing. 68 | 69 | cd ~ 70 | wget -o setup.sh https://raw.github.com/pageauc/opencv3-setup/master/setup.sh 71 | more setup.sh 72 | chmod +x setup.sh 73 | ./setup.sh 74 | rm setup.sh 75 | cd ~/opencv3-setup 76 | ./cv3-install-menu.sh 77 | 78 | ## How to Change Configuration 79 | Run the ***SETTINGS*** menu pick or Edit ***cv3-install-menu.conf*** file using nano per the following commands. 80 | 81 | cd ~/opencv3-setup 82 | nano cv3-install-menu.conf 83 | 84 | Press ctrl-x y to save changes and exit nano 85 | 86 | ### How to Change OpenCV Version 87 | Edit variable OPENCV_VER='3.4.2' and change to a valid version per information at 88 | https://github.com/opencv/opencv/releases The version number will be verified at launch 89 | against repo at https://github.com/Itseez/opencv/archive/ 90 | See https://github.com/opencv/opencv/releases 91 | and https://github.com/opencv/opencv_contrib/releases for valid zip versions 92 | 93 | ### How to Change Location of Temporary Working Folder 94 | ***cv3-menu-install.sh*** will create a working folder per 95 | ***INSTALL_DIR*** variable. Default is ***/home/pi/tmp_cv3***. 96 | This folder will store downloaded opencv source and build files. 97 | For a Full Build on a New OS, it is recommended you have a minimum 16GB SD card with at least 6GB free. 98 | Less space may be needed depending on what dependencies are already installed. 99 | To check free disk space run 100 | 101 | df -h 102 | 103 | You can recover most disk space after the build/install by running the DELETE menu pick. 104 | If there is not enough room on the system SD you can point the ***INSTALL_DIR*** to 105 | USB Stick or disk drive media. 106 | ***IMPORTANT:*** The USB memory stick or disk media must ***NOT be formatted as FAT*** 107 | since it does not support symbolic links that are needed to compile opencv. 108 | Use a unix format like ext4 or microsoft NTFS format to avoid a failed make compile. 109 | For details see 110 | [***How To Mount External USB Storage***](https://github.com/pageauc/opencv3-setup#how-to-mount-external-usb-storage) 111 | 112 | ctrl-x y to save changes and exit. Run ***cv3-install.menu.sh***. 113 | The script will create the temporary working folder at the designated location. 114 | 115 | ## RAM Memory 116 | If RPI has 1GB of RAM memory make will use 2 cores -j2, otherwise 1 core -j1 117 | will be used. Both will have Swap Memory temporarily set to 1024 MB during 118 | the make compile process step. At the end of the compile the original Swap 119 | config will be returned. 120 | 121 | ## How to Run Menu Picks 122 | From the main menu select ***1 UPDATE* menu pick. Follow Instructions that 123 | will guide you through the menu steps. If there are errors you can 124 | exit to the terminal to review output messages. Problems will most likely 125 | be related to Disk Space or Memory problems. 126 | 127 | You will be asked to reboot during some installation steps. 128 | If you answer yes on successful completion of a step, you will be 129 | sent to the next step otherwise you will be sent to the terminal 130 | to review errors or back to the main menu as appropriate. 131 | You may see warnings or not found messages and this is normal. 132 | Eg tesseract (OCR) Not Found. 133 | 134 | Users will be prompted to review output for errors and elect to continue. 135 | You can repeat a particular step from the menu if required after 136 | correcting or resolving any issues or errors. 137 | 138 | Once compiling starts it will show you a percent progress. ***Note*** If for some reason 139 | the compile is interrupted, You can restart the compile Menu again and it will quickly 140 | scan progress and will continue compile where it left off (was interrupted). 141 | If a make clean is done (per menu prompt) then a full compile will restart from beginning again. 142 | 143 | ## Logging 144 | A log file called ***cv3_log.txt*** will be created to record system 145 | information and the date/time and details for each operation. 146 | Review the log to check how long various steps took to complete. 147 | On exit you will be prompted if you want to clear the log file. 148 | A backup copy will be saved to ***cv3_log.txt.bak***. A new log will be created 149 | . The log can span multiple sessions. You must intentionally clear the log 150 | from the LOG menu or per commands below to clear. 151 | 152 | cd ~/opencv3-setup 153 | cp cv3-log.txt cv3-log.txt.bak 154 | rm cv3-log.txt 155 | 156 | ## How To Mount External USB Storage 157 | ***IMPORTANT*** If there is limited space on the Raspbian SD card 158 | you may want to change ***INSTALL_DIR*** variable to point to 159 | an Non Fat external storage drive/device. 160 | 161 | cd ~/opencv3-setup 162 | nano cv3-install-menu.sh 163 | 164 | Change the ***INSTALL_DIR*** variable to point to the desired mount location 165 | or a symbolic link to the desired external storage device. ctrl-x y to save 166 | and exit nano. See example mount commands below. 167 | 168 | For more details on mounting external USB storage 169 | see https://www.htpcguides.com/properly-mount-usb-storage-raspberry-pi/ 170 | 171 | Sample commands to mount and use an external ntfs USB hard drive. 172 | Plug ntfs formatted disk into a RPI USB slot. From SSH or terminal session 173 | use sample commands below. (Note) modify to suit your conditions. 174 | If you reboot you will need to redo sudo mount command unless you add 175 | an entry to the /etc/fstab file (not covered here). 176 | 177 | cd ~ 178 | sudo apt-get update 179 | sudo apt-get install ntfs-3g # Make sure ntfs support installed 180 | sudo fdisk -l # will list drive if installed 181 | cd ~/ 182 | mkdir /media/usb_1 183 | sudo mount -t ntfs-3g /dev/sda1 /media/usb_1 184 | df -h #check space on usb device 185 | cd ~/opencv-setup 186 | nano cv3-install-menu.sh 187 | 188 | In nano edit the variable per below. 189 | 190 | INSTALL_DIR='/media/usb_1/tmp_cv3' 191 | 192 | ctrl-x y to save changes and exit. 193 | 194 | ## How to Test Build 195 | To Test build for python or python3. See Example below 196 | Start the required python interpreter by running the appropriate 197 | command for python 2 or 3 below. 198 | 199 | python2 200 | 201 | or 202 | 203 | python3 204 | 205 | At the >>> python prompt enter the following commands 206 | 207 | import cv2 208 | cv2.__version__ 209 | 210 | You should see output indicating the opencv version installed. 211 | Press ctrl-d to exit python interpreter 212 | Also check python3 opencv version using python3 interpreter. 213 | 214 | If after a successful compile and install you see an older version of opencv 215 | you may have to uninstall a previous apt-get version per the following 216 | 217 | sudo apt-get purge python-opencv 218 | 219 | Check the opencv version again to see if the version is updated. Otherwise 220 | you can return the previous opencv per 221 | 222 | sudo apt-get install python-opencv 223 | 224 | See my other github repo at https://github.com/pageauc 225 | for various opencv motion and opencv camera projects. 226 | 227 | ## Credits 228 | Script Steps Based on GitHub Repo 229 | https://github.com/Tes3awy/OpenCV-3.2.0-Compiling-on-Raspberry-Pi 230 | 231 | For Additional Details See https://github.com/pageauc/opencv3-setup 232 | 233 | Have Fun 234 | Claude Pageau 235 | YouTube Channel https://www.youtube.com/user/pageaucp 236 | GitHub Repo https://github.com/pageauc 237 | 238 | -------------------------------------------------------------------------------- /cv3-install-menu.conf: -------------------------------------------------------------------------------- 1 | # Settings for cv3-install-menu.sh 2 | 3 | OPENCV_VER='4.1.2' # This needs to be a valid opencv3 version number 4 | # See https://github.com/opencv/opencv/releases 5 | 6 | INSTALL_DIR='/home/pi/tmp_cv3' # Working folder for Download/Compile of opencv files 7 | # Note Use symbolic link to external drive mount point 8 | # if sd card too small Min 5-6 GB Free Space is Needed 9 | -------------------------------------------------------------------------------- /cv3-install-menu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PROG_VER='ver 3.82' 3 | 4 | # Script to assist with installing OpenCV3 5 | # If problems are encountered exit to command to try to resolve 6 | # Then retry menu pick again or continue to next step 7 | 8 | PROG_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # folder location of this script 9 | PROG_NAME=$(basename -- "$0" .sh) # Extract Program Name minus .sh extension 10 | PROG_CONF="$PROG_DIR/$PROG_NAME.conf" # Setup program conf file name 11 | CUR_OPENCV_VER=$(echo 'import cv2 ; ver = cv2.__version__ ; print(ver)' | python) 12 | 13 | #------------------------------------------------------------------------------ 14 | function read_config_file () 15 | { 16 | if [ -f $PROG_CONF ] ; then 17 | source $PROG_CONF 18 | else 19 | whiptail --title "$PROG_CONF File NOT Found" --msgbox " \ 20 | File $PROG_CONF 21 | Variable Configuration File NOT Found. 22 | Could Not Import $PROG_NAME variables 23 | 24 | A replacement file will be downloaded from GitHub 25 | at https://github.com/pageauc/opencv3-setup 26 | \ 27 | " 0 0 0 28 | echo "Downloading replacement cv3-install-menu.conf file" 29 | wget -O cv3-install-menu.conf https://raw.github.com/pageauc/opencv3-setup/master/cv3-install-menu.conf 30 | echo "Restarting cv3-install-menu.sh" 31 | ./cv3-install-menu.sh 32 | fi 33 | } 34 | 35 | #------------------------------------------------------------------------------ 36 | function check_working_dir () 37 | { 38 | WORKING_DIR=$( dirname $INSTALL_DIR ) 39 | echo "Checking File System type for $WORKING_DIR" 40 | if [ -d $WORKING_DIR ]; then 41 | df -PTh $WORKING_DIR | awk '{print $2}' | grep fat 42 | if [ $? -eq 0 ]; then 43 | whiptail --title "WARNING FAT System Not Supported" --msgbox " \ 44 | $WORKING_DIR is a FAT File System. 45 | IMPORTANT: FAT Does Not Support symlinks 46 | that are Required to Compile opencv2 47 | Run SETTINGS Menu Pick to Edit $PROG_CONF File and 48 | Change WORKING_DIR variable to Point to a Non FAT File System. 49 | NOTE: Leave /tmp_cv3 Entry since Dir will be Created. 50 | \ 51 | " 0 0 0 52 | do_main_menu 53 | fi 54 | else 55 | whiptail --title "$WORKING_DIR Working Dir NOT Found" --msgbox " \ 56 | 57 | $WORKING_DIR Working Directory NOT Found. 58 | 59 | Run SETTINGS Menu Pick 60 | Edit INSTALL_DIR variable to Point to a Valid directory 61 | for downloading the opencv working files. 62 | 63 | NOTE: Leave /tmp_cv3 Entry at end of INSTALL_DIR variable 64 | since it will be Created if it Does Not Exist. 65 | \ 66 | " 0 0 0 67 | do_main_menu 68 | fi 69 | } 70 | 71 | #------------------------------------------------------------------------------ 72 | function do_Initialize () 73 | { 74 | # System Created Variables 75 | LOG_FILE="$PROG_DIR/cv3-log.txt" 76 | BUILD_DIR=$INSTALL_DIR/opencv-$OPENCV_VER/build 77 | # Get Total Memory 78 | TOTAL_MEM=$(free -m | grep Mem | tr -s " " | cut -f 2 -d " ") 79 | # Get Total Swap Memory 80 | TOTAL_SWAP=$(free -m | grep Swap | tr -s " " | cut -f 2 -d " ") 81 | DATE=$(date) 82 | # Set the number of cores for Compiling 83 | # 1G mem gets 2 cores 512 mem gets 1 core, Both get 1024 MB Swap 84 | if [ "$TOTAL_MEM" -gt "512" ] ; then 85 | COMPILE_CORES="-j2" 86 | else 87 | COMPILE_CORES="-j1" 88 | fi 89 | 90 | # Create Log File if it Does Not Exist 91 | if [ ! -f $LOG_FILE ] ; then 92 | echo "$DATE" > $LOG_FILE 93 | echo "$0 $PROG_VER OPENCV_VER=$OPENCV_VER written by Claude Pageau" >> $LOG_FILE 94 | echo "------------------------ Start of Log -----------------------" >> $LOG_FILE 95 | echo "" >> $LOG_FILE 96 | uname -a >> $LOG_FILE 97 | cat /proc/device-tree/model >> $LOG_FILE 98 | echo "" >> $LOG_FILE 99 | echo "$TOTAL_MEM MB Total RAM mem Compile Cores Set to $COMPILE_CORES" >> $LOG_FILE 100 | echo "" >> $LOG_FILE 101 | echo " ----- Start CPU Info -----" >> $LOG_FILE 102 | cat /proc/cpuinfo >> $LOG_FILE 103 | echo "------ End CPU Info -------" >> $LOG_FILE 104 | echo "" >> $LOG_FILE 105 | fi 106 | } 107 | 108 | #------------------------------------------------------------------------------ 109 | function check_min_free_space () 110 | { 111 | # Check Free disk space is above minimum 112 | FREE=`df -k --output=avail "$PWD" | tail -n1` # df -k not df -h 113 | if [[ $FREE -lt 1572864 ]]; then # 1.5 GB 114 | WARNING_MSG=" 115 | $FREE KB free disk space is less than 1.5 GB 116 | Not enough space for a 1 GB swap file plus extra 117 | 118 | Please Investigate Problem and Try Again. 119 | " 120 | do_anykey 121 | else 122 | echo "OK - $FREE KB free disk space Found." >> $LOG_FILE 123 | fi 124 | } 125 | 126 | #------------------------------------------------------------------------------ 127 | function do_check_cv3_ver () 128 | { 129 | opencv_zip="https://github.com/Itseez/opencv/archive/$OPENCV_VER.zip" 130 | echo "Internet Check OpenCV version $OPENCV_VER Wait ..." 131 | echo "" 132 | # Check if there is a url at the destination link 133 | wget -S --spider $opencv_zip 2>&1 | grep -q 'HTTP/1.1 200 OK' 134 | if [ $? -eq 0 ]; then 135 | echo "STATUS" 136 | echo "Current Installed python OpenCV version is $CUR_OPENCV_VER" 137 | echo "Variable OPENCV_VER=$OPENCV_VER Is a Valid OpenCV Version" 138 | echo "" 139 | if [ "$CUR_OPENCV_VER" == "$OPENCV_VER" ] ; then 140 | echo "WARNING" 141 | echo "Looks Like You Have the Latest python OpenCV Version $CUR_OPENCV_VER" 142 | else 143 | echo "UPGRADE" 144 | echo "python OpenCV Version From $CUR_OPENCV_VER" 145 | echo " To $OPENCV_VER" 146 | fi 147 | echo "" 148 | read -p "Press Enter to Continue to Menu" choice 149 | else 150 | whiptail --title "opencv version $OPENCV_VER Check Problem" --msgbox " \ 151 | Could NOT verify opencv $OPENCV_VER version 152 | at $opencv_zip 153 | 154 | 1 Check Internet Connection. 155 | 2 Check variable OPENCV_VER=$OPENCV_VER 156 | Using url https://github.com/opencv/opencv/releases 157 | Verify opencv zip release is valid. 158 | 159 | If Required, Run SETTINGS Menu pick 160 | and Edit variable OPENCV_VER=$OPENCV_VER to a 161 | valid opencv release version 162 | 163 | NOTE: The current installed python OpenCV version is $CUR_OPENCV_VER 164 | \ 165 | " 0 0 0 166 | do_main_menu 167 | fi 168 | } 169 | 170 | #------------------------------------------------------------------------------ 171 | function do_anykey () 172 | { 173 | if (whiptail --title "WARNING" \ 174 | --yes-button "Back" --no-button "Exit" --yesno " \ 175 | $WARNING_MSG 176 | \ 177 | " 0 0) \ 178 | then 179 | do_main_menu 180 | else 181 | exit 182 | fi 183 | } 184 | 185 | #------------------------------------------------------------------------------ 186 | function do_swap_check () 187 | { 188 | if [ "$TOTAL_SWAP" -gt "1000" ] ; then 189 | echo "Total Mem is $TOTAL_MEM MB so $TOTAL_SWAP MB Swap is OK" | tee -a $LOG_FILE 190 | else 191 | if [ ! -f "/etc/dphys-swapfile.bak" ] ; then 192 | check_min_free_space 193 | echo "Temporarily Increase Swap Space to 1024 MB" | tee -a $LOG_FILE 194 | sudo cp /etc/dphys-swapfile /etc/dphys-swapfile.bak 195 | sudo cp $PROG_DIR/dphys-swapfile.1024 /etc/dphys-swapfile 196 | echo "Stop Swap Wait ... " 197 | sudo /etc/init.d/dphys-swapfile stop 198 | echo "Start Swap. First Time Might Take a While so Be Patient ..." 199 | sudo /etc/init.d/dphys-swapfile start 200 | echo "Done ..." 201 | TOTAL_SWAP=$(free -m | grep Swap | tr -s " " | cut -f 2 -d " ") 202 | echo "Total Mem $TOTAL_MEM MB Swap is Now OK at $TOTAL_SWAP MB" | tee -a $LOG_FILE 203 | fi 204 | fi 205 | } 206 | 207 | #------------------------------------------------------------------------------ 208 | function do_swap_back () 209 | { 210 | if [ -f "/etc/dphys-swapfile.bak" ] ; then 211 | echo "Found File /etc/dphys-swapfile.bak" | tee -a $LOG_FILE 212 | echo "Returning Swap Settings Back Previous" | tee -a $LOG_FILE 213 | sudo cp /etc/dphys-swapfile.bak /etc/dphys-swapfile 214 | sudo rm /etc/dphys-swapfile.bak 215 | echo "Stop Swap Wait ..." 216 | sudo /etc/init.d/dphys-swapfile stop 217 | echo "Start Swap Wait ..." 218 | sudo /etc/init.d/dphys-swapfile start 219 | echo "Done ..." 220 | TOTAL_SWAP=$(free -m | grep Swap | tr -s " " | cut -f 2 -d " ") 221 | echo "Total Mem=$TOTAL_MEM MB Total Swap=$TOTAL_SWAP MB" | tee -a $LOG_FILE 222 | fi 223 | } 224 | 225 | #------------------------------------------------------------------------------ 226 | function do_rpi_update () 227 | { 228 | clear 229 | # Update Raspbian to Lastest Releases 230 | DATE=$(date) 231 | echo "$DATE STEP 1 - Update/Upgrade Raspbian Please Wait ..." | tee -a $LOG_FILE 232 | echo "$DATE sudo apt-get update Please Wait ..." | tee -a $LOG_FILE 233 | START=$(date +%s) 234 | echo "-- Update Start: $DATE" | tee -a $LOG_FILE 235 | sudo apt-get -y update 236 | DATE=$(date) 237 | END=$(date +%s) 238 | DIFF=$((END - START)) 239 | echo "-- Update End: $DATE" | tee -a $LOG_FILE 240 | echo "-- Update Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 241 | echo "" 242 | DATE=$(date) 243 | echo "$DATE sudo apt-get upgrade Please Wait ..." 244 | START=$(date +%s) 245 | echo "-- Upgrade Start: $DATE" | tee -a $LOG_FILE 246 | sudo apt-get -y upgrade 247 | DATE=$(date) 248 | END=$(date +%s) 249 | DIFF=$((END - START)) 250 | echo "-- Upgrade End: $DATE" | tee -a $LOG_FILE 251 | echo "-- Upgrade Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 252 | sudo apt-get -y autoremove 253 | echo "" 254 | echo "------------------ STEP 1 INSTRUCTIONS --------------------" 255 | echo "If there are Significant Changes then a Reboot Is Required." 256 | echo "After Reboot Run this menu script again and Select" 257 | echo "Menu Pick: 2 DEP Install Build Dependencies and Download Source" 258 | echo "-----------------------------------------------------------" 259 | read -p "Reboot Now? (y/n)? " choice 260 | case "$choice" in 261 | y|Y ) echo "yes" 262 | echo "Rebooting Now" 263 | sudo reboot 264 | ;; 265 | * ) do_cv3_dep 266 | ;; 267 | esac 268 | } 269 | 270 | #------------------------------------------------------------------------------ 271 | function do_cv3_dep () 272 | { 273 | clear 274 | if [ -d "$INSTALL_DIR/opencv-$OPENCV_VER" ] ; then 275 | if (whiptail --title "Folder Already Exists" \ 276 | --yes-button "Back" --no-button "Repeat" --yesno " \ 277 | WARNING 278 | $INSTALL_DIR/opencv-$OPENCV_VER 279 | Folder Already Exists. 280 | 281 | If a Previous Install was Successful, 282 | Do You Really Want to Repeat Install of 283 | opencv Dependencies and Source Files Again? 284 | \ 285 | " 0 0) \ 286 | then 287 | do_main_menu 288 | else 289 | do_cv3_dep_install 290 | fi 291 | else 292 | do_cv3_dep_install 293 | fi 294 | } 295 | 296 | #------------------------------------------------------------------------------ 297 | function do_cv3_dep_install () 298 | { 299 | check_min_free_space 300 | # Install opencv3 build dependencies 301 | echo "STEP 2 Install opencv $OPENCV_VER Build Dependencies" 302 | echo "" 303 | echo "This step will install opencv $OPENCV_VER Build Dependencies" 304 | echo "Then Download and unzip opencv source files to $INSTALL_DIR Folder" 305 | echo "" 306 | df -h 307 | echo "" 308 | echo "---------------- STEP 2 INSTRUCTIONS ---------------" 309 | echo "A Fresh Build Needs at Least 16GB SD with 5-6 GB Free. Free Space" 310 | echo "could be less depending on what dependencies are already installed" 311 | echo "If you are using a smaller system SD or are Low on Free Disk Space." 312 | echo "You can mount USB media and change variable INSTALL_DIR in this script." 313 | echo "Installs Will Take a While so be Patient ..." 314 | echo "----------------------------------------------------" 315 | read -p "Install Dep and Source? (y/n)? " choice 316 | case "$choice" in 317 | n|N ) do_main_menu 318 | ;; 319 | * ) echo "" 320 | ;; 321 | esac 322 | DATE=$(date) 323 | echo "$DATE STEP 2-1 Installing Dependencies Please Wait ..." | tee -a $LOG_FILE 324 | echo "" 325 | START=$(date +%s) 326 | echo "-- apt-get-install Start: $DATE" | tee -a $LOG_FILE 327 | sudo apt-get install -y build-essential 328 | sudo apt-get install -y git 329 | sudo apt-get install -y cmake 330 | sudo apt-get install -y pkg-config 331 | sudo apt-get install -y libjpeg-dev 332 | sudo apt-get install -y libtiff-dev 333 | sudo apt-get install -y libjasper-dev 334 | sudo apt-get install -y libpng-dev 335 | sudo apt-get install -y libavcodec-dev 336 | sudo apt-get install -y libavformat-dev 337 | sudo apt-get install -y libswscale-dev 338 | sudo apt-get install -y libgtk2.0-dev 339 | sudo apt-get install -y libgstreamer0.10-0-dbg 340 | sudo apt-get install -y libgstreamer0.10-0 341 | sudo apt-get install -y libgstreamer0.10-dev 342 | sudo apt-get install -y libv4l-0 343 | sudo apt-get install -y libv4l-dev 344 | sudo apt-get install -y libxvidcore-dev 345 | sudo apt-get install -y libgtk-3-dev 346 | sudo apt-get install -y libx264-dev 347 | sudo apt-get install -y libqtgui4 348 | sudo apt-get install -y libqt4-test 349 | sudo apt-get install -y libcanberra-gtk* 350 | sudo apt-get install -y libatlas-base-dev 351 | sudo apt-get install -y python2.7-dev 352 | sudo apt-get install -y python3-dev 353 | sudo apt-get install -y gfortran 354 | sudo apt-get install -y python-numpy 355 | sudo apt-get install -y python-scipy 356 | sudo apt-get install -y python-matplotlib 357 | sudo apt-get install -y default-jdk ant 358 | sudo apt-get install -y libgtkglext1-dev 359 | sudo apt-get install -y v4l-utils 360 | sudo apt-get install -y gphoto2 361 | sudo apt-get install -y python-pip 362 | sudo apt-get install -y python3-pip 363 | sudo apt-get install -y ntfs-3g 364 | DATE=$(date) 365 | END=$(date +%s) 366 | DIFF=$((END - START)) 367 | echo "-- apt-get-install End: $DATE" | tee -a $LOG_FILE 368 | echo "-- apt-get-install Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 369 | echo "Perform sudo apt-get autoremove and clean" 370 | echo "" 371 | sudo apt-get -y autoremove 372 | sudo apt-get clean 373 | if [ ! -d $INSTALL_DIR ] ; then 374 | echo "Create dir $INSTALL_DIR" 375 | mkdir -p $INSTALL_DIR 376 | if [ $? -ne 0 ] ; then 377 | WARNING_MSG=" 378 | Could Not Create Dir at $INSTALL_DIR 379 | Check permissions 380 | If on a mounted Device make sure 381 | 1- Device is mounted and is NOT FAT32 382 | 2- Device Must be writeable by Pi user. 383 | Check ownership and permissions 384 | 385 | Exit to Terminal to Investigate 386 | " 387 | do_anykey 388 | fi 389 | fi 390 | cd $INSTALL_DIR 391 | echo "" 392 | echo "Install pip" 393 | sudo pip install numpy 394 | echo "" 395 | echo "$DATE Done Install of Build Essentials and Dependencies ..." | tee -a $LOG_FILE 396 | echo "" 397 | DATE=$(date) 398 | echo "$DATE STEP 2-2 Download and unzip opencv $OPENCV_VER Source Files" | tee -a $LOG_FILE 399 | echo "" 400 | START=$(date +%s) 401 | echo "-- opencv.zip Start: $DATE" | tee -a $LOG_FILE 402 | wget -O opencv.zip https://github.com/Itseez/opencv/archive/$OPENCV_VER.zip 403 | unzip -o opencv.zip 404 | rm opencv.zip 405 | DATE=$(date) 406 | END=$(date +%s) 407 | DIFF=$((END - START)) 408 | echo "-- opencv.zip End: $DATE" | tee -a $LOG_FILE 409 | echo "-- opencv.zip Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 410 | echo "" 411 | DATE=$(date) 412 | echo "$DATE STEP 2-3 Download and unzip opencv $OPENCV_VER Contrib Files" | tee -a $LOG_FILE 413 | echo "" 414 | START=$(date +%s) 415 | echo "-- opencv.zip Start: $DATE" | tee -a $LOG_FILE 416 | wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/$OPENCV_VER.zip 417 | unzip -o opencv_contrib.zip 418 | rm opencv_contrib.zip 419 | DATE=$(date) 420 | END=$(date +%s) 421 | DIFF=$((END - START)) 422 | echo "-- opencv_contrib.zip End: $DATE" | tee -a $LOG_FILE 423 | echo "-- opencv_contrib.zip Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 424 | echo "" 425 | echo "Done Requirements for OpenCV $OPENCV_VER Source Build." 426 | echo "" 427 | df -h 428 | echo "" 429 | read -p "Proceed to STEP 3 COMPILE (y/n)? " choice 430 | case "$choice" in 431 | y|Y ) do_cv3_cmake 432 | ;; 433 | * ) do_main_menu 434 | ;; 435 | esac 436 | } 437 | 438 | #------------------------------------------------------------------------------ 439 | function do_cv3_compile_menu () 440 | { 441 | cd $INSTALL_DIR 442 | clear 443 | if [ ! -d "$BUILD_DIR" ] ; then 444 | echo "Create build directory $BUILD_DIR" 445 | mkdir -p $BUILD_DIR 446 | fi 447 | cd $BUILD_DIR 448 | SELECTION=$(whiptail --title "COMPILE OpenCV Menu from $CUR_OPENCV_VER to $OPENCV_VER" --menu "Arrow/Enter Selects or Tab Key" 0 0 0 --cancel-button Quit --ok-button Select \ 449 | "1 CMAKE" "Required unless a previous cmake was successful" \ 450 | "2 MAKE" "Run/Continue make compile if a previous cmake was successful" \ 451 | "3 CLEAN" "Run a make clean to Force compile from Start (Lose Previous Progress)" \ 452 | "q BACK" "Back to Main Menu" 3>&1 1>&2 2>&3) 453 | 454 | RET=$? 455 | if [ $RET -eq 1 ]; then 456 | exit 0 457 | elif [ $RET -eq 0 ]; then 458 | case "$SELECTION" in 459 | 1\ *) do_cv3_cmake 460 | ;; 461 | 2\ *) do_cv3_make 462 | ;; 463 | 3\ *) do_make_clean 464 | ;; 465 | q\ *) cd $PROG_DIR 466 | do_main_menu 467 | ;; 468 | *) whiptail --msgbox "Programmer error: unrecognised option" 20 60 1 ;; 469 | esac || whiptail --msgbox "There was an error running selection $SELECTION" 20 60 1 470 | fi 471 | } 472 | 473 | #------------------------------------------------------------------------------ 474 | function do_cv3_cmake () 475 | { 476 | if [ ! -d "$BUILD_DIR" ] ; then 477 | echo "Create build directory $BUILD_DIR" 478 | mkdir -p $BUILD_DIR 479 | fi 480 | cd $BUILD_DIR 481 | if [ ! -d $INSTALL_DIR ] ; then 482 | clear 483 | if (whiptail --title "$INSTALL_DIR Director Not Found" \ 484 | --yes-button "Back" --no-button "Run DEP" --yesno "\ 485 | 486 | $INSTALL_DIR Director Not Found. 487 | 488 | You Need to Run STEP 2 DEP Menu Pick 489 | in Order to Install Dependencies 490 | and Download Opencv3 Source Files. 491 | 492 | \ 493 | " 0 0) \ 494 | then 495 | do_main_menu 496 | else 497 | do_cv3_dep 498 | fi 499 | fi 500 | 501 | if (whiptail --title "cmake INSTRUCTIONS" \ 502 | --yes-button "Back" --no-button "Run cmake" --yesno "\ 503 | 504 | cmake will scan opencv dependencies, source directory tree 505 | and customize for the Raspberry Pi environment. This must be done 506 | before a make compile can be performed. 507 | 508 | You will see Failed, not found, no package, Etc messages during cmake. 509 | This is normal and not fatal. Fatal errors will normally 510 | stop cmake before completion. 511 | 512 | If cmake is OK Last Messages should say 513 | -- Configuring done 514 | -- Generating done 515 | -- Build files have been written to: ... 516 | 517 | Note: These will take a while to complete so be patient ... 518 | 519 | \ 520 | " 0 0) \ 521 | then 522 | do_cv3_compile_menu 523 | else 524 | clear 525 | DATE=$(date) 526 | echo "$DATE STEP 3-1 Run cmake Prior to Compiling opencv $OPENCV_VER with make -j1" | tee -a $LOG_FILE 527 | fi 528 | START=$(date +%s) 529 | cat /proc/device-tree/model 530 | echo "" 531 | echo "-- cmake Start: $DATE" | tee -a $LOG_FILE 532 | cat /proc/device-tree/model | grep -aq "Raspberry Pi 4" 533 | if [ $? -eq 0 ]; then 534 | echo "-- cmake Compile for Raspberry Pi 4 ENABLE NEON=ON" | tee -a $LOG_FILE 535 | cmake -D CMAKE_BUILD_TYPE=RELEASE \ 536 | -D CMAKE_INSTALL_PREFIX=/usr/local \ 537 | -D OPENCV_EXTRA_MODULES_PATH=$INSTALL_DIR/opencv_contrib-$OPENCV_VER/modules \ 538 | -D ENABLE_NEON=ON \ 539 | -D ENABLE_VFPV3=ON \ 540 | -D BUILD_TESTS=OFF \ 541 | -D OPENCV_ENABLE_NONFREE=ON \ 542 | -D INSTALL_PYTHON_EXAMPLES=OFF \ 543 | -D BUILD_EXAMPLES=OFF .. 544 | else 545 | cat /proc/device-tree/model | grep -aq "Raspberry Pi 3" 546 | if [ $? -eq 0 ]; then 547 | # This optimizes for Raspberry Pi 3 Models 548 | echo "-- cmake Compile for Raspberry Pi 3 ENABLE NEON=ON" | tee -a $LOG_FILE 549 | cmake -D CMAKE_BUILD_TYPE=RELEASE \ 550 | -D CMAKE_INSTALL_PREFIX=/usr/local \ 551 | -D INSTALL_C_EXAMPLES=OFF \ 552 | -D INSTALL_PYTHON_EXAMPLES=OFF \ 553 | -D OPENCV_EXTRA_MODULES_PATH=$INSTALL_DIR/opencv_contrib-$OPENCV_VER/modules \ 554 | -D BUILD_EXAMPLES=OFF \ 555 | -D ENABLE_NEON=ON .. 556 | else 557 | echo "-- cmake Compile for Raspberry Pi 2 ENABLE NEON=OFF" | tee -a $LOG_FILE 558 | cmake -D CMAKE_BUILD_TYPE=RELEASE \ 559 | -D CMAKE_INSTALL_PREFIX=/usr/local \ 560 | -D INSTALL_C_EXAMPLES=OFF \ 561 | -D INSTALL_PYTHON_EXAMPLES=OFF \ 562 | -D OPENCV_EXTRA_MODULES_PATH=$INSTALL_DIR/opencv_contrib-$OPENCV_VER/modules \ 563 | -D BUILD_EXAMPLES=OFF .. 564 | fi 565 | fi 566 | echo "----------------------- End of cmake Messages -------------------------" 567 | echo "" 568 | DATE=$(date) 569 | END=$(date +%s) 570 | DIFF=$((END - START)) 571 | echo "-- cmake End: $DATE" | tee -a $LOG_FILE 572 | echo "-- cmake Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 573 | echo "---------- cmake INSTRUCTIONS ------------" 574 | echo " NOTE: Most Not Found or Failed Messages are Normal" 575 | echo " 1- Review cmake messages above for Errors" 576 | echo " 2- Check that Python 2 and Python 3 sections" 577 | echo " Have directory path entries" 578 | echo " 3- Check if errors reported." 579 | echo " 4- Last Messages should say" 580 | echo "-- Configuring done" 581 | echo "-- Generating done" 582 | echo "-- Build files have been written to: ..." 583 | echo "---------------------------------------------" 584 | read -p "Was cmake Successful (y/n)? " choice 585 | case "$choice" in 586 | n|N ) echo "Exit to Terminal to Resolve Problems" 587 | exit 1 588 | ;; 589 | * ) do_cv3_make 590 | ;; 591 | esac 592 | } 593 | 594 | #------------------------------------------------------------------------------ 595 | function do_make_clean () 596 | { 597 | if (whiptail --title "make clean INSTRUCTIONS" \ 598 | --yes-button "Back" --no-button "Clean" --yesno " \ 599 | 600 | make clean will clear the current compile status 601 | and force a full compile from the beginning. 602 | This may be necessary if there were issues 603 | with a previous make compile. 604 | 605 | \ 606 | " 0 0 0 ) \ 607 | then 608 | do_cv3_compile_menu 609 | else 610 | echo "Running make clean" 611 | echo "------------------" 612 | sudo make clean 613 | echo "Completed make clean" 614 | echo "Next compile will start from beginning" 615 | read -p "Press Return to Continue" choice 616 | do_cv3_compile_menu 617 | fi 618 | } 619 | 620 | #------------------------------------------------------------------------------ 621 | function do_cv3_make () 622 | { 623 | if (whiptail --title "make INSTRUCTIONS" \ 624 | --yes-button "Back" --no-button "Run make" --yesno " \ 625 | 626 | A full make compile of openCV $OPENCV_VER will take many hours ... 627 | A single core RPI with 512 MB RAM can take approx 27 hours 628 | A quad core RPI with 1 GB of RAM can take 3 - 6 hours 629 | Note: Starting dphys-swapfile service may take a while .... 630 | 631 | RAM=$TOTAL_MEM MB Run make $COMPILE_CORES 632 | 633 | Once Compile is started, Percentage complete will be displayed. 634 | Go for a nice Long Walk, Binge watch Game of Thrones or Something Else ..... 635 | 636 | \ 637 | " 0 0 0 ) \ 638 | then 639 | do_cv3_compile_menu 640 | else 641 | do_swap_check 642 | TOTAL_SWAP=$(free -m | grep Swap | tr -s " " | cut -f 2 -d " ") 643 | DATE=$(date) 644 | echo "$DATE STEP 3-2 Start Compile of opencv $OPENCV_VER" | tee -a $LOG_FILE 645 | fi 646 | START=$(date +%s) 647 | echo "-- make $COMPILE_CORES RAM=$TOTAL_MEM SWAP=$TOTAL_SWAP" | tee -a $LOG_FILE 648 | echo "-- make Start: $DATE" | tee -a $LOG_FILE 649 | make $COMPILE_CORES 650 | echo "--------------- End of make $COMPILE_CORES Messages ----------------" 651 | DATE=$(date) 652 | END=$(date +%s) 653 | DIFF=$((END - START)) 654 | do_swap_back 655 | TOTAL_SWAP=$(free -m | grep Swap | tr -s " " | cut -f 2 -d " ") 656 | echo "-- make $COMPILE_CORES RAM=$TOTAL_MEM SWAP=$TOTAL_SWAP" | tee -a $LOG_FILE 657 | echo "-- make End: $DATE" | tee -a $LOG_FILE 658 | echo "-- make Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 659 | echo "----------- STEP 3-2 INSTRUCTIONS --------------" 660 | echo "1- 1f Compile make $COMPILE_CORES is 100 percent" 661 | echo " Proceed to STEP 4 make install" 662 | echo "2- If Less than 100 percent Complete" 663 | echo " Record Errors and Investigate Problem." 664 | echo "------------------------------------------------" 665 | read -p "Was make Compile Successful? (y/n)? " choice 666 | case "$choice" in 667 | y|Y ) do_cv3_install 668 | ;; 669 | * ) WARNING_MSG=" 670 | When Errors Resolved, Retry Step 3 COMPILE Menu Pick 671 | Note make will Continue where it Left Off 672 | Exit to Terminal to Review Errors 673 | " 674 | do_anykey 675 | ;; 676 | esac 677 | } 678 | 679 | #------------------------------------------------------------------------------ 680 | function do_cv3_install () 681 | { 682 | if [ -d "$INSTALL_DIR/opencv-$OPENCV_VER/build" ] ; then 683 | clear 684 | if (whiptail --title "make install INSTRUCTIONS" \ 685 | --yes-button "Back" --no-button "Run make install" --yesno " \ 686 | 687 | This Step will copy the compiled code to the Required System Folders 688 | 689 | WARNING - Do NOT run this unless you have successfully completed 690 | STEP 3 COMPILE 691 | \ 692 | " 0 0 0 ) \ 693 | then 694 | do_main_menu 695 | fi 696 | cd $INSTALL_DIR/opencv-$OPENCV_VER/build 697 | DATE=$(date) 698 | echo "$DATE STEP 4 Start make install of opencv $OPENCV_VER" | tee -a $LOG_FILE 699 | START=$(date +%s) 700 | echo "-- make install Start: $DATE" | tee -a $LOG_FILE 701 | sudo make install 702 | sudo ldconfig 703 | DATE=$(date) 704 | END=$(date +%s) 705 | DIFF=$((END - START)) 706 | echo "-- make install End: $DATE" | tee -a $LOG_FILE 707 | echo "-- make install Took: $(($DIFF / 60)) min $(($DIFF % 60)) sec" | tee -a $LOG_FILE 708 | if (whiptail --title "Reboot Required" \ 709 | --yes-button "Back" --no-button "Reboot" --yesno " \ 710 | 711 | You Need to Reboot to Complete Install 712 | of python OpenCV $open_ver 713 | 714 | \ 715 | " 0 0 0 ) \ 716 | then 717 | do_main_menu 718 | else 719 | sudo reboot 720 | fi 721 | else 722 | whiptail --title "WARNING Dir Not Found $INSTALL_DIR/opencv-$OPENCV_VER/build" --msgbox " \ 723 | 724 | WARNING: Directory Not Found $INSTALL_DIR/opencv-$OPENCV_VER/build 725 | You Need to Run Step 2 or 3 to Install Source Files or Compile. 726 | 727 | \ 728 | " 0 0 0 729 | do_main_menu 730 | fi 731 | } 732 | 733 | #------------------------------------------------------------------------------ 734 | function do_cv3_cleanup () 735 | { 736 | clear 737 | if [ ! -d $INSTALL_DIR ] ; then 738 | whiptail --title "WARNING" --msgbox " \ 739 | $INSTALL_DIR Directory NOT Found 740 | You May Need to Run MENU PICK 2 DEP 741 | If You Have Not Previously Done This. 742 | \ 743 | " 0 0 0 744 | do_main_menu 745 | fi 746 | echo "Step 5 - Delete OpenCV $OPENCV_VER Source Folders and zip files (optional)" 747 | echo "" 748 | echo "Current System Disk Status" 749 | df -h 750 | echo "------------- STEP 5 INSTRUCTIONS ---------------" 751 | echo "Temporary Source Files are in $INSTALL_DIR Folder" 752 | echo "You can DELETE this Folder and recover disk space" 753 | du -sh $INSTALL_DIR 754 | echo "-------------------------------------------------" 755 | read -p "DELETE $INSTALL_DIR Now? (y/n) " choice 756 | case "$choice" in 757 | y|Y ) read -p "Are you Sure? (y/n) " choice 758 | case "$choice" in 759 | y|Y ) echo "" 760 | echo "BEFORE - Disk Space Status" 761 | df -h 762 | echo "Deleting $INSTALL_DIR" 763 | ;; 764 | * ) do_main_menu 765 | ;; 766 | esac 767 | cd $INSTALL_DIR 768 | cd .. 769 | sudo rm -R $INSTALL_DIR 770 | echo "" 771 | echo "AFTER - Disk Space Status" 772 | df -h 773 | echo "Done Removing $INSTALL_DIR Source Folders and zip files .." 774 | read -p "Press Enter Key To Continue" choice 775 | ;; 776 | n|N ) do_main_menu 777 | ;; 778 | * ) echo "invalid Selection" 779 | ;; 780 | esac 781 | echo "" 782 | echo "Current System Disk Status" 783 | df -h 784 | read -p "Press Enter Key to Continue" choice 785 | } 786 | 787 | #------------------------------------------------------------------------------ 788 | function do_upgrade() 789 | { 790 | if (whiptail --title "GitHub Upgrade speed-cam" \ 791 | --yesno "Upgrade opencv3_setup Files from GitHub.\n" 0 0 0 \ 792 | --yes-button "upgrade" \ 793 | --no-button "Cancel" ); then 794 | curlcmd=('/usr/bin/curl -L curl -L https://raw.github.com/pageauc/opencv3-setup/master/setup.sh | bash') 795 | eval $curlcmd 796 | echo "Done $PROG_NAME Upgrade/Refresh" 797 | echo "Restart Menu to Implement Changes" 798 | echo "" 799 | echo " ./cv3-install-menu.sh" 800 | echo "Restarting cv3-install-menu.sh" 801 | read -p "Press Enter to Continue" choice 802 | ./cv3-install-menu.sh 803 | fi 804 | } 805 | 806 | #------------------------------------------------------------------------------ 807 | function do_log () 808 | { 809 | if [ -f "$LOG_FILE" ] ; then 810 | stty igncr # Suppress cr 811 | cat $LOG_FILE | more -d 812 | echo "" 813 | echo "------------------ End of Log -----------------" 814 | echo "" 815 | stty -igncr 816 | echo "Note: Copy of Log will be Saved as $LOG_FILE.bak" 817 | echo "------------------------------------------------" 818 | read -p "Delete Log File (y/n)? " choice 819 | case "$choice" in 820 | y|Y ) cp $LOG_FILE $LOG_FILE.bak 821 | rm $LOG_FILE 822 | echo "Deleted $LOG_FILE" 823 | echo "Saved Copy to $LOG_FILE.bak" 824 | echo "------------------------------------------------" 825 | read -p "Press Enter To Return to Main Menu" choice 826 | do_Initialize 827 | do_main_menu 828 | ;; 829 | * ) do_main_menu 830 | ;; 831 | esac 832 | else 833 | echo "" 834 | echo "Log File Not Found $LOG_FILE" 835 | echo "" 836 | read -p "Press Enter To Return to Main Menu" choice 837 | fi 838 | } 839 | 840 | #------------------------------------------------------------------------------ 841 | function do_auto () 842 | { 843 | clear 844 | if (whiptail --title "AUTO Install INSTRUCTIONS" \ 845 | --yes-button "Back" --no-button "Run Auto" --yesno " \ 846 | This Auto Install will perform a complete opencv 847 | unattended build including 848 | 849 | 1 Raspbian update/upgrade with No Reboot 850 | 2 Install Dependencies and opencv source files 851 | 3 Perform opencv cmake 852 | 4 Perform opencv make compile 853 | 5 Perform python opencv make install (if make successful) 854 | 855 | NOTE: You will Need to Run DELETE menu pick Manually. 856 | Also, This Unattended Build will Not Log Activity. 857 | During cmake and make you will see Failed, not found, 858 | no package Etc messages. This is normal and Not Fatal. 859 | Fatal errors will stop compile. 860 | 861 | Be Patient ... 862 | \ 863 | " 0 0) \ 864 | then 865 | do_main_menu 866 | else 867 | check_min_free_space 868 | fi 869 | read_config_file 870 | sudo apt-get -y update 871 | sudo apt-get -y upgrade 872 | sudo apt-get install -y build-essential 873 | sudo apt-get install -y git 874 | sudo apt-get install -y cmake 875 | sudo apt-get install -y pkg-config 876 | sudo apt-get install -y libjpeg-dev 877 | sudo apt-get install -y libtiff-dev 878 | sudo apt-get install -y libjasper-dev 879 | sudo apt-get install -y libpng-dev 880 | sudo apt-get install -y libavcodec-dev 881 | sudo apt-get install -y libavformat-dev 882 | sudo apt-get install -y libswscale-dev 883 | sudo apt-get install -y libgtk2.0-dev 884 | sudo apt-get install -y libgstreamer0.10-0-dbg 885 | sudo apt-get install -y libgstreamer0.10-0 886 | sudo apt-get install -y libgstreamer0.10-dev 887 | sudo apt-get install -y libv4l-0 888 | sudo apt-get install -y libv4l-dev 889 | sudo apt-get install -y libxvidcore-dev 890 | sudo apt-get install -y libgtk-3-dev 891 | sudo apt-get install -y libx264-dev 892 | sudo apt-get install -y libqtgui4 893 | sudo apt-get install -y libqt4-test 894 | sudo apt-get install -y libcanberra-gtk* 895 | sudo apt-get install -y libatlas-base-dev 896 | sudo apt-get install -y python2.7-dev 897 | sudo apt-get install -y python3-dev 898 | sudo apt-get install -y gfortran 899 | sudo apt-get install -y python-numpy 900 | sudo apt-get install -y python-scipy 901 | sudo apt-get install -y python-matplotlib 902 | sudo apt-get install -y default-jdk ant 903 | sudo apt-get install -y libgtkglext1-dev 904 | sudo apt-get install -y v4l-utils 905 | sudo apt-get install -y gphoto2 906 | sudo apt-get install -y python-pip 907 | sudo apt-get install -y python3-pip 908 | sudo apt-get install -y ntfs-3g 909 | sudo pip install numpy 910 | sudo pip3 install numpy 911 | sudo apt-get -y autoremove 912 | sudo apt-get clean 913 | mkdir -p $INSTALL_DIR 914 | cd $INSTALL_DIR 915 | wget -O opencv.zip https://github.com/Itseez/opencv/archive/$OPENCV_VER.zip 916 | unzip -o opencv.zip 917 | rm opencv.zip 918 | wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/$OPENCV_VER.zip 919 | unzip -o opencv_contrib.zip 920 | rm opencv_contrib.zip 921 | if [ ! -d "$BUILD_DIR" ] ; then 922 | mkdir -p $BUILD_DIR 923 | fi 924 | cd $BUILD_DIR 925 | cat /proc/device-tree/model | grep -aq "Raspberry Pi 4" 926 | if [ $? -eq 0 ]; then 927 | echo "-- cmake Compile for Raspberry Pi 4 ENABLE NEON=ON" | tee -a $LOG_FILE 928 | cmake -D CMAKE_BUILD_TYPE=RELEASE \ 929 | -D CMAKE_INSTALL_PREFIX=/usr/local \ 930 | -D OPENCV_EXTRA_MODULES_PATH=$INSTALL_DIR/opencv_contrib-$OPENCV_VER/modules \ 931 | -D ENABLE_NEON=ON \ 932 | -D ENABLE_VFPV3=ON \ 933 | -D BUILD_TESTS=OFF \ 934 | -D OPENCV_ENABLE_NONFREE=ON \ 935 | -D INSTALL_PYTHON_EXAMPLES=OFF \ 936 | -D BUILD_EXAMPLES=OFF .. 937 | else 938 | cat /proc/device-tree/model | grep -aq "Raspberry Pi 3" 939 | if [ $? -eq 0 ]; then 940 | # This optimizes for Raspberry Pi 3 Models 941 | echo "-- cmake Compile for Raspberry Pi 3 ENABLE NEON=ON" | tee -a $LOG_FILE 942 | cmake -D CMAKE_BUILD_TYPE=RELEASE \ 943 | -D CMAKE_INSTALL_PREFIX=/usr/local \ 944 | -D INSTALL_C_EXAMPLES=OFF \ 945 | -D INSTALL_PYTHON_EXAMPLES=OFF \ 946 | -D OPENCV_EXTRA_MODULES_PATH=$INSTALL_DIR/opencv_contrib-$OPENCV_VER/modules \ 947 | -D BUILD_EXAMPLES=OFF \ 948 | -D ENABLE_NEON=ON .. 949 | else 950 | echo "-- cmake Compile for Raspberry Pi 2 ENABLE NEON=OFF" | tee -a $LOG_FILE 951 | cmake -D CMAKE_BUILD_TYPE=RELEASE \ 952 | -D CMAKE_INSTALL_PREFIX=/usr/local \ 953 | -D INSTALL_C_EXAMPLES=OFF \ 954 | -D INSTALL_PYTHON_EXAMPLES=OFF \ 955 | -D OPENCV_EXTRA_MODULES_PATH=$INSTALL_DIR/opencv_contrib-$OPENCV_VER/modules \ 956 | -D BUILD_EXAMPLES=OFF .. 957 | fi 958 | fi 959 | make $COMPILE_CORES 960 | if [ -f "/etc/dphys-swapfile.bak" ] ; then 961 | sudo cp /etc/dphys-swapfile.bak /etc/dphys-swapfile 962 | sudo rm /etc/dphys-swapfile.bak 963 | sudo /etc/init.d/dphys-swapfile stop 964 | sudo /etc/init.d/dphys-swapfile start 965 | TOTAL_SWAP=$(free -m | grep Swap | tr -s " " | cut -f 2 -d " ") 966 | fi 967 | cd $PROG_DIR 968 | echo "Compile Complete. Check Above for Errors" 969 | echo "If Compile Failed Select n to Exit to Terminal" 970 | echo "To Review Error Messages." 971 | echo "" 972 | read -p "Was Compile Successful y/n? " choice 973 | case "$choice" in 974 | y|Y ) sudo make install 975 | sudo ldconfig 976 | echo "Auto Install is Complete" 977 | echo "Unless Errors were Reported" 978 | echo 'import cv2 ; ver = cv2.__version__ ; print("python2 current opencv version is %s" % ver) ' | python2 979 | echo 'import cv2 ; ver = cv2.__version__ ; print("python3 current opencv version is %s" % ver) ' | python3 980 | echo "Reboot to update opencv" 981 | echo "Test python opencv version per commands using" 982 | echo "the appropriate python interpreter" 983 | echo "" 984 | echo "python2 or python3" 985 | echo "at the python interpreter prompt enter the following commands" 986 | echo "" 987 | echo ">>> import cv2" 988 | echo ">>> cv2.__version__" 989 | echo "" 990 | echo "Verify version then Press ctrl-d to exit" 991 | echo "See Readme.md How to Test Build section for more details" 992 | exit 993 | ;; 994 | * ) echo "Exit to Terminal Due to Errors" 995 | exit 1 996 | ;; 997 | esac 998 | } 999 | 1000 | #------------------------------------------------------------------------------ 1001 | function do_about() 1002 | { 1003 | whiptail --title "About" --msgbox " \ 1004 | $0 $PROG_VER written by Claude Pageau 1005 | See GitHub https://github.com/pageauc/opencv3-setup 1006 | 1007 | This is a menu driven install script to download and 1008 | compile opencv3 from source code. Default is opencv 3.3.0 1009 | To change OPENCV_VER variable nano edit the 1010 | $PROG_CONF file. 1011 | The OPENCV_VER variable will be verified at 1012 | https://github.com/Itseez/opencv/archive/ 1013 | when this menu script is run. 1014 | 1015 | Prerequisites 1016 | 1 - RPI 2 or 3 Connected to Working Internet Connection 1017 | 2 - Recent Jessie or Stretch Raspbian Release. 1018 | Earlier versions like wheezy not tested but may work. 1019 | Recommended min 16GB SD card with at least 6 GB Free. 1020 | If Free disk space is Low or You have a Small system SD. 1021 | You can mount a NON FAT format eg ext4 or NTFS 1022 | USB memory stick or hard disk and change the 1023 | INSTALL_DIR variable in $PROG_CONF file 1024 | to point to the new path. 1025 | 1026 | Instructions 1027 | You will be asked to reboot during some installation steps. 1028 | If you answer yes to successful completion of a step, you will be 1029 | sent to the next step otherwise you will be sent to the terminal 1030 | to review errors or back to the main menu as appropriate. 1031 | For Additional Details See https://github.com/pageauc/opencv3-setup 1032 | Script Steps Based on GitHub Repo 1033 | https://github.com/Tes3awy/OpenCV-3.2.0-Compiling-on-Raspberry-Pi 1034 | Good Luck 1035 | \ 1036 | " 0 0 0 1037 | } 1038 | 1039 | #------------------------------------------------------------------------------ 1040 | function do_main_menu () 1041 | { 1042 | cd $PROG_DIR 1043 | SELECTION=$(whiptail --title "OpenCV Compile Assist from $CUR_OPENCV_VER to $OPENCV_VER" --menu "Arrow/Enter Selects or Tab Key" 0 0 0 --cancel-button Quit --ok-button Select \ 1044 | "1 UPDATE" "Run Raspbian Update and Upgrade" \ 1045 | "2 DEP" "Install Build Dependencies and Download Source" \ 1046 | "3 COMPILE $OPENCV_VER" "Run cmake and make $COMPILE_CORES (be patient)" \ 1047 | "4 INSTALL $OPENCV_VER" "Run make install (Copy Files to production)" \ 1048 | "5 DELETE" "$INSTALL_DIR Source Folder and Files" \ 1049 | "6 SETTINGS" "nano Edit cv3-install-menu.conf File" \ 1050 | "7 UPGRADE" "Program Files From GitHub" \ 1051 | "8 LOG" "View Log File cv3-log.txt" \ 1052 | "9 ABOUT" "Information About This Program" \ 1053 | "A AUTO" "Unattended Install (Do You Feel Lucky)" \ 1054 | "q QUIT" "Exit This Menu Program" 3>&1 1>&2 2>&3) 1055 | 1056 | RET=$? 1057 | if [ $RET -eq 1 ]; then 1058 | exit 0 1059 | elif [ $RET -eq 0 ]; then 1060 | case "$SELECTION" in 1061 | 1\ *) do_rpi_update 1062 | do_main_menu ;; 1063 | 2\ *) do_cv3_dep 1064 | do_main_menu ;; 1065 | 3\ *) do_cv3_compile_menu 1066 | do_main_menu ;; 1067 | 4\ *) do_cv3_install 1068 | do_main_menu ;; 1069 | 5\ *) do_cv3_cleanup 1070 | do_main_menu ;; 1071 | 6\ *) nano cv3-install-menu.conf 1072 | read_config_file 1073 | do_check_cv3_ver 1074 | check_working_dir 1075 | do_main_menu ;; 1076 | 7\ *) do_upgrade 1077 | echo "Upgrade Complete. Restart $0" 1078 | exit 0 ;; 1079 | 8\ *) do_log 1080 | do_main_menu ;; 1081 | 9\ *) do_about 1082 | do_main_menu ;; 1083 | A\ *) do_auto ;; 1084 | q\ *) echo "" 1085 | echo "$0 $PROG_VER written by Claude Pageau" 1086 | echo "Bye ..." 1087 | exit 0 ;; 1088 | *) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;; 1089 | esac || whiptail --msgbox "There was an error running selection $SELECTION" 20 60 1 1090 | fi 1091 | } 1092 | 1093 | clear 1094 | echo "$0 $PROG_VER written by Claude Pageau" 1095 | echo "" 1096 | read_config_file 1097 | do_check_cv3_ver 1098 | check_working_dir 1099 | do_Initialize 1100 | do_main_menu 1101 | 1102 | 1103 | -------------------------------------------------------------------------------- /dphys-swapfile.1024: -------------------------------------------------------------------------------- 1 | # /etc/dphys-swapfile - user settings for dphys-swapfile package 2 | # author Neil Franklin, last modification 2010.05.05 3 | # copyright ETH Zuerich Physics Departement 4 | # use under either modified/non-advertising BSD or GPL license 5 | 6 | # this file is sourced with . so full normal sh syntax applies 7 | 8 | # the default settings are added as commented out CONF_*=* lines 9 | 10 | 11 | # where we want the swapfile to be, this is the default 12 | #CONF_SWAPFILE=/var/swap 13 | 14 | # set size to absolute value, leaving empty (default) then uses computed value 15 | # you most likely don't want this, unless you have an special disk situation 16 | # CONF_SWAPSIZE=100 17 | CONF_SWAPSIZE=1024 18 | 19 | # set size to computed value, this times RAM size, dynamically adapts, 20 | # guarantees that there is enough swap without wasting disk space on excess 21 | #CONF_SWAPFACTOR=2 22 | 23 | # restrict size (computed and absolute!) to maximally this limit 24 | # can be set to empty for no limit, but beware of filled partitions! 25 | # this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it 26 | # but is also sensible on 64bit to prevent filling /var or even / partition 27 | #CONF_MAXSWAP=2048 28 | -------------------------------------------------------------------------------- /menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pageauc/opencv3-setup/0bad5fd65b63faad2354ae9d8d8ca33e0fffb1dc/menu.png -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Convenient opencv3 install script written by Claude Pageau 1-Jul-2016 3 | ver="2.5" 4 | DEST_DIR='opencv3-setup' # Default folder install location 5 | 6 | cd ~ 7 | if [ -d "$DEST_DIR" ] ; then 8 | STATUS="Upgrade" 9 | echo "Upgrade files" 10 | else 11 | echo "New Install" 12 | STATUS="New Install" 13 | mkdir -p $DEST_DIR 14 | echo "$DEST_DIR Folder Created" 15 | fi 16 | 17 | cd $DEST_DIR 18 | INSTALL_PATH=$( pwd ) 19 | 20 | # Remember where this script was launched from 21 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 22 | echo " 23 | ------------------------------------------------------------- 24 | setup.sh script ver $ver 25 | Install or Upgrade opencv3 26 | ------------------------------------------------------------- 27 | 1 - Downloading opencv3-setup github repo files" 28 | 29 | wget -O cv3-install-menu.sh https://raw.github.com/pageauc/opencv3-setup/master/cv3-install-menu.sh 30 | wget -O cv3-install-menu.conf https://raw.github.com/pageauc/opencv3-setup/master/cv3-install-menu.conf 31 | wget -O Readme.md https://raw.github.com/pageauc/opencv3-setup/master/Readme.md 32 | wget -O dphys-swapfile.1024 https://raw.github.com/pageauc/opencv3-setup/master/dphys-swapfile.1024 33 | echo "Done Download 34 | ------------------------------------------------------------- 35 | 2 - Make Required Files Executable" 36 | 37 | chmod +x *sh 38 | 39 | # Delete existing setup.sh and Force running setup from github repo curl command 40 | if [ -f "setup.sh" ] ; then 41 | rm setup.sh 42 | fi 43 | 44 | echo "Done Permissions 45 | ------------------------------------------------------------- 46 | IMPORTANT - It is recommended you have a minimum size 47 | 16GB system SD card with at least 6GB Free 48 | or mount USB Storage Media or Drive and 49 | edit opencv_dir variable in this script. 50 | See Readme.md for Details 51 | 52 | cd ~/opencv3-setup 53 | ./cv3-install-menu.sh 54 | 55 | See ABOUT Menu Pick and Readme.md for more Details. 56 | https://github.com/pageauc/opencv3-setup 57 | 58 | Good Luck Claude ... 59 | Bye" 60 | --------------------------------------------------------------------------------