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

Pure Python camera interface

Tue Sep 24, 2013 9:15 pm

EDIT:
Release 1.13 now out:
Original post follows...

----------------------------------------

I've been developing an application for the RPi's camera in Python and the lack of a pure Python interface was beginning to grate a bit, so I've sat down and written one. Well ... started one anyway. It's still thoroughly unfinished but it's vaguely functional and could be useful for some things (basically preview and image capture works, along with some configuration bits, but video capture still needs finishing, along with a load of other bits).

Also, when I say pure Python I mean it doesn't rely on calling raspistill or raspivid, but it does rely on libmmal; specifically I sat down and wrote a header translation of libmmal for Python using ctypes so while the code is technically pure Python, internally it looks like a horrendous mish-mash of C and Python! Still, that doesn't detract from its usability (I'm trying to give it a relatively "pythonic" interface). Using the package (which is rather unimaginatively called picamera) you can do something like the following to display a preview for 10 seconds:

Code: Select all

import time
import picamera

camera = picamera.PiCamera()
try:
    camera.start_preview()
    time.sleep(10)
    camera.stop_preview()
finally:
    camera.close()
Or more simply, using the context manager protocol:

Code: Select all

import time
import picamera

with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(10)
    camera.stop_preview()
You can ramp up the brightness while the preview's running with something like this:

Code: Select all

import time
import picamera

with picamera.PiCamera() as camera:
    camera.start_preview()
    for i in range(100):
        camera.brightness = i
        time.sleep(0.2)
    camera.stop_preview()
Or start the preview and then take an image after it's run a few seconds like this (note that the output doesn't have to be a filename - it can be a file-like object too!) :

Code: Select all

import time
import picamera

with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(2)
    camera.capture('foo.jpg')
    camera.stop_preview()
Anyway, here's some links for code, documentation, and the package. As mentioned it's still very incomplete, but I figured I'd try the "release early, release often" approach for once and see if anyone wants to pitch in! The package works with Python 2.7+ or 3.2+ and I would very much welcome patches, bug reports, pull requests, general suggestions of what you think the API should look like, or other comments.
Last edited by waveform80 on Sat Feb 25, 2017 8:31 pm, edited 16 times in total.

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

Re: Pure Python camera interface

Sun Sep 29, 2013 8:39 pm

Just uploaded release 0.2 of picamera. Video recording now works, and I've expanded the docs with a few more examples. The internal code is a lot cleaner too, although it's still looks horribly like C-ish Python in most places - still, users shouldn't have to worry about that - the API is pretty clean and the examples should demonstrate how easy it is to use.

Have fun - and do feel free to reply here or e-mail with suggestions / comments / fixes / whatever.


Cheers,

Dave.

jupp
Posts: 1
Joined: Tue Sep 24, 2013 2:16 pm
Location: Germany

Re: Pure Python camera interface

Mon Sep 30, 2013 7:26 pm

hey Dave,
your picamera project sounds very promising! However, did you also consider grabbing images for buffering, say at 25fps, and use this in a CV project?

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: Pure Python camera interface

Thu Oct 03, 2013 5:29 pm

I sandbox'd the easy_install, however, when I try the examples, I get the following error.

Traceback (most recent call last):
File "/home/pi/Desktop/Adam/Python Programs/aoh_picamera.py", line 2, in <module>
import picamera
ImportError: No module named picamera

Do I need to have my programs that I am writing or my definitions in a certain directory?

Mind you, I am WAY new to python, and rPi. But I am fascinated by it.
Electronmage

--------------------

I like Pi!

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

Re: Pure Python camera interface

Thu Oct 03, 2013 10:34 pm

jupp wrote:hey Dave,
your picamera project sounds very promising! However, did you also consider grabbing images for buffering, say at 25fps, and use this in a CV project?
Hi Jupp,

Sadly, that's rather beyond the scope of this project - all I'm concerned with at the moment is providing a way for people to do what raspistill and raspivid do, but directly from Python (without resorting to subprocesses), which leads to a few extra capabilities (e.g. manipulating camera properties while previewing/capturing, stream manipulation without having to resort to pipes or temporary files, etc.).

Furthermore, from my investigations so far, I'm not entirely convinced the camera could produce still frames at 25fps. There's a fairly considerable pause when capturing stills - I guesstimate the maximum rate would be 2-4fps. That said, when capturing video the camera's quite capable of providing an H.264 stream at 30fps. If one could decode frames from that fast enough (is the ARM part capable of this? Is there enough bandwidth?) that would do the trick.

Anyway, it's something I might look into in future, but it's not on the cards at the moment - sorry!


Dave.

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

Re: Pure Python camera interface

Thu Oct 03, 2013 10:44 pm

electronmage wrote:I sandbox'd the easy_install, however, when I try the examples, I get the following error.

Traceback (most recent call last):
File "/home/pi/Desktop/Adam/Python Programs/aoh_picamera.py", line 2, in <module>
import picamera
ImportError: No module named picamera

Do I need to have my programs that I am writing or my definitions in a certain directory?

Mind you, I am WAY new to python, and rPi. But I am fascinated by it.
I suspect you're running into the same problem another user ran into earlier this evening - notably that you need to reactivate the virtualenv sandbox in order to use picamera (you need to do this before running python each time you want to use picamera):

Code: Select all

$ source sandbox/bin/activate
$ python
>>> import picamera
That said, it's occurred to me that while virtualenvs are a fairly common thing in Python development, most people playing around with the camera on the Pi aren't Python coders by nature, and probably don't know (or care) about virtualenvs. Furthermore, it's not a platform where mucking around with the system packages is a terribly bad thing (it's just on an SD card after all - one can just fire up a new Raspbian install on another card to do things in a clean environment).

To that end I'll be revising the install docs in the 0.3 release (which is hopefully going to be a little later this evening) to simplify them a bit. There'll be a standard install (no virtualenv, just a straight easy_install which I expect most users will go for, and which should solve all these "can't find picamera" issues), a virtualenv install (for more advanced users that are hacking on several Python projects at once with potentially conflicting deps), and a development install (i.e. the current section for people wishing to develop picamera itself).

Anyway, hope that solves your issue - do let me know if you can't get it working - and I'd strongly recommend upgrading to 0.3 when it's released as well - I've squashed a couple of truly silly bugs in it :)


Dave.

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

Re: Pure Python camera interface

Fri Oct 04, 2013 1:00 am

Release 0.3! Now with tests! Well ... for preview and image capture anyway. Which also means a fair few bugs got squashed with this release ... ahem. There's probably some obvious ones still lurking in video capture - I'll get on with the tests for that this weekend and then spend some time integrating this into an actual application to give it a serious shake-down.

As mentioned in a post above, I've updated the docs with some slightly better installation instructions - comments / criticisms / suggestions always welcome!


Cheers,

Dave.

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: Pure Python camera interface

Fri Oct 04, 2013 10:34 am

I haven't tried downloading, installing and running this yet, but thank you for all your work. All done in a day!! I'll let you know how it goes!

Oh, the homepage link is broken. Just fyi. I would like to read about the picroscopy project.
Electronmage

--------------------

I like Pi!

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

Re: Pure Python camera interface

Fri Oct 04, 2013 10:13 pm

electronmage wrote:I haven't tried downloading, installing and running this yet, but thank you for all your work. All done in a day!! I'll let you know how it goes!
I'd be interested to hear any experiences, good or bad!
electronmage wrote:Oh, the homepage link is broken. Just fyi. I would like to read about the picroscopy project.
At the moment there is no homepage for the picroscopy project because I haven't made an official release of it (I probably shouldn't have bunged a homepage link in the README - planning too far ahead). That said, picamera's now at the point where I can start using it in picroscopy; I'm hoping to find enough time this weekend to get a first release of picroscopy out (which will also entail building some sort of crude website for it). The application itself is reasonably functional at the moment, but integrating picamera into it will add a few features (most notably full resolution preview).

The other bit I need to do some work on is actually mounting a camera on the microscopes in question. However, another user here posted a fascinating thread the other day and it seems he's been working on much the same thing but from the other direction (get the mount working, then figure out the software). He's very kindly agreed to share some designs with me so my hope is that in the relatively near future we might have something quite slick for microscopy with a pi.

I'm also dabbling with some rapid continuous shooting stuff as a) it seems a hot topic and b) while supping on a pint of ale at the pub a few hours ago it occurred to me that I can probably make picamera do something similar to raspistill's timelapse mode with very little tweaking (and a rather nice pythonic interface suggested itself too!)

Anyway, have fun and let me know how things go!

Dave.

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: Pure Python camera interface

Fri Oct 04, 2013 10:29 pm

That's much more adventurous than what I am intending. I want to make a camera that just mounts in my car and will take a picture of the sky every mile that I drive. So, I have to figure out a way to incorporate the camera bit (figuring out how to make a robust camera/computer case - because I haven't found one that has both camera and rPi in it - perhaps just modifying the PiFace case), then tie into the cars odometer. Then I want to map the courses that I drive to work and upload them. Just a sky-map of where I drive.
Electronmage

--------------------

I like Pi!

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

Re: Pure Python camera interface

Sat Oct 05, 2013 1:56 pm

electronmage wrote:then tie into the cars odometer
Not something I've done myself (I don't have a car) but I guess you'll want to look at http://www.raspberrypi.org/phpBB3/searc ... ywords=obd

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

Re: Pure Python camera interface

Fri Oct 11, 2013 1:40 am

And release 0.4 is out -- now with attributes for configuring preview opacity, full-screen, and window position, and a nice new method for rapid(ish) continuous image capture. Haven't had time to add tests for the video recording bits (not sure how I can test the output easily), so there's probably still some bugs lurking around there, but my trivial test recordings turned out fine so do give it a whirl and let me know if you run into any bugs!


Cheers,

Dave.

User avatar
recantha2
Posts: 277
Joined: Wed Nov 14, 2012 9:34 am
Location: Potton, Bedfordshire
Contact: Website

Re: Pure Python camera interface

Fri Oct 11, 2013 6:23 am

Great work, Dave - I've blogged about this and hope to use it when I return to my camera for my next project!
--
Michael Horne - @recantha
Raspberry Pi blog - http://www.recantha.co.uk/blog

Cambridge Raspberry Jam
Website: http://camjam.me
Facebook: https://www.facebook.com/cambridgeraspberryjam
Follow the Cambridge Raspberry Jam on Twitter - @cambridgejam

rasphoto
Posts: 14
Joined: Tue Aug 27, 2013 9:14 pm
Location: Mexico
Contact: Website

Re: Pure Python camera interface

Sat Oct 12, 2013 8:13 pm

HI,
I downloaded the easy install but when I go into python and type import picamera I get the following:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import picamera
ImportError: No module named picamera

Dont know what I am doing wrong.

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

Re: Pure Python camera interface

Sat Oct 12, 2013 8:20 pm

Hi rasphoto - when you say you "downloaded" the easy-install package, do you mean you ran "easy_install --user picamera" on the command line, or just downloaded the .tar.gz file from the homepage? The latter won't do much, while the former should install the package (assuming you've got easy_install available). If you haven't seen them, have a look at the installation instructions at http://picamera.readthedocs.org/en/rele ... stall.html - the User Installation section should do the trick (ignore the other sections). If you've followed those instructions already, my apologies - I'll need a bit more detail to figure out what's going wrong (exactly what commands you used to install the package would be good).


Cheers,

Dave.

rasphoto
Posts: 14
Joined: Tue Aug 27, 2013 9:14 pm
Location: Mexico
Contact: Website

Re: Pure Python camera interface

Sat Oct 12, 2013 8:24 pm

Hi, Sorry, I ran it in the command line and all seemed fine since this is what came back:

Searching for picamera
Reading http://pypi.python.org/simple/picamera/
Best match: picamera 0.4
Downloading https://pypi.python.org/packages/source ... d00b17940a
Processing picamera-0.4.tar.gz
Running picamera-0.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-X0JPjQ/picamera-0.4/egg-dist-tmp-xT4SUh
no previously-included directories found matching 'debian'
no previously-included directories found matching 'docs'
Adding picamera 0.4 to easy-install.pth file

Installed /home/pi/.local/lib/python2.7/site-packages/picamera-0.4-py2.7.egg
Processing dependencies for picamera
Finished processing dependencies for picamera

rasphoto
Posts: 14
Joined: Tue Aug 27, 2013 9:14 pm
Location: Mexico
Contact: Website

Re: Pure Python camera interface

Sat Oct 12, 2013 8:26 pm

the command I ran was:

easy_install --user picamera

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

Re: Pure Python camera interface

Sun Oct 13, 2013 11:07 am

Hmm, that installation looks perfect, but I've just re-read your first message and it looks like you're running under something called "pyshell" - not something I've ever heard of, but I'd hazard a guess that that's the issue. Can you try running "python" and then "import picamera" under that? If that works, at least we'll know whether pyshell really is the issue.

Cheers,

Dave.

rasphoto
Posts: 14
Joined: Tue Aug 27, 2013 9:14 pm
Location: Mexico
Contact: Website

Re: Pure Python camera interface

Mon Oct 14, 2013 12:56 am

Thanks for your reply. Really appreciate it. I will give it a go tomorrow.

StaticDet5
Posts: 21
Joined: Sun Nov 25, 2012 5:11 am

Re: Pure Python camera interface

Tue Oct 15, 2013 3:20 pm

I'm having the same issue.

I installed picamera using the suggested method (easy_install --user picamera) from the command line. I'm then starting a graphics desktop and opening up a terminal window there. I copied the program over, saved it, no problem.

At this point, when I try to run the program from the command line using:
sudo python CamPreview.py

I get:
ImportError: No module named picamera

However, if I run the program from the Python "command line"/shell, it works (My screen displays my ugly mug and messy office for ten seconds).

I'm trying to figure out how to run programs strictly from the command line, as I'm going to be working with several networked Pi's using SSH.

Nice work, by the way. I'm hoping to play with this further and inject textual notes on the bottom of the picture frames using OpenCV or something similar.

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

Re: Pure Python camera interface

Tue Oct 15, 2013 3:40 pm

StaticDet5 wrote:I'm having the same issue.

I installed picamera using the suggested method (easy_install --user picamera) from the command line. I'm then starting a graphics desktop and opening up a terminal window there. I copied the program over, saved it, no problem.

At this point, when I try to run the program from the command line using:
sudo python CamPreview.py

I get:
ImportError: No module named picamera
Ah ha! In this case, I do know what the issue is: sudo. You don't need it. More precisely, the "easy_install --user" command performs the installation for the logged in user specifically, it doesn't alter the system Python installation. I generally encourage this method as it doesn't mean fiddling around with anything the package manager might touch and doesn't require root privileges, however it does mean that the picamera installation will only be accessible to the user you installed it as (presumably "pi" unless you've changed your installation). I should probably add that point to the installation docs.

So, you've got two options: either run "python CamPreview.py" without sudo (i.e. as the ordinary pi user), or if you really want picamera to be accessible to all users (including root), you can do a system-level install with "sudo easy_install picamera".

I should point out that once I get to release 1.0 (hopefully reasonably soon), I'm intending to package this up in a .deb for "proper" system installation (i.e. with apt-get), and I'm not sure that'll play well with a system-level installation via "sudo easy_install" (which is another reason I tend to encourage using "easy_install --user"), but then I suppose raspbian's sufficiently easy to re-install that starting over isn't a big deal (my day job is system/database admin so I tend to be extremely conservative about doing things that might break the package manager at some point down the road!)


Cheers,

Dave.

StaticDet5
Posts: 21
Joined: Sun Nov 25, 2012 5:11 am

Re: Pure Python camera interface

Tue Oct 15, 2013 3:44 pm

That fixed it!

Now I need to go back and figure out why some of my other python programs require me to sudo them....

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

Re: Pure Python camera interface

Tue Oct 15, 2013 4:56 pm

StaticDet5 wrote:That fixed it!

Now I need to go back and figure out why some of my other python programs require me to sudo them....
Sorry if this is teaching you to suck eggs, but just in case:

Generally, if a program requires sudo there's some resource which it requires which isn't available to regular users. If you're playing around with OpenCV, and those other programs are related to OpenCV, I'd hazard a guess that they need access to the /dev/video0 device which I'd also guess isn't generally accessible to the ordinary "pi" user, though I'm not sure about this as I don't have the pi in front of me right now, just an Ubuntu desktop.

Just as an example (and hopefully the fact this is on Ubuntu and not Raspbian won't horribly confuse things), on my Ubuntu desktop machine the /dev/video0 device (a webcam) has the following permissions:

Code: Select all

[email protected]:~$ ls -l /dev/video0 
crw-rw----+ 1 root video 81, 0 Oct 15 16:53 /dev/video0
In other words, it's accessible to root (the first "rw"), and to members of the "video" group (the second "rw"), and no-one else (the rest of the permissions are ----). As it happens, my user is a member of the "video" group, so I get to access the webcam on the machine:

Code: Select all

[email protected]:~$ groups dave
dave : dave adm cdrom sudo dip video plugdev lpadmin sambashare libvirtd
But I could imagine that perhaps the "pi" user doesn't belong to "video" (or whatever group the /dev/video0 device belongs to on Raspbian, assuming a webcam is plugged in; I don't think the picam shows up under /dev). If that were the case there'd be a couple of ways of accessing the device from the "pi" user:
  • Run the program with "sudo", effectively running it as "root". This is the brute force and ignorance method (also known as the "dammit, just work!" method)
  • The slightly more nuanced method is to add the "pi" user to the "video" group with "sudo adduser pi video", then run the program normally without sudo.
The reason this isn't necessary with picamera is that the Pi's camera module seems to be accessible to any old user through the libmmal interface (I'm not actually sure this is a good thing, but at least it keeps things simple and that's probably part of the design philosophy, given the teaching angle).

Other reasons that it can be necessary to run as root:
  • If you want to open a network port <1024 (e.g. port 80 for a web-server) you have to be root
  • If you want to mount or unmount file-systems, you usually need to be root (there are exceptions to this)
  • If you want to access certain files or run certain applications you need to be root (this is basically the same thing as accessing restricted devices above - i.e. just file-system permissions)
  • If you want to hard-link directories (unusual, not recommended) you need to be root
Hope this helps narrow it down a bit!


Cheers,

Dave.

tristan1990
Posts: 38
Joined: Wed May 22, 2013 3:49 pm

Re: Pure Python camera interface

Wed Oct 16, 2013 6:47 pm

Heres the error I'm getting:

Install method

Code: Select all

sudo easy_install --user picamera

Code: Select all

[email protected] ~ $ sudo python camera.py
Traceback (most recent call last):
  File "camera.py", line 2, in <module>
    import picamera
  File "/home/pi/picamera.py", line 4, in <module>
    camera = picamera.PiCamera()
AttributeError: 'module' object has no attribute 'PiCamera'
I think I've tried all the solutions mentioned in this thread :/

Thanks,
Tristan

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Pure Python camera interface

Thu Oct 17, 2013 7:25 am

just came in to say 'well done' to OP :)
note: I may or may not know what I'm talking about...

Return to “Camera board”