Coder Social home page Coder Social logo

Comments (4)

waveform80 avatar waveform80 commented on April 29, 2024

I should add some bits to the advanced recipes section in the docs for this as I suspect it's going to be a common question! In the meantime, here's a quick couple of (untested and incomplete!) examples in a gist covering the topic: https://gist.github.com/waveform80/8496879

If you can let me know if there's any errors in the code posted (other than the obvious omission of an actual motion detection algorithm!), I'd be most grateful. Then I can tidy them up as much as possible and add them to the docs.

from picamera.

mjmare avatar mjmare commented on April 29, 2024

Hi Dave

I slightly modified your example: https://gist.github.com/mjmare/8503306

With the original example and with the modified one I get the following error at random moments. I may get one or more video files before the error occurs.

ssh://[email protected]:22/usr/bin/python -u /home/pi/picam/testcircular.py
Motion detected
No motion
No motion
No motion
No motion
Traceback (most recent call last):
File "/home/pi/picam/testcircular.py", line 50, in
main(camera)
File "/home/pi/picam/testcircular.py", line 36, in main
if detect_motion(camera):
File "/home/pi/picam/testcircular.py", line 6, in detect_motion
camera.capture(stream, format='jpeg', use_video_port=True)
File "/usr/local/lib/python2.7/dist-packages/picamera/camera.py", line 859, in capture
if not encoder.wait(30):
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 326, in wait
raise self.exception
picamera.exc.PiCameraError: Unable to return a buffer to the encoder port: Argument is invalid

Process finished with exit code 1

As a side note: currently the example records 10 video when motion is detected. It then stops for a second and starts looking for motion. This won’t fly in more realistic motion detection scenarios.
In the simplest case one needs 2 images to detect motion. But now the previous image is 10 secs old. One could of course get two new images but that makes the video interruption even longer (if motion is still present).
Even worse: if you want to implement more robust motion detection techniques like segmentation one needs dozens of images (to filter out background fluctuations).
So what is needed is to continuously detect motion by capturing and processing images. If motion is detected: start video capture (up to N seconds). If motion is again detected while recording video: keep recording video. If not motion is detected , stop recording after M seconds after last motion.
The reason for distinct, but contiguous video segments is to keep file size manageable and to enable parallel uploads to a server.

I’m telling you all this, not for you to solve my problems but to hopefully give you a better understanding of my particular use case.

Thanks again

Marcel

On 18 Jan 2014, at 22:39, Dave Jones [email protected] wrote:

I should add some bits to the advanced recipes section in the docs for this as I suspect it's going to be a common question! In the meantime, here's a quick couple of (untested and incomplete!) examples in a gist covering the topic: https://gist.github.com/waveform80/8496879

If you can let me know if there's any errors in the code posted (other than the obvious omission of an actual motion detection algorithm!), I'd be most grateful. Then I can tidy them up as much as possible and add them to the docs.

β€”
Reply to this email directly or view it on GitHub.

from picamera.

waveform80 avatar waveform80 commented on April 29, 2024

The exception you're encountering looks like #40 - I'm currently working on a fix for that (I think I've found the root cause; it's an odd one as I think it's been there all along, but something in 1.0's timing changed to cause the race condition to manifest).

Anyway, to the examples: the first example is deliberately simplistic (altering it to work with two pictures should be trivial, but working around the issue with continual recording while detecting motion isn't due to the limited size of the circular buffer). The second example is easier to manipulate to cover your needs:

  1. Watch for motion
  2. When motion is detected, split recording to disk (the "after" file)
  3. While recording to disk, write the prior 10 seconds from the circular buffer to disk as well (the "before" file), and wipe the circular buffer
  4. Continue recording to disk until motion is not detected for, say, 10 seconds
  5. Once this is satisfied, split recording back to the circular memory buffer

The existing example already does all of this, except for point 4 - which is a relatively trivial change. I've updated the original gist to cover this now (I also renamed the files in the gist as I'm intending one to become the example for circular recording in the "basic recipes" section, and the other to become the example in the "advanced recipes" section). The advanced example also demonstrates a crude method for capturing two images (though actual comparison is still left as an exercise for the reader).

from picamera.

waveform80 avatar waveform80 commented on April 29, 2024

Marking this as an enhancement issue for the docs - integrate the gists for 1.1

from picamera.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.