USB Webcam for Octoprint
OctoPrint has the ability to ingest a video stream allowing you to monitor your printing and create time-lapse videos. There are many possible sources for the video stream, but the simplest method could be using a USB webcam.
There are many programs available on Linux to get a video feed from a USB camera, but I found the open source software cam2web to be the easiest to use. Follow the steps below to get it working.
Get cam2web
You will need Git installed on your system to get the source for cam2web.
You will also need to install make and libjpeg-dev using the command below.
sudo apt install make libjpeg-dev
Clone the source code for cam2web using the command below.
git clone https://github.com/cvsandbox/cam2web.git
This will create a folder in your current working directory called cam2web
containing the source. Move into the cam2web directory using the command below.
cd cam2web
Build
There are two parts to cam2web, a web application called web2h and the cam2web application. The web application will let you preview the camera stream through a web browser, and it provides an API for other applications to access the stream. The web2h application converts the web files into header files, this lets the cam2web application run without requiring external files.
Build web2h
pushd .
cd src/tools/web2h/make/gcc/
make
popd
Build cam2web
This section will only work if the previous step finished successfully.
On any generic Linux run the commands below.
pushd .
cd src/apps/linux/
make
popd
If you’re on a Raspberry Pi run these commands instead.
pushd .
cd src/apps/pi/
make
popd
The executables cam2web
and web2h
will be in the folder /build/gcc/release/bin
within the cam2web
folder. Its a good idea to move these somewhere easier to find, I would create a folder called cam
within the home folder, and move these two executables there using the commands below.
mkdir ~/cam
mv ~/cam2web/build/gcc/release/bin/* ~/cam/
cd ~/cam
Run cam2web
While the windows version of cam2web has a GUI, the Linux version does not. Run the command below to see all the commands cam2web accepts.
./cam2web -?
cam2web - streaming camera to web
Version: 1.1.0
Available command line options:
-dev: Sets video device number to use.
Default is 0.
-size:<0-12> Sets video size to one from the list below:
0: 320x240
1: 480x360
2: 640x480 (default)
3: 800x600
4: 960x720
5: 1120x840
6: 1600x1200
7: 1920x1080
8: 2048x1536
9: 2592x1944
10: 3264x2448
11: 3840x2160
12: 4224x3156
Note: video device may switch to a different frame size,
the one it supports.
-fps:<1-30> Sets camera frame rate. Same is used for MJPEG stream.
Default is 30.
-port: Port number for web server to listen on.
Default is 8000.
-realm: HTTP digest authentication domain.
Default is 'cam2web'.
-htpass: htdigest file containing list of users to access the camera.
Note: only users for the specified/default realm are loaded.
Note: if users file is specified, then by default only users
from that list are allowed to view camera and only
'admin' user is allowed to change its settings.
-viewer: Group of users allowed to view camera: any, user, admin.
Default is 'any' if users file is not specified,
or 'user' otherwise.
-config: Group of users allowed to change camera settings.
Default is 'any' if users file is not specified,
or 'admin' otherwise.
-fcfg: Name of the file to store camera settings in.
Default is '~/.cam_config'.
-web: Name of the folder to serve custom web content.
By default embedded web files are used.
-title: Name of the camera to be shown in WebUI.
Use double quotes if the name contains spaces.
Plug in a webcam to a working USB port on the machine, then check that its showing up properly by running the command below.
ls /dev/video*
/dev/video0 /dev/video1
In most cases you should see two video devices named video0
and video1
. Cam2web will choose video0 by default.
Now you can use the command below to run cam2web with default settings. You will need to run cam2web as root using sudo.
sudo ./cam2web
Web server started on port 8000 ...
Ctrl+C to stop.
You can access the cam2web web interface by using the URL
http://IP.OF.COMPUTER.RUNNING.CAM2WEB:8000
To use cam2web in OctoPrint you need to access it through the API.
The stream URL is
http://IP.OF.COMPUTER.RUNNING.CAM2WEB:8000/camera/mjpeg
The snapshot URL is
http://IP.OF.COMPUTER.RUNNING.CAM2WEB:8000/camera/jpeg
Make sure to replace the first part of the URL with the correct IP or hostname of the computer running cam2web.
Auto Start cam2web
You can use systemd to auto start the cam2web software when the system boots.
First move the executables to a better location, /opt is good location.
The command below will move the cam
directory and its contents into /opt
.
sudo mv ~/cam /opt/
Next create a service file called cam.service
using the command below.
sudo nano /etc/systemd/system/cam.service
Fill that file with the details below.
[Unit]
Description=cam2web auto start script
[Service]
User=root
WorkingDirectory=/opt/cam/
ExecStart=/opt/cam/cam2web
Restart=always
[Install]
WantedBy=multi-user.target
Most of this file should be self explanatory, but more details on the commands and other available commands can be found on the freedesktop.org manual for systemd.
After saving the file, you need to reload the systemd daemon using the command below.
sudo systemctl daemon-reload
Then you can test if the service works by starting it manually using the command below.
sudo systemctl start cam.service
Navigate to the URL you used before to see if the service started.
If it doesn’t work, you can use the command below to see the status of the service.
sudo systemctl status cam.service
To set the service to auto start you need to enable the service using the command below.
sudo systemctl enable cam.service
That’s all! Now you can pull a video or image stream from a USB webcam attached to your Linux machine.