Students will learn how to install software on a UNIX computer system. To test the installation, they will hook up a camera and run example face tracking python program and a target recognition program using computer vision.
Learning outcome:
Prepare portable computing platform and practice Unix shell skills. Observe Unix open software distribution through online repositories both in binary as well as source code.
Urs Utzinger, Updated 1/14/2024
Your Raspberry Pi
Network Connection
When 30 students attempt downloading software packages the network will slow down. You can attempt connecting to UAWiFi. You can measure network speed in a web browser using by searching for speedtest.
Please add these libraries and tools to your raspberry pi. You will need to answer questions with "Y". All commands below will need to complete without errors.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libopencv-dev
sudo apt-get install opencv-data
sudo apt-get install python3-opencv
sudo apt-get install v4l-utils
sudo apt-get install i2c-tools
sudo apt-get install libcamera-v4l2
sudo apt-get install libcamera-tools
Update the python setup.
sudo apt-get install python3-dev python3-pip
sudo apt-get install python3-numpy
sudo apt-get install python3-setuptools
sudo apt-get install python3-wheel
sudo apt-get install python3-mock
sudo apt-get install python3-smbus
sudo apt-get install python3-picamera2
Raspian bookworm expects you to work in virtual environment. You can create several virtual environments. Often you need one version of a library for one application but an other for an other application. This allows to easily switch to different setting. We will make one for BME210:
mkdir ~/pythonBME210
cd ~/pythonBME210
python3 -m venv --system-site-packages env
To activate Python Virtual Environment (each time after you power on)
cd ~/pythonBME210
source env/bin/activate
To deactivate Python Virtual Environment
deactivate
You can run and debug your python code in Thonny. Its light weight and works for most of your code. However, in order to use Thonny and the virtual environment you need to change the Python interpreter in Thonny to the one in the virtual environment.
Switch Thonny to regular mode: Top right.
Restart Thonny
In Tool->Options select interpreter.
Browse for python3 in the ~/pythonBME210/env/bin/python3
Visual studio code needs more resources than other editors but I still prefer it on RasPi. Usually the binaries are available at Microsoft: https://code.visualstudio.com/#alt-downloads however for the Raspberry Pi you will need to use an other approach:
sudo apt install code
In the Raspberry Pi menu under programming you should be able to find Visual Studio Code.
Open the program and click on the 4 squares on the left side.
Install Python Intellisense and Plance by Microsoft.
Visual Studio Code will recognize your virtual environments if you keep them in the same folder as your project and use that environment.
cd ~/pythonBME210
source env/bin/activate
We will use these libraries later for the air quality sensor and the motor assignment.
pip3 install adafruit-pureio
pip3 install adafruit-blinka
pip3 install adafruit-circuitpython-htu21d
git clone https://github.com/adafruit/Adafruit_CircuitPython_HTU21D.git
pip3 install adafruit-circuitpython-motorkit
git clone https://github.com/uutzinger/meArmPi.git
The meArmPi library I will need to update to work with the new motorkit library. Later, you can update it with git pull when you are in the meArmPi folder.
Obtain one of the CSI cameras from the course staff. You will need also the flat ribbon cable.
Make sure the Raspberry Pi is turned off.
Insert the cable into the camera slot of the Raspberry Pi. Gently pull the release hooks towards you. Orient the cable with the pads towards the visible metal connector pins and slide the cable into the connector. Push the cable hook back in. Have the connection inspected by course staff.
Make sure no metal parts touch your camera and the exposed connections are covered up.
Power on the Raspberry Pi.
To check if the camera works execute in a terminal libcamera-hello -t 0
We would like to have a graphical interface to check the camera. If you know a better solution let me know, otherwise donwload the RPiCamGUI.py file from: https://github.com/Gordon999/RPiCamGUI
You can do this in the terminal with:
git clone https://github.com/Gordon999/RPiCamGUI.git
This will create a folder called RPiCamGUI. Then you can run it with
cd ~/RPiCamGUI
python3 RPiCamGUI.py.
You can test opencv installation with:
python3
import cv2 If this does not complete successfully you did not complete the installation script above.
cv2.__version__
should display version number
exit()
As next step we want to test the camera with a face detection algorithm.
You will need to obtain the following python programs from https://github.com/opencv/opencv/tree/master/samples/python
facedetect.py
common.py
tst_scene_renders.py
We will use the script below to obtain them and copy everything in a folder called facedetect in our pythonBME210 directory on the raspberry pi.
cd ~/pythonBME210
mkdir facedetect
cd facedetect
wget https://raw.githubusercontent.com/opencv/opencv/master/samples/python/facedetect.py
wget https://raw.githubusercontent.com/opencv/opencv/master/samples/python/common.py
wget https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tst_scene_render.py
mkdir haarcascades
cp /usr/share/opencv4/haarcascades/haar* ./haarcascades
You need to modify the facedetect .py program using visual studio code or Thonny. That is because we can no longer use opencv directly to access the CSI camera.
1) Replace
from video import create_capture
with
from picamera2 import Picamera2
2) Replace
cam = create_capture(video_src, fallback='synth:bg={}:noise=0.05'.format(cv.samples.findFile('lena.jpg')))
With
picam2 = Picamera2()
picam2.start()
This will also remove reference to lena.jpg as in professional environment, we should no longer use the Lena Playboy centerfold picture.
3) Replace
_ret, img = cam.read()
Width
img = picam2.capture_array()
You can run the face detector now with python3 ./facedetect.py
Does it work? Press "esc" key to exit the camera.
The program will run faster if the images are smaller. You can add the line img=cv.resize(img, (0,0), fx=0.5, fy=0.5) This will reduce the image size by a half in both directions. Run it again. Its it any faster?
Its is possible to configure the camera so that we read smaller images without needing to scale them with resize. Camera Control documentation is in Chapter 5 of https://datasheets.raspberrypi.com/camera/picamera2-manual.pdf For now we skip that step.
You will need to show the lab staff that your micro controller shows appropriate time. When your Raspberry Pi boots, it might be able to obtain the time over the internet but if that does not complete, your time will be off. This will result in the security certificates not to be accepted and all your https connections will fail.
If you can reliably boot your Raspberry Pi with the correct time and date you do not need the hardware clock.
To install the hardware clock physically on your expansion bus, please turn off your raspberry pi by shutting it down and removing the power cord, then plug it it. Please check twice that the pins align before powering up again. It uses the very first pin and a total of 5 pins. It uses the row of pins towards the center of the board as show below.
Attach the clock as show in the picture at the very bottom of this page.
Open a terminal window/shell:
check if the clock is attached: sudo i2cdetect -y 1, This command will show you all i2c devices connected to your computer. It will need to display UU at address 68. If it does not display UU or 68, the clock is not installed properly.
If it displays 68 instead of UU you need to install the hardware clock driver: sudo nano /boot/config.txt and to enable the hardware clock add the line with dtoverlay=i2c-rtc,ds3231 at the bottom. Ctrl-X and Y to exit and save the file. Then, reboot your Raspberry Pi.
Now make sure the clock is working properly: sudo hwclock -D -r . This command will show you the time in the hardware clock.
date will show you the system time.
sudo date -s "your new date and time" . Best is to copy date and time from above output and change the text where needed.
sudo hwclock -w will write the date and time back onto the hardware clock. Next time you boot the raspberry pi it will have the correct time as the hardware clock runs on a battery.
Show that the time of your raspberry pi is correct after you boot it to the course staff.
In this assignement you used many unix commands. You could enter them into your cheat sheet for unix commands.