Camera on a Raspberry Pi

In my post about getting Flask running as a service, I included a screenshot with a large dark area as the background. In this post, I will work on filling it with an image from a web camera, which will run on the same Raspberry Pi. I will cover the guages in a later post.

My ROV Flask app running on port 8082, with out the webcam
My ROV Flask app is running on port 8082, without the camera feed.

The software I am using is called Motion and is available for download from the project GitHub. The two cameras I am using are:

  • An aftermarket Raspberry Pi camera, with IR led’s1, however, it is quite old
  • An inexpensive generic USB camera, probably about 5 years old.
Mess of wires that makes up my test setup, The two cameras, the Adeept Robot HAT for Raspberry Pi, and the Servo that I am testing with.
Mess of wires that makes up my test setup, The two cameras, the Adeept Robot HAT for Raspberry Pi, and the Servo that I am testing with.

Getting Motion onto the Pi with a USB Cam

SSH onto the Raspberry Pi and log in. The following are run from the Pi, at this point, the USB camera is the only one attached. I haven’t managed to get the latest version of Motion working, so at the moment I am using Motion 4.7.0 for the 64-bit ARM. 4.7.1 fails due to having unmet dependencies.

cd ~
wget https://github.com/Motion-Project/motion/releases/download/release-4.7.0/bookworm_motion_4.7.0-1_arm64.deb -O motion.deb
sudo apt update
sudo apt upgrade
sudo apt dist-upgrade
sudo apt install autoconf automake build-essential pkgconf libtool libzip-dev libjpeg-dev gettext libmicrohttpd-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev default-libmysqlclient-dev libpq-dev libsqlite3-dev libwebp-dev libcamera-v4l2
sudo dpkg -i motion.deb
sudo nano /etc/motion/motion.conf

My config differs from the basic config, but these changes will get you started:

Find the following lines and ensure they are set to the values below.

daemon off
stream_localhost off

To enable motion as a demon so it starts every boot, and to get it started running now, enter the following commands:

sudo systemctl enable motion
sudo systemctl start motion
My ROV Flask app running on port 8082, now with added webcam, this is the USB camera.
My ROV Flask app is running on port 8082, now with an added webcam, which is the USB camera.

The config file will tell you what the default port for the webcam is. I have mine running on Port 8081. There are other changes in my config file that also help to speed up the frame rate; we will cover them in a bit.

You can’t see much in this image, it’s pointing at a keyboard… but it does prove it’s working (the masking tape pointing it at a servo has given up at the moment).

I mentioned a second camera…

Motion with a Raspberry Pi Camera

Motion doesn’t support the Raspberry Pi Cam out the box,

the command v4l2-ctl –list-devices tells me I have a few video devices available:

The CSI ones are the Raspberry Pi Camera; the USB Camera is listed as video_device /dev/video in the motion config that is working above.

According to cat /etc/os-release:

cat /etc/os-release

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Because I am running Bookworm, it turns out that legacy camera has been removed in Debian Bookworm, and Motion will NOT work directly with the Pi Camera unless you add a compatibility layer.

sudo apt install libcamera-apps v4l-utils
sudo nano /boot/firmware/config.txt
  • Add the lines:
    • start_x=1
    • gpu_mem=256
  • Comment out
    • # camera_auto_detect=1

My three config files are now:

/etc/motion/motion.conf

daemon off
stream_localhost off
webcontrol_localhost off

# Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL).
log_level 6

# Target directory for pictures, snapshots and movies
target_dir /home/pi/vids


camera /etc/motion/motion1.conf
camera /etc/motion/motion2.conf

/etc/motion/motion1.conf

############################################################
# Image Processing configuration parameters
############################################################

camera_name USBcam

# USB Webcam
video_device /dev/video0

# Image width in pixels.
width 640

# Image height in pixels.
height 480

# Maximum number of frames to be captured per second.
framerate 30

# Text to be overlayed in the lower left corner of images
text_left CAMERA 1\n%{fps} FPS

# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q

v4l2_palette 17
stream_port 8081
webcontrol_port 8088

/etc/motion/motion2.conf

############################################################
# Image Processing configuration parameters
############################################################

camera_name PiCam

# Raspberry Pi Camera (CSI)
videodevice /dev/video2

# Image width in pixels.
width 640

# Image height in pixels.
height 480

# Maximum number of frames to be captured per second.
framerate 30

# Text to be overlayed in the lower left corner of images
text_left CAMERA 2\n%{fps} FPS

# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q

v4l2_palette 17    # YUYV
stream_port 8083
webcontrol_port 8089

Image from my second camera (CSI RPi Cam):

View from my CSI RPi Camera.
View from my CSI RPi Camera.

Image from my first camera (USB Cam)

View from my USB Camera.
View from my USB Camera.

Given the hassle of using the CSI Camera, when I update the software from Raspberry Pi Debian to Bookworm, to Trixie I suspect I will move to just using USB Cameras.

  1. Removed as they caused the RPi to brownout

Leave a comment

Your email address will not be published. Required fields are marked *

The maximum upload file size: 20 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop files here