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.
- 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
- Pi 3:
- Download and install VNC Viewer on your computer.
- Once your Pi is setup, you should follow the VNC setup instructions from the original walkthrough.
- Download and install ApplePi-Baker on your computer.
- Download the zipped IMG file from Dropbox. This is a large file (~4gb) and will likely take a while to download.
- Open up ApplePi-Baker and insert your SD card. Enter your computer's Admin password when prompted.
- You should see the SD card populate on the left in the "Pi-Crust" section. If not, click the refresh button.
- With your SD card selected, click "Prep for NOOBS" to format the card with a fat32 partition.
- From the Pi-Ingredients section, provide the path to the zipped IMG file you downloaded from dropbox.
- 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.
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.
- Insert your MicroSD card, plug in your ethernet cable, and power on the Pi.
- From your computer, open up VNC viewer.
- 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.
- Insert your MicroSD card, connect your USB keyboard and external display, and power on the Pi.
- After a short boot you should see the Pi's desktop.
- 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.
- From the Start Menu, select Preferences > Raspberry Pi Configuration.
- Change your password.
- Change your hostname to something distinct.
- Click OK and reboot your Pi.
At this point, we no longer need to interact with the GUI, and will connect over ssh instead.
- From your computer, open a terminal window and type
ssh [email protected]
, replacing "yourHostname" with the hostname you set a moment ago. - 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.
- ssh into your pi and type
sudo nano /etc/ssh/sshd_config
- Near the top of the file you will see "#PORT 22". Remove the "#" and change 22 to another number. For example, 9001.
- Exit and save the file.
- Edit the settings for fail2ban by typing
sudo nano /etc/fail2ban/jail.local
- Change the value of port from 22 to whatever you just chose:
- Exit, save, and reboot your Pi.
From now on when you connect you will have to provide the flag -p <yourPort>
. For example, ssh [email protected] -p 9001
. I would recommend setting up a bash alias for this on your computer:
nano ~/.bash_profile
- Add
alias pi-ssh='ssh [email protected] -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.
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.
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.
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!