Robobot webcam server: Difference between revisions

From Rsewiki
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
Back to [[Robobot_B]]
Back to [[Robobot_B | Robobot]]


== View camera ==
== View camera ==
Line 5: Line 5:
=== Python cam server ===
=== Python cam server ===


Try this https://medium.com/@studymattersinlife/building-a-simple-video-streaming-server-client-in-python-e88d3b9e7158
The source is copied from https://medium.com/@studymattersinlife/building-a-simple-video-streaming-server-client-in-python-e88d3b9e7158


This server is part of the Robobot repository at ''svn/robobot/stream_server'' and should be started at boot-time.
This server is part of the Robobot repository at ''svn/robobot/stream_server'' and should be started at boot-time.
Line 13: Line 13:


  cd /home/local/svn/robobot/stream_server
  cd /home/local/svn/robobot/stream_server
  /usr/bin/python3 stream_server.py &
  /usr/bin/python3 stream_server.py


=== Motion ===
==== Python packages ====


* NO! use Python web server instead
There is a need for some packages that may need to be installed:
* @todo update this page
* When running in a VENV environment


A motion camera ser is installed called "motion", it can detect motion and save video-clip, if motion is detected.
Some system packages:
sudo apt install libcap-dev
sudo apt install libcamera-dev libatlas-base-dev


Motion detection is the primary purpose, but being able to see what the camera sees.
And some Python packages
pip install setproctitle
pip install picamera2


=== Configuration file ===
If running Python in a virtual environment (Venv), additional settings are required.


Edit the configuration file
...


$ sudo nano /etc/motion/motion.conf
I didn't get it to work.
E.g. see https://forums.raspberrypi.com/viewtopic.php?t=361758
# File to write log messages into.  If not defined, stderr and syslog are used.
log_file /home/local/.motion/motion.log
# Target directory for pictures, snapshots and movies
target_dir /home/local/Video/motion
# Video device (e.g. /dev/video0) to be used for capturing.
videodevice /dev/video0
# The port number for the live stream.
stream_port 8081
# Restrict stream connections to the localhost (if 'stream_localhost on').
stream_localhost off
# Image width in pixels.
width 640
# Image height in pixels.
height 480
# Maximum number of frames to be captured per second.
framerate 5


=== run motion ===
Deactivate the VENV, then it worked.
deactivate


To run the streamer, SSH to the robot and start motion
=== Watch result using a browser ===


$ motion
Using the IP of the robot (in this case, robot Coco), the robot camera image can be viewed at this address:
[0:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[0:motion] [NTC] [ALL] motion_startup: Logging to file (/home/local/.motion/motion.log)


Stop by ctrl-C
http://10.197.218.233:7123


Note! Remember to stop the streamer before using the camera for other purposes.
[[file:web-streamer-coco.jpg | 300px]]
There is an alternative if you want both:


Start a loopback feature to create a copy with the same information when the camera is opened.
=== Change configuration ===
Run:
sudo modprobe v4l2loopback


When /dev/video0 is opened a /dev/video1 will be created (with the data determined by the first user).
Change the configuration in the streamer source code:


- not tested much.
/home/local/svn/robobot/stream_server/stream_server.py


=== View the stream ===
The resolution needs to be changed two places!


To view the result, use a browser to the address of the robot and port 8081, e.g. if the robot has IP '10.197.218.248':
In the HTML script at the beginning of the file, and the camera configuration near the end.


10.197.218.248:8081
Note that some configurations reduce the field of view.


You should now see a stream of default 640x480 at 5 FPS as default; more options are available in /etc/motion/motion.conf.
==== HTML file ====


==2024==
Select one of these lines (comment <nowiki><!--img ... --> or uncomment <img... /></nowiki>) as needed


Test a new method from https://pimylifeup.com/raspberry-pi-webcam-server/
Near the start of stream_server.py


Install:
...
<nowiki><body>
<h1>Picamera2 MJPEG Streaming from {}</h1>
<img src="stream.mjpg" width="820" height="616" />
<!--img src="stream.mjpg" width="1296" height="972" -->
<!--img src="stream.mjpg" width="320" height="240" -->
<!--img src="stream.mjpg" width="640" height="480" -->
</body></nowiki>
...


sudo apt install autoconf automake build-essential pkgconf libtool git 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
==== Camera configuration ====


Select the matching line to the settings above:


sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.6.0/$(lsb_release -cs)_motion_4.6.0-1_$(dpkg --print-architecture).deb
Near the end of stream_server.py


sudo dpkg -i $(lsb_release -cs)_motion_4.6.0-1_$(dpkg --print-architecture).deb
...
picam2 = Picamera2()
#picam2.configure(picam2.create_video_configuration(main={"size": (1296, 972)},controls={'FrameDurationLimits': (200000, 200000)}))
picam2.configure(picam2.create_video_configuration(main={"size": (820, 616)},controls={'FrameDurationLimits': (200000, 500000)}))
#picam2.configure(picam2.create_video_configuration(main={"size": (1296, 972)},controls={'FrameDurationLimits': (200000, 500000)}))
#picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)},controls={'FrameDurationLimits': (200000, 500000)}))
#picam2.configure(picam2.create_video_configuration(main={"size": (320, 240)},controls={'FrameDurationLimits': (200000, 500000)}))
...


change the /etc/motion/motion.conf
Reboot or restart to see the effect.


* not much luck; it seems not compatible with libcamera.
==== Restart streaming ====


installed
Restart by first stopping the running version:
sudo apt install libqt5dxcb-plugin


to try
  pkill stream_server
rpicam-vid --qt-preview


But (also) not over ssh -X login
Then restart


A libcamera wrapper: https://github.com/kbarni/LCCV
cd /home/local/svn/robobot/stream_server
With opencv, install
python3 stream_server.py
  libcamera-dev
Prints something like:
[0:41:06.196852273] [12840]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:41:06.226600078] [12841]  INFO RPI vc4.cpp:437 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media1 and ISP device /dev/media3
[0:41:06.226689448] [12841]  INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:41:06.233368984] [12840]  INFO Camera camera.cpp:1033 configuring streams: (0) 320x240-XBGR8888 (1) 640x480-SGBRG10_CSI2P
  [0:41:06.233900943] [12841]  INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA

Latest revision as of 14:23, 29 October 2025

Back to Robobot

View camera

Python cam server

The source is copied from https://medium.com/@studymattersinlife/building-a-simple-video-streaming-server-client-in-python-e88d3b9e7158

This server is part of the Robobot repository at svn/robobot/stream_server and should be started at boot-time. (see ~/on_reboot.bash)

Or try:

cd /home/local/svn/robobot/stream_server
/usr/bin/python3 stream_server.py

Python packages

There is a need for some packages that may need to be installed:

  • When running in a VENV environment

Some system packages:

sudo apt install libcap-dev
sudo apt install libcamera-dev libatlas-base-dev

And some Python packages

pip install setproctitle
pip install picamera2

If running Python in a virtual environment (Venv), additional settings are required.

...

I didn't get it to work. E.g. see https://forums.raspberrypi.com/viewtopic.php?t=361758

Deactivate the VENV, then it worked.

deactivate

Watch result using a browser

Using the IP of the robot (in this case, robot Coco), the robot camera image can be viewed at this address:

http://10.197.218.233:7123

Change configuration

Change the configuration in the streamer source code:

/home/local/svn/robobot/stream_server/stream_server.py

The resolution needs to be changed two places!

In the HTML script at the beginning of the file, and the camera configuration near the end.

Note that some configurations reduce the field of view.

HTML file

Select one of these lines (comment <!--img ... --> or uncomment <img... />) as needed

Near the start of stream_server.py

...
<body>
 <h1>Picamera2 MJPEG Streaming from {}</h1>
 <img src="stream.mjpg" width="820" height="616" />
 <!--img src="stream.mjpg" width="1296" height="972" -->
 <!--img src="stream.mjpg" width="320" height="240" -->
 <!--img src="stream.mjpg" width="640" height="480" -->
 </body>
...

Camera configuration

Select the matching line to the settings above:

Near the end of stream_server.py

...
picam2 = Picamera2()
#picam2.configure(picam2.create_video_configuration(main={"size": (1296, 972)},controls={'FrameDurationLimits': (200000, 200000)}))
picam2.configure(picam2.create_video_configuration(main={"size": (820, 616)},controls={'FrameDurationLimits': (200000, 500000)}))
#picam2.configure(picam2.create_video_configuration(main={"size": (1296, 972)},controls={'FrameDurationLimits': (200000, 500000)}))
#picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)},controls={'FrameDurationLimits': (200000, 500000)}))
#picam2.configure(picam2.create_video_configuration(main={"size": (320, 240)},controls={'FrameDurationLimits': (200000, 500000)}))
...

Reboot or restart to see the effect.

Restart streaming

Restart by first stopping the running version:

 pkill stream_server

Then restart

cd /home/local/svn/robobot/stream_server
python3 stream_server.py

Prints something like:

[0:41:06.196852273] [12840]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:41:06.226600078] [12841]  INFO RPI vc4.cpp:437 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media1 and ISP device /dev/media3
[0:41:06.226689448] [12841]  INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:41:06.233368984] [12840]  INFO Camera camera.cpp:1033 configuring streams: (0) 320x240-XBGR8888 (1) 640x480-SGBRG10_CSI2P
[0:41:06.233900943] [12841]  INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA