Ketta
Posts: 9
Joined: Mon Feb 10, 2014 2:18 pm

Re: Pure Python camera interface

Fri Aug 22, 2014 4:57 pm

Hello all, and especially Dave.

I've been busy at class through the summer so I'm just now back and catching up with the development of the Pi and Picamera. I see that there is some rudimentary development now into simple cases of motion detection with the camera and this library?

Can anyone point me to some resource that I can use to start to understand how this kind of motion detection analysis works? One of the few applications I am exploring would be made very simple if I could work with Picamera to get the basics figured out. I tried to work with OpenCV on the board briefly a few months ago and was beating my head against the wall since it isn't exactly supported well on RPi.

Basically I would love how to do something like detect motion across a certain pixel threshold or something. I'm just a 2nd year student at Uni so I've got a lot of stuff to learn, but it sure is easier when you are genuinely interested. (And may potentially get paid for it through your Co-Op opportunities.) So any barebones reading that would help me get started and understand things like "blob detection" and whatever other terms would be greatly appreciated.

But thanks again for all your hard work Dave. I'm excited to get back on track with this stuff, through the end of the year at least.

User avatar
waveform80
Posts: 308
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK

Re: Pure Python camera interface

Fri Aug 22, 2014 6:37 pm

Ketta wrote:Hello all, and especially Dave.

I've been busy at class through the summer so I'm just now back and catching up with the development of the Pi and Picamera. I see that there is some rudimentary development now into simple cases of motion detection with the camera and this library?

Can anyone point me to some resource that I can use to start to understand how this kind of motion detection analysis works? One of the few applications I am exploring would be made very simple if I could work with Picamera to get the basics figured out. I tried to work with OpenCV on the board briefly a few months ago and was beating my head against the wall since it isn't exactly supported well on RPi.

Basically I would love how to do something like detect motion across a certain pixel threshold or something. I'm just a 2nd year student at Uni so I've got a lot of stuff to learn, but it sure is easier when you are genuinely interested. (And may potentially get paid for it through your Co-Op opportunities.) So any barebones reading that would help me get started and understand things like "blob detection" and whatever other terms would be greatly appreciated.

But thanks again for all your hard work Dave. I'm excited to get back on track with this stuff, through the end of the year at least.
Some very rudimentary pointers can be found in the docs for the PiMotionAnalysis class. Unfortunately I haven't had time to have a serious play with this stuff myself (I would like to add a small chapter on motion detection to the docs eventually). Another thing to take a look at is OpenCV's accumulateWeighted function, and scipy's label function.

As you can probably gather from those pointers there's several ways to go about doing motion detection. Using the inline motion vectors (i.e. the stuff from the picamera docs) is by far the most efficient, but probably the least accurate method. After all, it's based on the H.264 encoder which isn't concerned with accurately detecting motion, but in accurately reconstructing the appearance of a scene based on motion (a subtle difference, but important).

The OpenCV algorithms are probably more accurate (if used properly) but OpenCV feels more like a research tool which (understandably) isn't tuned to minimal platforms like the Pi. When using it, set your expectations realistically for the platform (low framerates / resolutions in order to obtain reasonable performance).

Anyway, hopefully there's a few useful bits in those links!


Dave.

User avatar
jbeale
Posts: 3513
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Fri Aug 22, 2014 6:40 pm

Many people are using the "motion" application for a security camera use case. It is written in C but the algorithm is straight-forward, and you can capture the basic idea in one page of python along with the picamera library, you don't really need much more. The idea is:

1) Build up an "average background image" which is just the average value of each pixel in the image, over some number of frames.
2) For each new frame coming from the camera, find the pixels that are different from their average value by some "threshold" amount.
3) If more than some number of pixels are found in step 2, call that "motion detected". Record image and/or sound alarm, etc.

Then there are various improvements, like requiring motion detected for some number of frames before it's valid, or requiring some area-density of changed pixels, rather than just total count (which would needs more computations, OpenCV might help there).

Notice that this algorithm does not detect motion, it detects changes of brightness. Might be an actual object moving, but turning on a light bulb, or a shadow appearing as the sun comes out from behind clouds, would also be counted.

Really detecting motion requires you to first recognize an object, and then compare its location from frame to frame. Object recognition is not easy. You can work with motion vectors from the H.264 encoder, but these don't always do what you want. H.264 motion vectors are designed for image compression in general and not object recognition specifically. Meaning, if the encoder decides it can save bits by saying "this block of pixels in the last frame is like those pixels in the next frame" it will do so and generate a motion vector, regardless if the block belongs to the same "moving object" by human visual standards, or the fixed background just has similar patterns in two places, and there's a little bit of noise in the camera sensor, or the lighting changed slightly.

Ketta
Posts: 9
Joined: Mon Feb 10, 2014 2:18 pm

Re: Pure Python camera interface

Fri Aug 22, 2014 7:12 pm

Alright, thanks guys. I'll read through the links and work on my comprehension.

As an aside note, I get an error when trying to use this new "annotate_text" function. Says

Code: Select all

picamera.exc.PiCameraMMALError: Failed to set annotation status: Function not implemented
I have checked a few times that my install of python3-picamera is up to date, which it is.

As for the code, I just tried to add an annotation in to the "crude" motion detecting sample in the documentation:

Code: Select all

import numpy as np
import picamera
import picamera.array

class DetectMotion(picamera.array.PiMotionAnalysis):
    def analyse(self, a):
        a = np.sqrt(
            np.square(a['x'].astype(np.float)) +
            np.square(a['y'].astype(np.float))
            ).clip(0, 255).astype(np.uint8)
        # If there're more than 10 vectors with a magnitude greater
        # than 60, then say we've detected motion
        if (a > 60).sum() > 10:
            print('Motion detected!')

with picamera.PiCamera() as camera:
    with DetectMotion(camera) as output:
        camera.resolution = (640, 480)
        camera.start_preview()
        camera.annotate_text = 'Detecting Motion'
        camera.start_recording(
              '/dev/null', format='h264', motion_output=output)
        camera.wait_recording(30)
        camera.stop_recording()
        camera.stop_preview()

I'll put on a simpler script and see what my results are there I suppose. Perhaps I am using the function improperly.

User avatar
waveform80
Posts: 308
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK

Re: Pure Python camera interface

Fri Aug 22, 2014 7:25 pm

Ketta wrote:Alright, thanks guys. I'll read through the links and work on my comprehension.

As an aside note, I get an error when trying to use this new "annotate_text" function. Says

Code: Select all

picamera.exc.PiCameraMMALError: Failed to set annotation status: Function not implemented
I have checked a few times that my install of python3-picamera is up to date, which it is.
Yup, but your firmware isn't. Run "sudo rpi-update" and reboot afterwards (annotations required a new function to be exported from the firmware so anything older than a few weeks won't cut it).


Dave.

fs007
Posts: 14
Joined: Mon Aug 18, 2014 6:34 am

Re: Pure Python camera interface

Sun Aug 24, 2014 7:24 am

Since I have upgraded to the 3.12.26+ i have the following problem with capturing an image through PiCamera:
I set shutter speed to 1s (and framerate to 1/1).
Capturing one image needs more than 5 seconds (during that time led is on).
Is there any way to shorten that time ?
I can't find anything similar to raspistill -t parameter in the PiCamera API.

User avatar
waveform80
Posts: 308
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK

Re: Pure Python camera interface

Sun Aug 24, 2014 10:06 am

fs007 wrote:Since I have upgraded to the 3.12.26+ i have the following problem with capturing an image through PiCamera:
I set shutter speed to 1s (and framerate to 1/1).
Capturing one image needs more than 5 seconds (during that time led is on).
Is there any way to shorten that time ?
I can't find anything similar to raspistill -t parameter in the PiCamera API.
Sounds about right; the camera always uses a couple of frames to measure things like white balance and exposure (even if you're setting them manually). Given that picamera and raspistill just use MMAL to talk to the camera you should see very similar times there (there's not much picamera does different that raspistill doesn't when simply shooting images from the still port).

For example, on my development Pi:

Code: Select all

[email protected] ~ $ time raspistill -t 1000 -ss 1000000 -o foo.jpg

real    0m5.117s
user    0m0.030s
sys     0m0.030s
So, when shutter speed is set to 1 second, raspistill takes about 5 seconds to capture an image too, even with a timeout of 1s set.


Dave.

fs007
Posts: 14
Joined: Mon Aug 18, 2014 6:34 am

Re: Pure Python camera interface

Sun Aug 24, 2014 2:13 pm

So, when shutter speed is set to 1 second, raspistill takes about 5 seconds to capture an image too, even with a timeout of 1s set
.

Thank you Dave for that confirmation, but my question was: Is there a way to shorten this time ? In raspistill there is the -t parameter; do you know anything like that in PiCamera ?

User avatar
waveform80
Posts: 308
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK

Re: Pure Python camera interface

Sun Aug 24, 2014 3:50 pm

fs007 wrote:
So, when shutter speed is set to 1 second, raspistill takes about 5 seconds to capture an image too, even with a timeout of 1s set
.

Thank you Dave for that confirmation, but my question was: Is there a way to shorten this time ? In raspistill there is the -t parameter; do you know anything like that in PiCamera ?
The -t parameter in raspistill simply controls the delay prior to capture; the same thing in picamera is simply to use time.sleep:

Code: Select all

import time
import picamera

with picamera.PiCamera() as camera:
    camera.framerate = 0
    camera.shutter_speed = 1000000 # -ss 1000000
    time.sleep(1) # -t 1000
    camera.capture('foo.jpg') # -o foo.jpg
Obviously one could remove any delay prior to shooting to speed things up, but that generally leads to bad selections for AWB (best to let the camera measure things for a bit). The other thing you could try to speed things up is capturing from the video port. This doesn't involve a mode switch so it tends to be a good deal faster than still port captures. However there're various caveats with this (e.g. no exif, no bayer data, etc.) so it'll depend on your use case whether this is an option.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Pure Python camera interface

Sun Aug 24, 2014 4:58 pm

bantammenace2012 wrote:Secondly/alternatively any pointers on where to look for guidance on how to set up the buttons on the PiFace Control and Display to change Picamera parameters and then to take photos/ videos i.e. In the field I want to pick from options rather than type in the python commands. This would be great when the camera is being handheld but on ocassions I will want/need to be remote from the RPi.
This might have some code you could adapt: http://www.raspberrypi.org/touchscreen- ... -adafruit/

And / or http://www.piface.org.uk/guides/how_to_ ... napcamera/

BerryPicker
Posts: 177
Joined: Tue Oct 16, 2012 3:03 pm
Location: The East of England

Re: Pure Python camera interface

Mon Aug 25, 2014 4:37 pm

On trying the picamera advanced recipe '5.7. Recording motion vector data' found at
http://picamera.readthedocs.org/en/rele ... ipes2.html
a file is made but the motion data extracted from that file cannot be fitted into a three-dimensional numpy array.
The line
motion_data = motion_data.reshape((frames, rows, cols))
throws
ValueError: total size of new array must be unchanged

I think this may be due to the recorder not filing data only in complete frame quanta.

This problem exists for both versions of python. Help in resolving it would be welcome.

skypi
Posts: 111
Joined: Sat Aug 09, 2014 11:48 pm

Re: Pure Python camera interface

Tue Aug 26, 2014 8:56 pm

Excellent documentation, Excellent package. Thanks.

Docs in a pdf file would be cool.

edit: Doing an rpi-update fixed it!

just testing reading the awb_gains on a B+ and it failed.

File "picamera_longexposure.py", line 16, in <module>
g = camera.awb_gains
[...]
picamera.exc.PiCameraMMALError: Failed to get camera settings: Function not implemented

User avatar
waveform80
Posts: 308
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK

Re: Pure Python camera interface

Tue Aug 26, 2014 10:00 pm

skypi wrote:Excellent documentation, Excellent package. Thanks.

Docs in a pdf file would be cool.

just testing reading the awb_gains on a B+ and it failed.

File "picamera_longexposure.py", line 16, in <module>
g = camera.awb_gains
[...]
picamera.exc.PiCameraMMALError: Failed to get camera settings: Function not implemented
The only reason I can think that reading awb_gains would fail is if you've got an old firmware (awb_gains used to be a write-only property, but this was changed a couple of months ago if I recall correctly). On PDF docs, just click on the "ReadTheDocs" bit at the bottom left of the docs and you can access PDF and Epub versions of the docs (the PDF rendering isn't perfect, particularly tables, but that's mostly down to tables being ridiculously hard to construct properly in LaTeX, so it's the best conversion Sphinx/ReadTheDocs can manage).

Dave.

skypi
Posts: 111
Joined: Sat Aug 09, 2014 11:48 pm

Re: Pure Python camera interface

Wed Aug 27, 2014 12:45 pm

yeah, worked OK after an rpi-update, which also fixed it so it now does the 6s exposure... waiting for a nice moonlit night now then! :)

Thats cool then, the docs site allows a pdf download, thanks!

do you know anything about the pi firmware/hardware face recognition and learning stuff? question I have is, is it better than opencv, which is very-good up to a point.

bigtabs
Posts: 2
Joined: Thu Aug 28, 2014 11:50 am

Re: Pure Python camera interface

Thu Aug 28, 2014 11:54 am

After updating RPI I no longer get the error message. Instead the pi completely hangs, forcing a power cycle. When removing the line of code with the annotate_text call everything works again.

Can anyone shed any light?

User avatar
jbeale
Posts: 3513
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Thu Aug 28, 2014 5:01 pm

When you say "updating" did you do apt-get update, apt-get upgrade and also rpi-update? In which order?

All I can say is that after doing those three things in that order, annotations are working for me.

bigtabs
Posts: 2
Joined: Thu Aug 28, 2014 11:50 am

Re: Pure Python camera interface

Thu Aug 28, 2014 7:50 pm

Thanks a lot jbeale. I had neglected the upgrade step.

sportsnapper
Posts: 69
Joined: Wed Sep 05, 2012 11:27 am

Re: Pure Python camera interface

Tue Sep 02, 2014 11:42 am

Just for info - I couldn't get the pdf doc download to work using the links provided by clicking the pdf or epub links at the bottom left. I had to click the downloads link below "On read the Docs", then all the downloads for all the versions are available.

Looking forward to getting this working in the next couple of days as I have a very real application for two time-lapse cameras this weekend! Trying to decide if writing to a USB hard drive or a small (pi based?) server is the best option.

sportsnapper
Posts: 69
Joined: Wed Sep 05, 2012 11:27 am

Re: Pure Python camera interface

Tue Sep 02, 2014 11:59 pm

I've run into a few problems with picamera this evening, and I've found where my issues are. The problem is basically I'm now getting blank frames, about 41kb instead of nice 1.5mb images.

I've got a B+, and it's running Picamera 1.7. I've done the update/upgrade routine, and updated firmware using rpi-update - it's now
Linux picam1 3.12.26+ #707 PREEMPT Sat Aug 30 17:39:19 BST 2014
The camera's working, a basic raspistill command produces an image.

If I execute a preview (first example in quick start), it produces a preview image.

Running the simple time-lapse sequence example (4.7) works fine.

Adding the extra code to create consistent images from example 4.6 means that I then start to get blank images. Even running the example in 4.6 gives blank images.

If I remove the lines

Code: Select all

camera.shutter_speed = camera.exposure_speed
    camera.exposure_mode = 'off'
then the 4.6 example works.
If I leave the shutter speed lines in and remove the awb lines, it fails. So it looks to me like there is a problem in the camera.exposure_speed/shutter_speed part.

so this cobbled together test:

Code: Select all

import time
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (1920,1080)
    camera.ISO = 800
    camera.framerate = 30
    time.sleep(2)
# Now fix the values
#    camera.shutter_speed = camera.exposure_speed
#    camera.exposure_mode = 'off'
    g = camera.awb_gains
    camera.awb_mode = 'off'
    camera.awb_gains = g
    camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2014 GAreth Jones Sports-alive'
    camera.start_preview()
    for filename in camera.capture_continuous('image{timestamp:%Y-%m-%d-%H-%M-%S-%f}.jpg'):
        print('Captured %s' % filename)
        time.sleep(15) # wait
works, albeit without the consistent shutter speed.

I can live without this, as I think I'll have reasonably consistent lighting, but it's been a bit frustrating getting here. Is this my install (which is fresh from NOOBS today), or a glitch in the code??

skypi
Posts: 111
Joined: Sat Aug 09, 2014 11:48 pm

Re: Pure Python camera interface

Wed Sep 03, 2014 8:05 pm

would be good to be able to get full manual mode of the camera, I'd like to know how to get that first bright shot at "1250 ISO" which picamera says is impossible to set, it has 800 max ISO

I think the foundation should publish and stick to how the parameters interact.

User avatar
jbeale
Posts: 3513
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Wed Sep 03, 2014 11:29 pm

Unless it's changed recently, raspistill shared that limitation, that it could only go up to ISO 800 by direct setting. You can actually get the higher ISO 1250 in practice, but only by setting "sports" exposure mode, which also favors higher shutter speeds and is incompatible with direct setting of shutter speed.

At least, that is how matters stood some months ago when I was experimenting with this, maybe recent firmware changes have affected this.

skypi
Posts: 111
Joined: Sat Aug 09, 2014 11:48 pm

Re: Pure Python camera interface

Thu Sep 04, 2014 8:59 pm

jbeale wrote:Unless it's changed recently, raspistill shared that limitation, that it could only go up to ISO 800 by direct setting. You can actually get the higher ISO 1250 in practice, but only by setting "sports" exposure mode, which also favors higher shutter speeds and is incompatible with direct setting of shutter speed.

At least, that is how matters stood some months ago when I was experimenting with this, maybe recent firmware changes have affected this.
yeah, its change change change... (and a bit of hope eh!) just done a git clone of raspicam as on another thread it was noted he had fixed something, now I'm not even getting the 1250 mode with the same python script... and much brighter exposure.... where I was getting a great green earlier that let starlight through cloud.... no more...

User avatar
waveform80
Posts: 308
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK

Re: Pure Python camera interface

Sat Sep 06, 2014 12:57 am

And picamera 1.8 shuffles into the limelight! Highlights this time are:
  • Several bug fixes, most notably for the old-firmware issues that 1.7 introduced (the mmal_queue_timedwait bug).
  • Arbitrary preview overlays (useful for designing interfaces?)
  • Better annotations including backgrounds and a frame counter (thanks to the tireless efforts of the firmware devs!)
  • Very tentative support for stereoscopic cameras on the compute module (this is almost certainly wrong, but I can't test it - so feedback is very welcome! See the new stereo_* parameters on the PiCamera initializer)
  • Configurable image effect parameters
  • Plenty of documentation enhancements, including a new chapter on dealing with deprecated functionality
Bug reports, suggestions, comments welcome as always; don't forget the Raspbian package will take a few days to hit the repo but the PyPI (pip) version is available now. Enjoy!

Dave.

User avatar
waveform80
Posts: 308
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK

Re: Pure Python camera interface

Mon Sep 08, 2014 8:00 pm

sportsnapper wrote:Just for info - I couldn't get the pdf doc download to work using the links provided by clicking the pdf or epub links at the bottom left. I had to click the downloads link below "On read the Docs", then all the downloads for all the versions are available.

Looking forward to getting this working in the next couple of days as I have a very real application for two time-lapse cameras this weekend! Trying to decide if writing to a USB hard drive or a small (pi based?) server is the best option.
Looks like the PDF link being broken was a fault on RTD - seems to be fixed now (although I haven't tested whether older versions of the docs need rebuilding to fix it - I'll try and remember to do that!).

On the subject of where to write to: in practice I generally find dumping data over USB (which includes any networking given that all goes via USB on the Pi) better than writing to the SD card. The SD card's bandwidth seems pretty limited (whether that's the interface or SD cards in general I don't know), but USB seems to have more bandwidth available. Still, the fact you're dealing with timelapse implies that you don't need much bandwidth anyway (presumably you'll only be writing single images with longs gaps between them) so the choice becomes rather moot.


Dave.

bernard0833
Posts: 1
Joined: Tue Sep 09, 2014 6:49 am

Re: Pure Python camera interface

Tue Sep 09, 2014 7:19 am

Hi !

awesome library ;)

I'm trying to use pistreaming and motion detection together.
Motion recording is on default splitter_port 1 and I set pistreaming to splitter_port 3.

Code: Select all

stream = picamera.PiCameraCircularIO(camera, seconds=10, bitrate=2000000)
camera.start_recording(stream, format='h264', motion_output=detect_output)

camera.start_recording(output, 'yuv', splitter_port=3)

if motion_detected
   camera.split_recording(the_file)
   camera.wait_recording(10)
   camera.split_recording(stream)
But when motion detected I get :

Code: Select all

Traceback (most recent call last):
  File "server.py", line 320, in <module>
    main()
  File "server.py", line 273, in main
    camera.split_recording(the_file)
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1145, in split_recording
    encoder.split(output, options.get('motion_output'))
  File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 1088, in split
    'Timed out waiting for a split point')
picamera.exc.PiCameraRuntimeError: Timed out waiting for a split point
any ideas what is wrong ?

bernard

Return to “Camera board”