Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement libcamerify support for motion binary #2765

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from

Conversation

strips
Copy link

@strips strips commented May 19, 2023

For better support for newer libcam-only cameras on RPI. Please bear with me as I'm not a Python developer.

@strips strips marked this pull request as ready for review May 19, 2023 12:24
@strips
Copy link
Author

strips commented May 21, 2023

Please advice what's the next step here for me or if it's only resting on approval.

@MichaIng
Copy link
Member

Just to be sure: This is really only for Raspberry Pi and software which uses the legacy RPi camera API while the modern libcamera driver/API + KMS is enabled, right? We should make that pretty clear in the config file + add instructions or a link about how to actually install libcamerify. Optionally a page in our wiki + a link to it in the config file.

motioneye/motionctl.py Outdated Show resolved Hide resolved
motioneye/motionctl.py Outdated Show resolved Hide resolved
motioneye/motionctl.py Outdated Show resolved Hide resolved
strips and others added 3 commits May 22, 2023 10:22
@strips
Copy link
Author

strips commented May 22, 2023

Just to be sure: This is really only for Raspberry Pi and software which uses the legacy RPi camera API while the modern libcamera driver/API + KMS is enabled, right? We should make that pretty clear in the config file + add instructions or a link about how to actually install libcamerify. Optionally a page in our wiki + a link to it in the config file.

Yes, this is only for RPI. Any new camera released by Raspberry will only be supported the new libcamera APIs. Motion will probably never support libcamera. That's what the new MotionPlus is for, and it supports libcamera. I'm using it for streaming my 3d printer with a RPI-cam v3 and it's snappier.

As long as MotionEye uses Motion we need to use libcamerify as a backwards compatibility layer. Alternatively there are workarounds like creating a local ip-camera and use it as a source in MotionEye which is pretty cumbersome. The other option is to use the "MOTION_BINARY" config to run a script using the libcamera wrapper but then MotionEye struggles to restart Motion (some PID issue).

@strips
Copy link
Author

strips commented May 22, 2023

@MichaIng I have updated the sample config file with your suggestions. I do not have permission to edit the Wiki.

@joknjokn
Copy link

Any idea when this will be released?

@joknjokn
Copy link

Does anyone have a guide about how to set this up from scratch? I have a Raspberry Pi 4 and one of the new camera modules. I'm a bit surprised that MotionEye doesn't support this - then found this issue around libcamera, but I have no idea how make it work when it's just in a PR (and this PR mentioning the need for manually compiling MotionEye, without instructions about what/how to compile.).

@zagrim
Copy link
Collaborator

zagrim commented Jun 28, 2023

I think this is waiting for someone to test it, and it would be great if you @joknjokn could do that.

I don't remember if there are instructions anywhere on how to compile MotionEye after cloning the repository with git or Github CLI tools, but I think you should be able to use the instructions for running the yet-unreleased dev branch which are given here: https://github.com/motioneye-project/motioneye/tree/dev#installation
You just need to replace the URL of the compressed source code in step 3 with a reference to the files in this PR.
So, the first command in step 3 should be sudo python3 -m pip install 'https://api.github.com/repos/motioneye-project/motioneye/tarball/pull/2765/head'

The rest of the instructions should work as-is as long as you have any possible dependencies needed by libcamerify already installed.

@Schattenruf
Copy link

Schattenruf commented Oct 19, 2023

Hi :)

Is this still worked on? I tried installing from scratch but the errors of motion remain.

Oct 19 15:19:20 raspmatic motion[3087]: [0:motion] [NTC] [ALL] motion_startup: Using log type (ALL) log level (WRN)
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [ERR] [VID] v4l2_fps_set: Error setting fps. Return code -1
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [ERR] [VID] v4l2_mmap_set: Error starting stream. VIDIOC_STREAMON: Invalid argument
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [ERR] [VID] vid_start: V4L2 device failed to open
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [WRN] [ALL] motion_init: Could not fetch initial image from camera
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [WRN] [ALL] motion_init: Motion continues using width and height from config file(s)
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [WRN] [ALL] mlp_retry: Retrying until successful connection with camera
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [ERR] [VID] v4l2_fps_set: Error setting fps. Return code -1
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [ERR] [VID] v4l2_mmap_set: Error starting stream. VIDIOC_STREAMON: Invalid argument
Oct 19 15:19:20 raspmatic motion[3087]: [1:ml1:Camera1] [ERR] [VID] vid_start: V4L2 device failed to open

the interesing part is if I stop motioneye and just call "libcamerify motion" I do not get any errors except for the fps setup:

[0:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[0:motion] [NTC] [ALL] motion_startup: Logging to syslog
[0:motion] [NTC] [ALL] motion_startup: Motion 4.5.1 Started
[0:motion] [NTC] [ALL] motion_startup: Using default log type (ALL)
[0:motion] [NTC] [ALL] motion_startup: Using log type (ALL) log level (NTC)
[0:motion] [NTC] [STR] webu_start_strm: Starting all camera streams on port 8081
[0:motion] [NTC] [STR] webu_strm_ntc: Started camera 0 stream on port 8081
[0:motion] [NTC] [STR] webu_start_ctrl: Starting webcontrol on port 8080
[0:motion] [NTC] [STR] webu_start_ctrl: Started webcontrol on port 8080
[0:motion] [NTC] [ENC] ffmpeg_global_init: ffmpeg libavcodec version 59.37.100 libavformat version 59.27.100
[0:motion] [NTC] [ALL] translate_init: Language: English
[0:motion] [NTC] [ALL] motion_start_thread: Camera ID: 0 is from /etc/motion/motion.conf
[0:motion] [NTC] [ALL] motion_start_thread: Camera ID: 0 Camera Name: (null) Device: /dev/video0
[0:motion] [NTC] [ALL] main: Waiting for threads to finish, pid: 3955
[1:ml1] [NTC] [ALL] motion_init: Camera 0 started: motion detection Enabled
[1:ml1] [NTC] [VID] vid_start: Opening V4L2 device
[1:ml1] [NTC] [VID] v4l2_device_open: Using videodevice /dev/video0 and input -1
[0:05:22.157396992] [3959]  INFO Camera camera_manager.cpp:284 libcamera v0.1.0+81-d477ee97
[0:05:22.199376883] [3960]  WARN RPiSdn sdn.cpp:39 Using legacy SDN tuning - please consider moving SDN inside rpi.denoise
[0:05:22.203133715] [3960]  WARN RPI vc4.cpp:344 Mismatch between Unicam and CamHelper for embedded data usage!
[0:05:22.204070303] [3960]  INFO RPI vc4.cpp:398 Registered camera /base/soc/i2c0mux/i2c@1/imx219@10 to Unicam device /dev/media0 and ISP device /dev/media1
[1:ml1] [NTC] [VID] v4l2_device_capability: - VIDEO_CAPTURE
[1:ml1] [NTC] [VID] v4l2_device_capability: - STREAMING
[1:ml1] [NTC] [VID] v4l2_input_select: Name = "/base/soc/i2c0mux/i2c@1/imx219@"- CAMERA
[1:ml1] [NTC] [VID] v4l2_norm_select: Device does not support specifying PAL/NTSC norm
[1:ml1] [NTC] [VID] v4l2_pixfmt_try: Testing palette YU12 (640x480)
[0:05:22.206136162] [3959]  INFO Camera camera.cpp:1181 configuring streams: (0) 640x480-YUV420
[0:05:22.206635641] [3960]  INFO RPI vc4.cpp:556 Sensor: /base/soc/i2c0mux/i2c@1/imx219@10 - Selected sensor format: 640x480-SBGGR10_1X10 - Selected unicam format: 640x480-pBAA
[1:ml1] [NTC] [VID] v4l2_pixfmt_set: Using palette YU12 (640x480)
[1:ml1] [ERR] [VID] v4l2_fps_set: Error setting fps. Return code -1
[0:05:22.208803222] [3959]  INFO Camera camera.cpp:1181 configuring streams: (0) 640x480-YUV420
[0:05:22.209185369] [3960]  INFO RPI vc4.cpp:556 Sensor: /base/soc/i2c0mux/i2c@1/imx219@10 - Selected sensor format: 640x480-SBGGR10_1X10 - Selected unicam format: 640x480-pBAA
[1:ml1] [NTC] [ALL] image_ring_resize: Resizing pre_capture buffer to 1 items
[1:ml1] [NTC] [ALL] image_ring_resize: Resizing pre_capture buffer to 4 items

But the grey screen with "unable to open video device" is still present...

@zagrim
Copy link
Collaborator

zagrim commented Oct 19, 2023

the interesing part is if I stop motioneye and just call "libcamerify motion" I do not get any errors except for the fps setup:

@Schattenruf I think the reason for that is motion in that case using a different config file than it does when commanded by MotionEye:

[0:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf

With MotionEye, /etc/motioneye/motion.conf is being used, and it refers to probably different camera config files than the one under /etc/motion.

Did you try running code from this PR, or just current dev branch?

@Schattenruf
Copy link

Schattenruf commented Oct 20, 2023

Did you try running code from this PR, or just current dev branch?

This PR to be exact.
If I am back home again I will check the other config file and see what's in there 👍
libcamera-jpg and libcamera-hello works and takes pictures.

I tried to go to the old camera stack because vcgenmod posted "supported = 0 and detected = 0 and libcamera = 0". But after the switch i just got "supported = 1 and detected = 0 and libcamera = 1" but they grey windows stayed :(

Edit: now libcamerify motion does not work as well...

@yaomtc
Copy link

yaomtc commented Nov 1, 2023

OK, so I uninstalled motioneye, used sudo python3 -m pip install 'https://api.github.com/repos/motioneye-project/motioneye/tarball/pull/2765/head, sudo motioneye_init , sudo systemctl enable motioneye, then rebooted. I am still unable to get my Camera Module v3 working. "Local MMAL camera" still says "No cameras", and the V4L option still has a bunch of duplicate options, and at least the first two of each don't work.

motionEye Version | 0.43.0
Motion Version | 4.5.1
OS Version | Raspbian 11

@yaomtc
Copy link

yaomtc commented Nov 1, 2023

Screenshot_20231101_015151

@zagrim
Copy link
Collaborator

zagrim commented Nov 3, 2023

OK, so I uninstalled motioneye, used sudo python3 -m pip install 'https://api.github.com/repos/motioneye-project/motioneye/tarball/pull/2765/head, sudo motioneye_init , sudo systemctl enable motioneye, then rebooted. I am still unable to get my Camera Module v3 working. "Local MMAL camera" still says "No cameras", and the V4L option still has a bunch of duplicate options, and at least the first two of each don't work.

motionEye Version | 0.43.0
Motion Version | 4.5.1
OS Version | Raspbian 11

Oh shoot, I seem to have not read the PR changes fully, since I only now realise that there's still no version of Motion that would work ok with libcamerify 😢 The comment in https://github.com/motioneye-project/motioneye/pull/2765/files#diff-38b4df7bd750714ac86810a7505d371b466d8a5c9b5cdfa51c7454824a48cf62 suggests that recompiling Motion would be the only way to get that part working, which is the basis of the solution this PR proposes.
Here are instructions (based on the instructions in comment here) that I hope would work work on Raspbian, and even though it might take some time, RPi3 should be able to do it (I've done it myself earlier due to that exact issue):

sudo apt-get update
sudo apt-get install autoconf automake pkgconf libtool libjpeg8-dev build-essential libzip-dev gettext libmicrohttpd-dev

git clone https://github.com/Motion-Project/motion.git
cd motion
git checkout 1b3a8262fef5d6d1c249a726957b1aa607f39196

autoreconf -fiv
./configure
make
make install

(1b3a8262fef5d6d1c249a726957b1aa607f39196 is the current HEAD of master, the fix is a bit earlier in b760d0be48c2ebf9fc0ff2b991cd9c80e07ca502 but I chose 1b3a826 just to make sure any possible related hotfixes are included)

Anyway, after recompiling motion, you'd need to add the following line in /etc/motioneye/motioneye.conf:

libcamerify /usr/bin/libcamerify

And while you are editing that file, also change the line starting with log_level to

log_level debug

so that we get better visibility to what is happening.
After that, sudo systemctl restart motioneye and we can see if things work.

I really hope I didn't goof up anything above since I have no such camera to test myself... 😬 😅

@yaomtc
Copy link

yaomtc commented Nov 6, 2023

Package libjpeg8-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or is only available from another source
However the following packages replace it:
libjpeg9-dev libjpeg62-turbo-dev

I went with libjpeg9-dev.

git clone https://github.com/Motion-Project/motion.git

Had to install git (surprised it wasn't already)

$ autoreconf -fiv
autoreconf: Entering directory `.'
autoreconf: running: autopoint --force
Can't exec "autopoint": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 345.
autoreconf: failed to run autopoint: No such file or directory
autoreconf: autopoint is needed because this package uses Gettext

./configure
-bash: ./configure: No such file or directory

I have a configure.ac.

@zagrim
Copy link
Collaborator

zagrim commented Nov 8, 2023

Maybe I should have cross-checked with Motion projects build instructions given the long time that had passed since jasaw's comment... 🤦

There the required packages list is a bit longer (and includes e.g. autopoint that seemed to be the issue based on the error you pasted):

sudo apt-get install autoconf automake autopoint build-essential pkgconf libtool libzip-dev libjpeg-dev git libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev libwebp-dev gettext libmicrohttpd-dev

Other than that, the previous instructions seem to match. So please try to install the rest of the needed packages and start again from autoreconf -fiv

@yaomtc
Copy link

yaomtc commented Nov 8, 2023

Having done this the results are the same, no MMAL camera option, no functional V4L2 camera. Let me know what logs you'd want.

@zagrim
Copy link
Collaborator

zagrim commented Nov 16, 2023

edit: No I didn't goof up the instructions, actually that libcamerify line was just currect based on the modification made in this PR to the example one.

@yaomtc Please post the output of sudo journalctl -u motioneye preferably so that we can see what happens at service restart. And make sure you've also installed libcamera-tools (e.g. /usr/bin/libcamerify exists).

@strips Do you have ideas on debugging this? How are those "libcamerified" cameras supposed to be shown in ME? I supposed they will not show as MMAL even with this, do they, since this doesn't seem to change the code for detecting cameras?

@yaomtc
Copy link

yaomtc commented Nov 17, 2023

@zagrim
Copy link
Collaborator

zagrim commented Nov 18, 2023

@yaomtc the log shows lack of mention of finding libcamerify binary. Please check if you have the following line in /etc/motioneye/motioneye.conf:

libcamerify /usr/bin/libcamerify

Please also check if /usr/bin/libcamerify exists. If not, execute this:

sudo apt-get install libcamera-tools

After installing the package, re-check existence of libcamerify. If it is still not found - or if the package was already installed - please execute which libcamerify and post the output.

@yaomtc
Copy link

yaomtc commented Nov 18, 2023

I have that line at the end of the .conf file, and

$ which libcamerify
/usr/bin/libcamerify

@zagrim
Copy link
Collaborator

zagrim commented Nov 19, 2023

I'm now out of ideas. @strips do you have ideas on why the debug logging (see the gist linked in above comment) isn't showing anything related to libcamerify even though the binary should be available and the config has the libcamerify option defined?
Logically it should be due to some mistake that I just can't see (I wouldn't be surprised if it was in my instructions). yaomtc also earlier indicated that the code of this PR is indeed what was installed. And yet the most obvious potential reasons for the results are a) wrong version of ME code being installed or b) incorrect configuration 🤷

@strips
Copy link
Author

strips commented Nov 19, 2023

I'm now out of ideas. @strips do you have ideas on why the debug logging (see the gist linked in above comment) isn't showing anything related to libcamerify even though the binary should be available and the config has the libcamerify option defined? Logically it should be due to some mistake that I just can't see (I wouldn't be surprised if it was in my instructions). yaomtc also earlier indicated that the code of this PR is indeed what was installed. And yet the most obvious potential reasons for the results are a) wrong version of ME code being installed or b) incorrect configuration 🤷

Sorry for not chipping in but I kind of gave up after my RPI crashed and I have to reinstall. I'm not sure I will even try to use MotionEye again for any new camera that only supports libcamera. MotionEye uses Motion and Motion will never have libcamera-support. Thats why the Motion-guys (I think) are making MotionPlus. MotionEye should be changing over to MotionPlus to support any new cameras on Rasperry Pi. But as this is a part time hobby for most people I don't think it will happen any day soon.

If I had just spent all that time trying to get this to work working extra at work I could have bought my self multiple dedicated standalone wifi-cameras. But not sure if it would be as "fun" :)

Back to your question, Sorry I don't remember all the steps I did in rebuilding.

@zagrim
Copy link
Collaborator

zagrim commented Nov 19, 2023

@strips ok, that's fair enough. I haven't even looked at MotionPlus, but if the interface is not like with Motion (even if it was nicer for integration in code level) then it'll probably be too much work, at least given the current amount of work the current maintainers are able to put in this project. But still, I'd expect the changes in this PR should work, I assume. At least they've worked for you, right? 😃

@strips
Copy link
Author

strips commented Nov 19, 2023

@zagrim It did work but it also crashed sporadically. It could work for a few hours or a few days. I think it could not restart itself after some kind of event like it should. I think it might be something with a PID.

@strips
Copy link
Author

strips commented Nov 19, 2023

@zagrim

@strips Do you have ideas on debugging this? How are those "libcamerified" cameras supposed to be shown in ME? I supposed they will not show as MMAL even with this, do they, since this doesn't seem to change the code for detecting cameras?

I think I tried all the cams in the list and one worked. Teflon memory.

@joknjokn
Copy link

joknjokn commented Mar 23, 2024

Hi,

I never got to try your branch, but I somehow just now managed to get my camera working with MotionEye. I have the Raspberry Pi HQ camera with IMX477 sensor. I've spent roughly 6 hours just doing random things I found in github-threads here and there.

I have this:

  • Raspberry Pi HQ camera with IMX477 sensor
  • Raspberry Pi 4
  • Current Bookworm version (desktop) from today (Raspberry Pi Imager)

This is approximately what I did:

  1. From the motioneye readme (https://github.com/motioneye-project/motioneye )
sudo apt update
sudo apt --no-install-recommends install ca-certificates curl python3 python3-dev libcurl4-openssl-dev gcc libssl-dev

  1. To prevent the externally-managed warning:
grep -q '\[global\]' /etc/pip.conf 2> /dev/null || printf '%b' '[global]\n' | sudo tee -a /etc/pip.conf > /dev/null
sudo sed -i '/^\[global\]/a\break-system-packages=true' /etc/pip.conf

curl -sSfO 'https://bootstrap.pypa.io/get-pip.py'
sudo python3 get-pip.py
rm get-pip.py
  1. Then all of this: Motioneye and Libcamera #2812 (comment)
  • I believe this gave an error about missing motioneye.conf or similar
  1. Then this:
    sudo motioneye_init

  2. I don't really recall what then happened, but it didn't work, and I definitely rebooted the pi.

  3. At some point, I saw new camera models appearing in the motioneye web-interface, and selected the first:
    /base/soc/i2c0mux/i2c@1/imx477@1a (which is /dev/video0)

  4. Still no luck. Checking the motioneye log, it said something like "could not set format: device is busy"-ish

  5. I added this to the bottom of the motioneye.conf (and enabled debug logging) as suggested in this thread:
    libcamerify /usr/bin/libcamerify

  6. I then ran this (not knowing what I'm doing, but I thought: If the cam is busy, and there's both a motion and a motioneye service running, why not try killing motion and see what happens - I'm really not experienced with these commands - note that I actually didn't have sudo at the last two commands for some reason):

sudo systemctl disable motion --now
sudo systemctl stop motion --now
systemctl daemon-reload
systemctl enable motioneye --now
tail /var/log/motioneye/motion.log
  1. And from that log tail - suddenly no errors. After 6 hours.

  2. I went to the web-interface to see if there was video, but no luck... Until I stared at it for maybe 10 seconds, and there it was! Camera streaming! (Low resolution, but that's likely just because I didn't touch any settings related to that yet).

Also:
At some point (somewhere between step 5 and 9), I did this, but I have a gutfeeling it may not have any effect (it didn't work after that at least) - found somewhere in a github thread:

  1. Add this before exit in /etc/rc.local
    sudo -u myusername -g myusername /home/myusername/bin/startup-script
    and the startup-script file:
    cd /home/myusername/ cat /dev/null >/home/myusername/motion/log;libcamerify motion;cat /home/myusername/motion/pid
    (and then likely another reboot)

The output of the log after I saw the camera working in motioneye:

[1:ml1:Camera1] [ERR] [VID] Above message repeats 1 times
[1:ml1:Camera1] [ERR] [VID] v4l2_pixfmt_select: Palette selection failed for format UYVY
[1:ml1:Camera1] [ERR] [VID] v4l2_pixfmt_select: Unable to find a compatible palette format.
[1:ml1:Camera1] [ERR] [VID] vid_start: V4L2 device failed to open
[1:ml1:Camera1] [WRN] [ALL] mlp_retry: Retrying until successful connection with camera
[0:50:46.984078373] [1282]  INFO Camera camera.cpp:1183 configuring streams: (0) 320x240-YUV420
[0:50:46.984949698] [1283]  INFO RPI vc4.cpp:611 Sensor: /base/soc/i2c0mux/i2c@1/imx477@1a - Selected sensor format: 2028x1520-SBGGR12_1X12 - Selected unicam format: 2028x1520-pBCC
[1:ml1:Camera1] [ERR] [VID] v4l2_fps_set: Error setting fps. Return code -1
[0:50:46.987527691] [1282]  INFO Camera camera.cpp:1183 configuring streams: (0) 320x240-YUV420
[0:50:46.988186814] [1283]  INFO RPI vc4.cpp:611 Sensor: /base/soc/i2c0mux/i2c@1/imx477@1a - Selected sensor format: 2028x1520-SBGGR12_1X12 - Selected unicam format: 2028x1520-pBCC

As said - I don't know what I'm doing, but I hope maybe some of you, who know more about this, may be able to figure out how/why it ended up working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Add support for modern libcamera Raspberry Pi camera stack
7 participants