Difference between revisions of "Raspberry PI WebCam"

From BITPlan Wiki
Jump to navigation Jump to search
 
(38 intermediate revisions by the same user not shown)
Line 1: Line 1:
* https://www.linux-projects.org/uv4l/installation/
+
* {{:PlayChessWithAWebCam/Links}}
 +
* {{:Self_Driving_RC_Car/Links}}
 +
 
 
= Webcam =
 
= Webcam =
<source lang='bash'  highlight="1-2">
+
This page deals mostly with USB devices as input. The software tested can also handle the built in Raspberry Cam. See the two comment chat groups above for the projects in which a webcam are used.
 +
Another usecase is {{Link|target=OctoPrint}}
 +
== check camera and video device ==
 +
The tests were done with two different Logitech USB Webcams on different Raspberry PI devices. A Raspberry 3B+ and a Raspberry 4 using raspbian buster.
 +
 
 +
<source lang='bash' highlight='1'>
 +
lsusb | grep cam
 +
Bus 001 Device 005: ID 046d:0825 Logitech, Inc. Webcam C270
 +
</source>
 +
<source lang='bash'  highlight="1,3">
 
lsusb
 
lsusb
 
Bus 001 Device 004: ID 046d:082c Logitech, Inc. HD Webcam C615
 
Bus 001 Device 004: ID 046d:082c Logitech, Inc. HD Webcam C615
 +
dmesg | grep uvcvideo | head -1
 +
[    4.538065] uvcvideo: Found UVC 1.00 device HD Webcam C615 (046d:082c)
 +
</source>
 +
 +
<source lang='bash' highlight='1'>
 +
v4l2-ctl -V
 +
Format Video Capture:
 +
Width/Height      : 352/288
 +
Pixel Format      : 'MJPG' (Motion-JPEG)
 +
Field            : None
 +
Bytes per Line    : 0
 +
Size Image        : 102400
 +
Colorspace        : sRGB
 +
Transfer Function : Default (maps to sRGB)
 +
YCbCr/HSV Encoding: Default (maps to ITU-R 601)
 +
Quantization      : Default (maps to Full Range)
 +
Flags            :
 
</source>
 
</source>
 +
 
= Software =
 
= Software =
 
== qv4l2 Videograbbing ==
 
== qv4l2 Videograbbing ==
Line 16: Line 45:
  
 
=== Installation ===
 
=== Installation ===
<source lang='bash'>
+
<source lang='bash' highlight='1,2'>
 
sudo apt-get install fswebcam
 
sudo apt-get install fswebcam
 +
fswebcam test.jpg
 +
--- Opening /dev/video0...
 +
Trying source module v4l2...
 +
/dev/video0 opened.
 +
No input was specified, using the first.
 +
Adjusting resolution from 384x288 to 352x288.
 +
--- Capturing frame...
 +
Captured frame in 0.00 seconds.
 +
--- Processing captured image...
 +
Writing JPEG image to 'test.jpg'.
 
</source>
 
</source>
  
Line 32: Line 71:
 
</source>
 
</source>
 
[[File:chess37846.jpg|400px]]
 
[[File:chess37846.jpg|400px]]
 +
  
 
== motion ==
 
== motion ==
<source lang='bash' highlight='1,3,4,16-19,21' lines>
+
Motion can be used to stream the camera images over the network.
lsusb | grep cam
+
Motion is used for detecting movements and reacting on those movements. This needs extra CPU power so the solution is slower than others.
Bus 001 Device 005: ID 046d:0825 Logitech, Inc. Webcam C270
+
<source lang='bash' highlight='1-8'>
 
sudo apt-get install motion
 
sudo apt-get install motion
v4l2-ctl -V
 
Format Video Capture:
 
Width/Height      : 352/288
 
Pixel Format      : 'MJPG' (Motion-JPEG)
 
Field            : None
 
Bytes per Line    : 0
 
Size Image        : 102400
 
Colorspace        : sRGB
 
Transfer Function : Default (maps to sRGB)
 
YCbCr/HSV Encoding: Default (maps to ITU-R 601)
 
Quantization      : Default (maps to Full Range)
 
Flags            :
 
 
cd /etc/motion
 
cd /etc/motion
 
git init
 
git init
Line 55: Line 83:
 
git commit -a -m "initial checkin"
 
git commit -a -m "initial checkin"
 
# edit motion.conf with your favorite editor
 
# edit motion.conf with your favorite editor
git diff motion.conf  
+
git diff motion.conf  
 
diff --git a/motion.conf b/motion.conf
 
diff --git a/motion.conf b/motion.conf
 
index 23c8810..b96df83 100644
 
index 23c8810..b96df83 100644
Line 102: Line 130:
 
</source>
 
</source>
  
= Uv4l trial on Buster =
+
== MjpegStream ==
 +
see https://github.com/jacksonliam/mjpg-streamer
 +
=== Install ===
 +
<source lang='bash' highlight='1-7'>
 +
cd /usr/local/lib
 +
sudo -s
 +
git clone https://github.com/jacksonliam/mjpg-streamer
 +
apt-get install -y cmake libjpeg8-dev
 +
cd mjpg-streamer/mjpg-streamer-experimental
 +
make
 +
make install
 +
</source>
 +
=== Run ===
 +
<source lang='bash' highlight='1'>
 +
./mjpg_streamer -i input_uvc.so -o 'output_http.so -w www'
 +
</source>
 +
=== Test ===
 +
You need to modify the localhost ip to the ip of the raspberry
 +
http://127.0.0.1:8080/?action=stream
 +
 
 +
=== Service ===
 +
==== Run script ====
 +
Prepare a script to run the mjpegstreamer with the wanted configuration
 +
<source lang='bash' highlight='1'>
 +
cat /usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run
 +
#!/bin/bash
 +
# Start Webcam streaming
 +
# WF 2020-01-24
 +
scriptdir=$(dirname $0)
 +
echo $scriptdir
 +
cd $scriptdir
 +
./mjpg_streamer -i input_uvc.so -o 'output_http.so -w www'
 +
</source>
 +
make script executable
 +
<source lang='bash' highlight='1'>
 +
chmod +x /usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run
 +
</source>
 +
 
 +
==== Service Unit ====
 +
<source lang='bash' highlight='1'>
 +
root@pi4car:/etc/systemd/system# cat webcam.service
 +
[Unit]
 +
Description=Webcam Streaming
 +
 
 +
[Service]
 +
Type=simple
 +
ExecStart=/usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run
 +
StandardOutput=syslog
 +
StandardError=syslog
 +
SyslogIdentifier=webcam
 +
Restart=on-abnormal
 +
LimitNOFILE=65536
 +
 
 +
[Install]
 +
WantedBy=multi-user.target
 +
</source>
 +
==== Activate Service ====
 +
<source lang='bash' highlight='1-3'>
 +
sudo systemctl enable webcam.service
 +
service webcam start
 +
service webcam status
 +
● webcam.service - Webcam Streaming
 +
  Loaded: loaded (/etc/systemd/system/webcam.service; enabled; vendor preset: enabled)
 +
  Active: active (running) since Fri 2020-01-24 07:42:33 CET; 4s ago
 +
Main PID: 2396 (run)
 +
    Tasks: 4 (limit: 4915)
 +
  Memory: 780.0K
 +
  CGroup: /system.slice/webcam.service
 +
          ├─2396 /bin/bash /usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run
 +
          └─2398 ./mjpg_streamer -i input_uvc.so -o output_http.so -w www
 +
 
 +
Jan 24 07:42:34 pi4car webcam[2396]:  o: HTTP Listen Address..: (null)
 +
Jan 24 07:42:34 pi4car webcam[2396]:  o: username:password....: disabled
 +
Jan 24 07:42:34 pi4car webcam[2396]:  o: commands.............: enabled
 +
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: www-folder-path......: www/
 +
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: HTTP TCP port........: 8080
 +
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: HTTP Listen Address..: (null)
 +
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: username:password....: disabled
 +
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: commands.............: enabled
 +
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: starting input plugin input_uvc.so
 +
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: starting output plugin: output_http.so (ID: 00)
 +
</source>
 +
=== Potential Issues ===
 +
<pre> uvcvideo: Failed to resubmit video URB (-19).</pre>
 +
* https://dev.archive.openwrt.org/ticket/19399
 +
 
 +
== Uv4l trial on Buster ==
 +
With Uv4l your mileage may vary. We had mixed results. While on raspberry stretch things ran with not much of an issue we had hours of fiddling on buster devices and could not get
 +
all cameras to work properly.
 +
* https://www.linux-projects.org/uv4l/installation/
 +
 
 
As of 2019-10-15 there is no official Buster support for uv4l yet see e.g.
 
As of 2019-10-15 there is no official Buster support for uv4l yet see e.g.
 
https://www.raspberrypi.org/forums/viewtopic.php?t=247305
 
https://www.raspberrypi.org/forums/viewtopic.php?t=247305
Line 117: Line 235:
 
# sudo rpi-update
 
# sudo rpi-update
 
sudo apt-get install uv4l-server uv4l-uvc uv4l-xscreen uv4l-mjpegstream uv4l-dummy uv4l-raspidisp
 
sudo apt-get install uv4l-server uv4l-uvc uv4l-xscreen uv4l-mjpegstream uv4l-dummy uv4l-raspidisp
sudo apt-get install uv4l-webrtc
+
sudo apt-get install uv4l-webrtc  
 +
# for raspi-cam (we are using webcam!)
 +
# apt-get install uv4l-raspicam-extras
 +
</source>
 +
=== Configure ===
 +
<source lang='bash'>
 +
/etc/uv4l # git init
 +
Initialized empty Git repository in /etc/uv4l/.git/
 +
/etc/uv4l# git add *
 +
/etc/uv4l# git commit -a -m "initial state"
 +
[master (root-commit) 0e2dc87] initial state
 +
2 files changed, 555 insertions(+)
 +
create mode 100644 uv4l-raspicam.conf
 +
create mode 100644 uv4l-uvc.conf
 +
git diff uv4l-uvc.conf
 +
diff --git a/uv4l-uvc.conf b/uv4l-uvc.conf
 +
index c01824e..f00b6b6 100644
 +
--- a/uv4l-uvc.conf
 +
+++ b/uv4l-uvc.conf
 +
@@ -54,8 +54,7 @@ frame-buffers = 4
 +
 +
# device-id = <vid>:<pid>
 +
# alternative to 'device-path', vendor and product identifiers of the USB device
 +
-
 +
-
 +
+device-id = 046d:0825
 +
#################################
 +
# streaming server options
 +
#################################
 +
 
 
</source>
 
</source>
=== Check Webcam being identified ===
+
 
<source lang='bash' highlight="1,6">
+
=== System start script ===
dmesg | grep video
+
==== uv4l.service unit ====
[   4.691736] videodev: Linux video capture interface: v2.00
+
<source lang='bash'>
[   4.838925] bcm2835-codec bcm2835-codec: Device registered as /dev/video10
+
root@picarford:/etc/systemd/system# cat uv4l.service
[    4.844913] bcm2835-codec bcm2835-codec: Device registered as /dev/video11
+
[Unit]
[    4.852532] bcm2835-codec bcm2835-codec: Device registered as /dev/video12
+
Description=UV4L Webcam
[    4.989574] uvcvideo: Found UVC 1.00 device HD Webcam C615 (046d:082c)
+
 
[    5.028207] uvcvideo 1-1.2:1.2: Entity type for entity Extension 5 was not initialized!
+
[Service]
[   5.028226] uvcvideo 1-1.2:1.2: Entity type for entity Processing 2 was not initialized!
+
Type=simple
[    5.028238] uvcvideo 1-1.2:1.2: Entity type for entity Camera 1 was not initialized!
+
ExecStart=/usr/bin/uv4l -k --sched-rr --mem-lock --config-file=/etc/uv4l/uv4l-uvc.conf --driver uvc --driver-config-file=/etc/uv4l/uv4l-uvc.conf --server-option=--editable-config-file=/etc/uv4l/uv4l-uvc.conf --device=046d:0825
[    5.028250] uvcvideo 1-1.2:1.2: Entity type for entity Extension 6 was not initialized!
+
StandardOutput=syslog
[    5.028261] uvcvideo 1-1.2:1.2: Entity type for entity Extension 7 was not initialized!
+
StandardError=syslog
[    5.028273] uvcvideo 1-1.2:1.2: Entity type for entity Extension 8 was not initialized!
+
SyslogIdentifier=uv4l
[    5.028968] usbcore: registered new interface driver uvcvideo
+
Restart=on-abnormal
 +
LimitNOFILE=65536
 +
 
 +
[Install]
 +
WantedBy=multi-user.target
 +
</source>
 +
 
 +
==== enable and start ====
 +
<source lang='bash'>
 +
sudo systemctl enable uv4l.service
 +
sudo systemctl start uv4l.service
 
</source>
 
</source>
  
== U4v4l with OpenCV ==
+
=== U4v4l with OpenCV ===
 
* http://www.linux-projects.org/uv4l/tutorials/opencv/
 
* http://www.linux-projects.org/uv4l/tutorials/opencv/
  

Latest revision as of 13:30, 24 January 2020

see PlayChessWithAWebCam

see Self Driving RC Car

Webcam

This page deals mostly with USB devices as input. The software tested can also handle the built in Raspberry Cam. See the two comment chat groups above for the projects in which a webcam are used. Another usecase is OctoPrint

check camera and video device

The tests were done with two different Logitech USB Webcams on different Raspberry PI devices. A Raspberry 3B+ and a Raspberry 4 using raspbian buster.

lsusb | grep cam
Bus 001 Device 005: ID 046d:0825 Logitech, Inc. Webcam C270
lsusb
Bus 001 Device 004: ID 046d:082c Logitech, Inc. HD Webcam C615
dmesg | grep uvcvideo | head -1
[    4.538065] uvcvideo: Found UVC 1.00 device HD Webcam C615 (046d:082c)
v4l2-ctl -V
Format Video Capture:
	Width/Height      : 352/288
	Pixel Format      : 'MJPG' (Motion-JPEG)
	Field             : None
	Bytes per Line    : 0
	Size Image        : 102400
	Colorspace        : sRGB
	Transfer Function : Default (maps to sRGB)
	YCbCr/HSV Encoding: Default (maps to ITU-R 601)
	Quantization      : Default (maps to Full Range)
	Flags             :

Software

qv4l2 Videograbbing

see https://wiki.ubuntuusers.de/Videograbbing/

sudo apt-get install qv4l2
qv4l2

Chesspic2019-10-15 1514.png

fswebcam

Installation

sudo apt-get install fswebcam
fswebcam test.jpg
--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 384x288 to 352x288.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'test.jpg'.

Script to take pictures (remotely)

#!/bin/bash
server=pichess
pic=chess$$.jpg
resolution=640x480
# https://raspberrypi.stackexchange.com/a/29284/11220 - skip some frames to adjust exposure
ssh $server fswebcam -r $resolution -S 10 -d /dev/video0 /tmp/$pic
scp $server:/tmp/$pic /tmp/$pic
open /tmp/$pic

Chess37846.jpg


motion

Motion can be used to stream the camera images over the network. Motion is used for detecting movements and reacting on those movements. This needs extra CPU power so the solution is slower than others.

sudo apt-get install motion
cd /etc/motion
git init
git add *
git commit -a -m "initial checkin"
# edit motion.conf with your favorite editor
git diff motion.conf 
diff --git a/motion.conf b/motion.conf
index 23c8810..b96df83 100644
--- a/motion.conf
+++ b/motion.conf
@@ -8,7 +8,7 @@
 ############################################################
 
 # Start in daemon (background) mode and release terminal (default: off)
-daemon off
+daemon on
 
 # File to store the process ID, also called pid file. (default: not defined)
 process_id_file /var/run/motion/motion.pid
@@ -76,14 +76,14 @@ rotate 0
 flip_axis none
 
 # Image width (pixels). Valid range: Camera dependent, default: 320
-width 320
+width 352
 
 # Image height (pixels). Valid range: Camera dependent, default: 240
-height 240
+height 288
 
 # Maximum number of frames to be captured per second.
 # Valid range: 2-100. Default: 100 (almost no limit).
-framerate 2
+framerate 30
 
 # Minimum time in seconds between capturing picture frames from the camera.
 # Default: 0 = disabled - the capture rate is given by the camera framerate.
@@ -458,7 +458,7 @@ stream_motion off
 stream_maxrate 1
 
 # Restrict stream connections to localhost only (default: on)
-stream_localhost on
+stream_localhost off
 
 # Limits the number of images per connection (default: 0 = unlimited)
 # Number can be defined by multiplying actual stream rate by desired number of seconds
# fix /etc/default/motion
cat /etc/default/motion 
# set to 'yes' to enable the motion daemon
start_motion_daemon=yes

MjpegStream

see https://github.com/jacksonliam/mjpg-streamer

Install

cd /usr/local/lib
sudo -s
git clone https://github.com/jacksonliam/mjpg-streamer
apt-get install -y cmake libjpeg8-dev
cd mjpg-streamer/mjpg-streamer-experimental
make
make install

Run

./mjpg_streamer -i input_uvc.so -o 'output_http.so -w www'

Test

You need to modify the localhost ip to the ip of the raspberry http://127.0.0.1:8080/?action=stream

Service

Run script

Prepare a script to run the mjpegstreamer with the wanted configuration

cat /usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run
#!/bin/bash
# Start Webcam streaming
# WF 2020-01-24
scriptdir=$(dirname $0)
echo $scriptdir
cd $scriptdir
./mjpg_streamer -i input_uvc.so -o 'output_http.so -w www'

make script executable

chmod +x /usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run

Service Unit

root@pi4car:/etc/systemd/system# cat webcam.service 
[Unit]
Description=Webcam Streaming

[Service]
Type=simple
ExecStart=/usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=webcam
Restart=on-abnormal
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

Activate Service

sudo systemctl enable webcam.service
service webcam start
service webcam status
● webcam.service - Webcam Streaming
   Loaded: loaded (/etc/systemd/system/webcam.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-01-24 07:42:33 CET; 4s ago
 Main PID: 2396 (run)
    Tasks: 4 (limit: 4915)
   Memory: 780.0K
   CGroup: /system.slice/webcam.service
           ├─2396 /bin/bash /usr/local/lib/mjpg-streamer/mjpg-streamer-experimental/run
           └─2398 ./mjpg_streamer -i input_uvc.so -o output_http.so -w www

Jan 24 07:42:34 pi4car webcam[2396]:  o: HTTP Listen Address..: (null)
Jan 24 07:42:34 pi4car webcam[2396]:  o: username:password....: disabled
Jan 24 07:42:34 pi4car webcam[2396]:  o: commands.............: enabled
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: www-folder-path......: www/
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: HTTP TCP port........: 8080
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: HTTP Listen Address..: (null)
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: username:password....: disabled
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: commands.............: enabled
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: starting input plugin input_uvc.so
Jan 24 07:42:34 pi4car mjpg_streamer[2398]: MJPG-streamer [2398]: starting output plugin: output_http.so (ID: 00)

Potential Issues

 uvcvideo: Failed to resubmit video URB (-19).

Uv4l trial on Buster

With Uv4l your mileage may vary. We had mixed results. While on raspberry stretch things ran with not much of an issue we had hours of fiddling on buster devices and could not get all cameras to work properly.

As of 2019-10-15 there is no official Buster support for uv4l yet see e.g. https://www.raspberrypi.org/forums/viewtopic.php?t=247305

As of 2020-01-21 there seems to be a work-around see https://blog.domski.pl/uv4l-on-raspberry-pi-running-debian-buster/

We will try using the commands for Raspberry Stretch

curl http://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add -
echo "deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/stretch stretch main" >>  /etc/apt/sources.list
sudo apt-get update
sudo apt-get install uv4l
# do we need to update the firmware?
# sudo rpi-update
sudo apt-get install uv4l-server uv4l-uvc uv4l-xscreen uv4l-mjpegstream uv4l-dummy uv4l-raspidisp
sudo apt-get install uv4l-webrtc 
# for raspi-cam (we are using webcam!)
# apt-get install uv4l-raspicam-extras

Configure

/etc/uv4l # git init
Initialized empty Git repository in /etc/uv4l/.git/
/etc/uv4l# git add *
/etc/uv4l# git commit -a -m "initial state"
[master (root-commit) 0e2dc87] initial state
 2 files changed, 555 insertions(+)
 create mode 100644 uv4l-raspicam.conf
 create mode 100644 uv4l-uvc.conf
git diff uv4l-uvc.conf 
diff --git a/uv4l-uvc.conf b/uv4l-uvc.conf
index c01824e..f00b6b6 100644
--- a/uv4l-uvc.conf
+++ b/uv4l-uvc.conf
@@ -54,8 +54,7 @@ frame-buffers = 4
 
 # device-id = <vid>:<pid>
 # alternative to 'device-path', vendor and product identifiers of the USB device
-
-
+device-id = 046d:0825
 #################################
 # streaming server options
 #################################

System start script

uv4l.service unit

root@picarford:/etc/systemd/system# cat uv4l.service 
[Unit]
Description=UV4L Webcam 

[Service]
Type=simple
ExecStart=/usr/bin/uv4l -k --sched-rr --mem-lock --config-file=/etc/uv4l/uv4l-uvc.conf --driver uvc --driver-config-file=/etc/uv4l/uv4l-uvc.conf --server-option=--editable-config-file=/etc/uv4l/uv4l-uvc.conf --device=046d:0825
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=uv4l
Restart=on-abnormal
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

enable and start

sudo systemctl enable uv4l.service
sudo systemctl start uv4l.service

U4v4l with OpenCV

Links