Raspberry Pi HD web cam

- RaspberryPi
The camera is build around the Raspberry Pi Zero W (that does have wireless LAN connectivity) and the Raspberry Pi High Quality Camera that does have a Sony back-illuminated sensor with great size compared to the super tiny one normally used with the Raspberry Pi (sensor size is way more important that pixel count for the final image quality).
It does also have a C and CS mount for choosing among different lenses (this typology of lenses are used for security cameras or industrial applications). Thanks to that, it's possible to use the camera with a wide choice of lenses (high quality made with glass as well, not done with the cheap plastic as the tiny one) and the camera can be used in a wide range of applications, like for a wide angle camera, a telescope one or a microscope as well.
The lenses used in this post are from the company ArduCam that does offer a lot of solutions for many platforms, like Raspberry Pi obviously as well as NVIDIA Jetson, OpenCV AI, SPI solution cameras for microcontroller etc.
Goals
The goal is to build a compact customizable high quality 1080p HDMI camera for any possible use, ready to me mounted with a standard 1/4" screw on any existing tripod and fixture for flexible positioning. It should also have some buttons to easily flip the image to quickly adjust the orientation (so no need to do that in post or use OBS).
Quick and fast setup recap
-
Install the Raspberry Pi OS on the micro SD card by using the Raspberry Pi Imager. In the Advanced options setup the SSH credentials and also a custom hostname for the Raspberry Pi as well as the Wifi connections settings. In this way once booted, the Pi will be autmatically on the local network without the need to use mouse and keyboard.
-
Install the SD and power it up. Access via terminal (mac and linux) of PuTTY. Via terminal (mac and linux) write
ssh user@hostname.local
to access to the Pi (example:ssh pi@raspberry-pi-cam.local
). With PuTTY insert just the hostname.local in the hostname field and follow the login procedure in the terminal. -
Activate the camera port (interface) and set the screen resolution to 1920x1080 in the raspberry config
sudo raspi-config
. Exit the config and reboot the Pi withsudo reboot
. You have to do the same SSH connection procedure of the point 2. to reconnect afterwards. -
Update, upgrade, install python3, python3-camera and download the script from GitHub. Run the script to test that everything is working:
$ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get install python3 $ sudo apt-get install python3-picamera $ sudo apt-get install git $ git clone https://github.com/marclura/raspberry-pi-hd-web-cam.git $ cd raspberry-pi-hd-web-cam/code $ python3 web-cam.py
-
Set the camera script to start automatically at power on.
$ sudo chmod +x web-cam.py $ sudo nano /etc/rc.local // add this line to the rc.local file before the last line "exit 0", then close and save the file: sudo python3 /home/pi/raspberry-pi-hd-web-cam/code/web-cam.py & $ sudo reboot
Raspberry Pi Zero W setup
The first thing to do is to flash the micro SD card with the Raspbian OS by using the official Raspberry Pi Imager.
To avoid the need to connect the keyboard and mouse to setup the Raspberry Pi Zero (Pi from now on), open the "Advanced options" menu in the Raspberry Pi Imager (from v1.6 only by pressing Ctrl-Shift-X or latest version does have the icon for that). There check the boxes for "Set hostname" (and give a name for you Pi on the network, so you can search for it not only through its IP address but with nice simple name that you choose), "Enable SSH" and set a strong password to access to it, "Configure wifi" by inserting you local wifi network name (SSID) and password as well, and last "Set locale settings" for time zone and keyboard layout.
Note: if you have problems with the wifi (company firewall & co.), you can use your smartphone as hotspot, just insert the wifi name (SSID) and password of your hotspot. You will see your Pi appear in the devices connected to your hotspot to witch you can connect your computer as well to communicate together.
This initial setup will make the Pi connect automatically to your wifi network so that you can access to it directly form your main computer.
Save the "Advanced options" to go back, and choose as Operating System the first recommended option, the Raspberry Pi OS (32-bit).
As storage indicate the micro SD card (careful not to choose an internal disk of your computer, it will be completely erased if you do so!). Click on Write to flash the SD card, administrator password will be required to proceed.
Once the writing process is done the micro SD card can be inserted in the Pi and then it can be powered up by connecting the cable in the micro USB port that says "PWR_IN". Wait for the Pi do do the first boot (it will take a moment).
Download and install the Raspberry Pi Imager (version 1.6 or up) Press Ctrl-Shift-X to open the Advanced options menu and set the local hostname, SSH and Wifi for the Pi for it to automatically connect to your network without the need of keyboard and mouse Choose the recommended Raspberry Pi OS version and proceed with the witing of the micros SD card
Connect to the Pi via SSH
At this point the Pi should be connected to your local network via wifi and accessible with SSH.
Windows
Download and install PuTTY. Insert your Pi hostname that you did choose previously with the Raspberry Pi Imager and select SSH as connection type, click on Open to start the connection.
Mac/Linux
Open the terminal and type ssh
followed as well by the Pi hostname you did choose previously with the Raspberry Pi Imager, for consistency in this post, it has been used the one from the screenshots as an example:
ssh pi@raspberry-cam-1.local
In case this doesn't work, you have to know the IP address of your Pi on your local network; access your router (type in your browser 192.168.3.1 to access it, this is normally the IP address of your router) and you will see the list of devices that are connected with the corresponding IP.
If you did the hotspot point with your smartphone you should have all the list of the connected devices, so you should be able to know the IP address of the Pi. The command will be similar then, but instead of using raspberry-cam-1.local
use the IP address, as and example it could be something like 192.168.1.52
:
ssh pi@192.168.1.52
With PuTTY on Windows insert your Pi hostname that you did setup before with the Raspberry Pi Imager and choose SSH as connection type The terminal will ask for the loging. Type pi as login and then the password you choose from the previous Raspberry Pi Imager SSH setup
Once successfully connected, the terminal will ask you for the login. Type pi
as login user if asked (PuTTY) and then the password you choose from the previous Raspberry Pi Imager SSH setup.
At this moment you are inside the Pi filesystem via the command line (CLI)! It's possible to navigate inside it, create files and launch commands. The main commands required to know that you can type and execute by pressing Enter on your keyboard are the following:
cd ..
move up one folder
ls
to show the list of the files available in your current folder
cd foldername
enter in the folder with the name you specified
rm filename.txt
delete a file that is in the current folder !attention on what you delete
rmdir foldername
delete a folder !attention on what you delete
Setup the Pi
The first thing to do is to activate the camera in the configurations. According on the version of the OS installed, some options are called and located differently, so just search in the menu to find them. Type this in the terminal:
sudo raspi-config
Camera activation
To activate the camera, scroll down with the arrow keys to Interface Options > Camera and a prompt window will ask you if you would like the camera interface to be enabled, say Yes. A confirmation will appear.
Video resolution change
Navigate to Display Options > Resolution and scroll down with the arrow keys to CEA Mode 34 1920x1080 30Hz 16:9 and press Ok.
Command to enter the Pi configuration tools Camera activation | 1: Interface Options Camera activation | 2: Camera Camera activation | 3: Would you like to enable the camera? Yes Camera activation | 4: Camera interface enabled Video resolution change | 1: Display Options Video resolution change | 2: Resolution Video resolution change | 3: choose the CEA Mode 34 1920x1080 30Hz 16:9 Video resolution change | 4: Confirmation that the resolution has been changed to CEA 34 mode Exit the Configuration Tool by pressing the right arrow to select "Finish" Don't reboot the Pi right now, so choose "No" to go back to the command line
Packages installation
In order to use the camera it's necessary to install some packages. First the system has to get the list of the last available versions of the packages and then upgrade and install them.
To do this type in the terminal the first following command command and press enter to execute it and once done then the second one.
If the terminal asks for a [Y/n] just type "Y"and press enter again. I could take a while to upgrade:
sudo apt-get update
sudo apt-get upgrade
The next step is to install the python packages to use the camera, type this in the terminal:
sudo apt-get install python-picamera python3-picamera
Camera script
At this point the Pi is ready to run any python script. The one that will be used here can be found at this GitHub repository. You can either download it and upload it to the Pi or create a new file with the nano editor of the terminal.
Option 1: create the script from the terminal
Type the following command to open the nano text editor and create a new file:
sudo nano
Select all the code from this GitHub repository, copy it and paste it by just pressing the right click of the mouse in the nano editor.
That's all, press Ctrl+X to Exit, you will be asked if you want to save the file, press Y to confirm, type the name of the file (we will call it web-cam.py
) and press Enter to finish.
You can now see the newly create file if you type:
ls
Nano editor | 1. This is the editor open on a new empty file Nano editor | 2. Right mouse click on the editor to paste the code from the repository Nano editor | 3. Ctrl+X to Exit the editor, and then press Y to confirm that you want to save the file Nano editor | 4. Write the name of the file that you would like to use, in this case web-cam.py and press Enter to confirm and exit the editor The "ls" command shows the new created file
Option 2: upload the script with FileZilla to the Pi
The other method is to use a FTP program to access to the Pi and copy the script in it. First thing you have to download the script from GitHub. Right click on the "RAW" icon and select "Save link as ..." to download the web-cam.py file.
Download and Install the FileZilla client.
In FileZilla click on File > Site Manager and click on "New site". Select the potocol SFTP - SSH File Transfer Protocol, and insert in host the hostname of the Pi (raspberry-cam-1) on the port 22.
Use the Normal logon type and use "pi" as user and the SSH password. Click Connect to continue.
On the left hand side you have your computer, on the right hand side the Pi. Navigate to your desktop and upload the web-cam.py file.
Once done close FileZilla.
GitHub | Download the web-cam.py and save it on your desktop FileZilla | 1. Enter in the Site manager FileZilla | 2. Create a New site, choose SFTP - SSH as Protocol, enter the Host name raspberry-cam-1 and indicate the Login credentials user pi and the password for the SSH. Click on Connect to proceed. FileZilla | 3. Upload the web-cam.py file to the Pi FileZilla | 4. Check that the web-cam.py script has been uploaded succesfully. You can close FileZilla.
Run the script
The script is ready to be run. To do that you have to make it executable by typing this command in the terminal:
sudo chmod +x web-cam.py
To test it run this command and attach the Pi with the micro HDMI cable to your monitor or video input:
python web-cam.py
You should be able to see the image form the camera, and with the rotate button to flip the image. To stop the script from running, press Ctrl+C.
Set the script to run automatically at startup
To run automatically the script and use the camera once the Pi is powered up, it's necessary to add the script to the rc.local file (that will be executed as administrator at the system startup):
sudo nano /etc/rc.local
Add the following line just above the "exit 0" text. Pay attention to not delete or modify anything else!
sudo python /home/pi/web-cam.py &
Interesting note: the "&" is necessary because it will indicate to the Pi that it doesn't have to wait for ending this script to do something else, but the "&" will say that it can run other task in parallel with the web-cam.py.
Press Ctrl+X to Exit, Y to save, and Enter to save the file with the same current name and exit from the nano editor.
From the terminal reboot the Pi to see if the script properly starts at the beginning:
sudo reboot
To do any modification, you have to connect again via SSH as before (when the Pi reboots, it cuts the communication).
Run | 1. Make the file executable with the chmod +x command Run | 2. Test the script to check that everything does work. Connect a monitor or console via mini HDMI - HDMI cable. Press the keys Ctrl+X to stop it. Run | 3. Add the script to the /etc/rc.local file to make it run automatically at the startup Run | 4. Reboot the Pi and check that the script is run auromatically and the video does work as well