Skip to content

Instantly share code, notes, and snippets.

@gagregrog
Last active August 13, 2018 19:00
Show Gist options
  • Save gagregrog/9173e419322b604cf2490058b3b86e15 to your computer and use it in GitHub Desktop.
Save gagregrog/9173e419322b604cf2490058b3b86e15 to your computer and use it in GitHub Desktop.

Raspbian Stretch with OpenCV 3.3 - Setup

Follow these steps to flash a pre-built Raspberry Pi image to an SD card. This image is established with everything documented in this original setup walkthrough. This install requires a MicroSD card with at least 32GB of storage. This will save you many hours of setup, specifically because you don't need to compile OpenCV from source.

Gather Required Components

  1. Raspberry Pi (Pi 3 or Pi Zero) and 32+GB MicroSD card.
    • Pi 3:
      • Ethernet Cable to plug in directly
      • Another computer (Mac)
      • If you don't have the ability to hook up ethernet, you will need the same peripherals as the Pi Zero.
    • Pi Zero:
      • Mini HDMI to HDMI
      • Micro USB to USB A
      • USB Keyboard
      • External Display
  2. Download and install VNC Viewer on your computer.
  • Once your Pi is setup, you should follow the VNC setup instructions from the original walkthrough.
  1. Download and install ApplePi-Baker on your computer.
  2. Download the zipped IMG file from Dropbox. This is a large file (~4gb) and will likely take a while to download.

Flashing the IMG to the SD Card

  1. Open up ApplePi-Baker and insert your SD card. Enter your computer's Admin password when prompted.
  2. You should see the SD card populate on the left in the "Pi-Crust" section. If not, click the refresh button.
  3. With your SD card selected, click "Prep for NOOBS" to format the card with a fat32 partition.
  4. From the Pi-Ingredients section, provide the path to the zipped IMG file you downloaded from dropbox.
  5. Click "Restore Backup" and wait (for probably about 20 minutes).

If you ever want to make a backup of your Raspberry Pi setup in the future, you can do so with ApplePi-Baker. That's how this IMG file was made in the first place.

Connecting to the Pi the First Time

If all went well, your card flashed correctly and you should be able to plug your SD card into your Pi. By default, you will be setup with the following credentials:

  • Username: pi
  • Hostname: raspberrypi
  • Password: raspberry

SSH and VNC will be enabled.

The process for connecting to your pi depends on whether you can plug into an ethernet cable.

With Ethernet

  1. Insert your MicroSD card, plug in your ethernet cable, and power on the Pi.
  2. From your computer, open up VNC viewer.
  3. Connect to your Pi by typing raspberrypi.local in the top bar. Provide the username "pi" and password "raspberry" when prompted. You should now see the Pi's desktop.

Without Ethernet

  1. Insert your MicroSD card, connect your USB keyboard and external display, and power on the Pi.
  2. After a short boot you should see the Pi's desktop.

Once you see the desktop

  1. From the task bar at the top of the screen, click the icon with an up and down arrow (or the lines with red Xs) and connect to wifi.
  • At this point, on the Pi Zero you can disconnect your peripherals and connect via a computer and VNC as shown above.
  1. From the Start Menu, select Preferences > Raspberry Pi Configuration.
  2. Change your password.
  3. Change your hostname to something distinct.
  4. Click OK and reboot your Pi.

At this point, we no longer need to interact with the GUI, and will connect over ssh instead.

Connecting Over SSH

  1. From your computer, open a terminal window and type ssh pi@yourHostname.local, replacing "yourHostname" with the hostname you set a moment ago.
  2. Type the password you set a moment ago when prompted.
  • If you encounter a "known hosts" issue, see the original walkthrough for help in dealing with that.

Changing the SSH Port

  1. ssh into your pi and type sudo nano /etc/ssh/sshd_config
  2. Near the top of the file you will see "#PORT 22". Remove the "#" and change 22 to another number. For example, 9001.
  3. Exit and save the file.
  4. Edit the settings for fail2ban by typing sudo nano /etc/fail2ban/jail.local
  5. Change the value of port from 22 to whatever you just chose:
  6. Exit, save, and reboot your Pi.

From now on when you connect you will have to provide the flag -p <yourPort>. For example, ssh pi@raspberrypi.local -p 9001. I would recommend setting up a bash alias for this on your computer:

  • nano ~/.bash_profile
  • Add alias pi-ssh='ssh pi@raspberrypi.local -p 9001' replacing with your information where necessary. Exit, save, and close/reopen terminal for the changes to take effect.

Now would be a good time to look into setting up a static ip and port forwarding on your router. Take a look at the original walkthrough for help in dealing with that.

Virtualenvs and OpenCV

You should now be set up with a clean install of Raspbian Stretch including OpenCV 3.3 and Python virtual environments. Use your virtual environments in the same way you normally would. If the workon or mkvirtualenv commands don't work for you, type source ~/.profile and try them again.

To create a new virtualenv named test, type mkvirtualenv test. This will create and activate that environment. Anytime you enter a new terminal shell, refresh the source, or reboot, make sure to re-enter the environment before installing any packages.

Use workon yourEnvironmentName to reactivate the environment, and deactivate to deactivate it.

If you are creating an environment and would like to use OpenCV in your project, after creating the environment type linkcv yourEnvironmentName to symlink CV2 into that environment.

You can test that this worked through Python. From inside the virtualenv, type python to enter the interactive shell. Notice that this will default to python3. Type import cv2 to import the package. Type cv2.__version__ and you should see "3.3.0". If the symlink worked correctly, you won't receive an error. If you do receive an error, try running the "linkcv" command again and double checking you typed the name of the environment correctly.

Raspberry Pi Zero

If you've flashed this for use on a Pi Zero, there's an additional step you need to perform to get CV working properly. If you've tried the above, you likely ran into the following error: ImportError: numpy.core.multiarray failed to import. This image was created on a Pi 3, so OpenCV was setup using an armv7 wheel of numpy. In order to work properly on the Zero, an armv6 wheel is needed. From inside the environment you want to use OpenCV in, type pip install numpy. This should install the proper version of numpy, and hopefully importing cv2 will now work properly for you.

Summary

That's it! As always, you should periodically update your Pi.

  • sudo apt-get update
  • sudo apt-get dist-upgrade -y
  • sudo apt-get upgrade -y

Have fun at start making some cool computer vision projects!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment