Housekeeping
close all; clear; clc;
Fresh Install of Bullseye
The first step to get a camera stream to MATLAB is to set up our RaspberryPi with a camera (I am using the Pi Camera v3 NoIR). For this I suggest using the Raspberry Pi Imager software and the default RASPBERRY PI OS(32-BIT). Before you write to the SD card, we'll need to edit a few settings behind the gear icon.
We will want to:
- Enable SSH
- Set a username & password
- Configure wireless LAN
- Set the Wireless LAN Country
- Set the locale settings
Once this is done, we can begin writing to the SD card. Once installed we will want to plug the SD card into the Pi, plug it in and give it a minute to boot. After everything is booted up we will want to:
A. Plug the Pi into a monitor so that we can do things visually
B. Use an application like PuTTY to SSH into our Pi remotely using the username & password
Once you have access to the Pi you will want to open a terminal and run [sudo rasip-config] and enable the legacy camera support. After that is finished you will want to return to the terminal and run [sudo apt-get update && sudo apt-get upgrade] to make sure everything is up to date.
Camera Setup
Unfortunately camera support can be inconsistent on fresh installs, but a few edits will get things working. After rebooting after our updates, open a new terminal and run [sudo nano /boot/config.txt]. You will want to comment out the line "start=1" and add a new line underneath it reading "camera_auto_detect=1". After saving and exiting the file we just need to run the line [sudo modprobe bcm2835-v4l2] in the terminal and reboot. After this the Pi should recognize our camera automatically on boot without any intervention. We can check this run running the line [libcamera-vid -t 0] to open a camera stream.
Install & Configure "Motion"
After rebooting, open a terminal and run [sudo apt-get install motion] to install the Motion library. After than run the command [sudo nano /etc/motion/motion.conf] to open the configuration file and match the following settings. Once done, reboot.
Note: You may want to edit the width, height and rotate parameters depending on your installation.
# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 4.3.2
# Documentation: /usr/share/doc/motion/motion_guide.html
#
# This file contains only the basic configuration options to get a
# system working. There are many more options available. Please
# consult the documentation for the complete list of all options.
#
############################################################
# System control configuration parameters
############################################################
# Start in daemon (background) mode and release terminal.
daemon off
# Start in Setup-Mode, daemon disabled.
setup_mode off
# File to store the process ID.
; pid_file value
# File to write logs messages into. If not defined stderr and syslog is used.
#log_file /var/log/motion/motion.log
log_file /home/log/motion/motion.log
# 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 /var/lib/motion
# Video device (e.g. /dev/video0) to be used for capturing.
videodevice /dev/video0
# Parameters to control video device. See motion_guide.html
; vid_control_params value
# The full URL of the network camera stream.
; netcam_url value
# Name of mmal camera (e.g. vc.ril.camera for pi camera).
; mmalcam_name value
# Camera control parameters (see raspivid/raspistill tool documentation)
; mmalcam_control_params value
############################################################
# Image Processing configuration parameters
############################################################
# Image width in pixels.
width 1280
#width 1920
# Image height in pixels.
height 720
#height 1080
# Maximum number of frames to be captured per second.
framerate 60
# Image Flip
#rotate=270
# Text to be overlayed in the lower left corner of images
#text_left CAMERA1
# Text to be overlayed in the lower right corner of images.
#text_right %Y-%m-%d\n%T-%q
text_right
############################################################
# Motion detection configuration parameters
############################################################
# Always save pictures and movies even if there was no motion.
emulate_motion off
# Threshold for number of changed pixels that triggers motion.
threshold 1500
# Noise threshold for the motion detection.
; noise_level 32
# Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl
# Number of images that must contain motion to trigger an event.
minimum_motion_frames 1
# Gap in seconds of no motion detected that triggers the end of an event.
event_gap 60
# The number of pre-captured (buffered) pictures from before motion.
pre_capture 3
# Number of frames to capture after motion is no longer detected.
post_capture 0
############################################################
# Script execution configuration parameters
############################################################
# Command to be executed when an event starts.
; on_event_start value
# Command to be executed when an event ends.
; on_event_end value
# Command to be executed when a movie file is closed.
; on_movie_end value
############################################################
# Picture output configuration parameters
############################################################
# Output pictures when motion is detected
picture_output off
# File name(without extension) for pictures relative to target directory
picture_filename %Y%m%d%H%M%S-%q
############################################################
# Movie output configuration parameters
############################################################
# Create movies of motion events.
movie_output on
# Maximum length of movie in seconds.
movie_max_time 60
# The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 45
# Container/Codec to used for the movie. See motion_guide.html
movie_codec mkv
# File name(without extension) for movies relative to target directory
movie_filename %t-%v-%Y%m%d%H%M%S
############################################################
# Webcontrol configuration parameters
############################################################
# Port number used for the webcontrol.
webcontrol_port 8080
# Restrict webcontrol connections to the localhost.
webcontrol_localhost off
# Type of configuration options to allow via the webcontrol.
webcontrol_parms 0
############################################################
# Live stream configuration parameters
############################################################
# The port number for the live stream.
stream_port 8081
# Maximum streaming rate
stream_maxrate 60
# Restrict stream connections to the localhost.
stream_localhost off
##############################################################
# Camera config files - One for each camera.
##############################################################
; camera /usr/etc/motion/camera1.conf
; camera /usr/etc/motion/camera2.conf
; camera /usr/etc/motion/camera3.conf
; camera /usr/etc/motion/camera4.conf
##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /usr/etc/motion/conf.d
Configure Motion for Autostart
Once rebooted, open a new terminal and run the following commands in order. This will update the location of our log files to avoid permission errors, stop the motion process, and disable it from autorunning.
- sudo mkdir /home/log/motion
- sudo touch /home/log/motion/motion.log
- sudo chown motion:motion /home/log/motion/motion.log
- sudo systemctl stop motion
- sudo systemctl disable motion
After saving, closing, and rebooting open a new terminal and run [sudo nano /etc/rc.local]. We will want to add a new line at the bottom (but before exit 0) reading [sudo libcamerify motion]. This will mean Motion will start at boot, but in a manner compatible with the Raspberry Pi's new libcamera library
Success!
If you've followed all of these steps, your Raspberry Pi should automatically start an RTMP video stream of it's camera feed.