berkowski
Posts: 3
Joined: Tue Aug 13, 2013 8:51 pm

RpiCamera: A Python 3.x package for the RPi Camera module

Tue Aug 13, 2013 9:26 pm

I've put together a quick-and-very-dirty Python package for taking still images with the RPi Camera Module. It's based extensively on the raspistill and raspiyuv programs provided by James Hughes.

This package was put together to test whether I could use the RPi Camera in an instrument I'm building. Sort of a proof-of-concept. As such it's very rough, with all sorts of issues surely lurking. But it does take images, takes them quickly, and provides you with a 1D numpy ndarray object to manipulate to your heart's content!

Quick list of features:
  • Single frame capture up to full sensor resolution in YUV420p or BGR24 formats
  • Multiple frame integration
  • Arbitrary image sizes
  • Access to camera settings such as brightness, saturation, etc.
  • Logging using Python's logging module.
Basically it allows you to do much of what raspiyuv does, but without having to perform system calls or reinitialize the camera each time you want to take an image. You can get some pretty good performance when you only perform that overhead once!

The code is here: https://github.com/berkowski/python-rpicamera
The readme has some examples. You really should read it if you decide to try the package.

I run Arch on my RPi where the camera libraries are installed to /opt/vc/lib. If it's different on raspian, just edit the setup.py file to point to the correct location.

Unfortunately I don't think I'll be using the RPi camera module for this instrument. I'd like to be able to get at some setting which don't seem exposed through the MMAL API, and it's too hard to get at the strobe lines on the sensor connector. I'll also be losing access to the RPi camera module I've been testing against shortly, so bug fixes might be an issue if anyone actually uses my code. I'll be attempting to clean it up over the next week though.

deso
Posts: 3
Joined: Wed Aug 14, 2013 12:07 pm

Re: RpiCamera: A Python 3.x package for the RPi Camera modu

Wed Aug 14, 2013 4:28 pm

I installed python-dev python-setuptools and got userland libs,
but:

Code: Select all

pi@pi ~/py/python-rpicamera $ python setup.py build
running build
running build_ext
building 'RpiCamera' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I./externals/rpi-userland -I./externals/rpi-userland/interface/vcos -I./externals/rpi-userland/interface/vcos/pthreads -I./externals/rpi-userland/interface/mmal -I./externals/rpi-userland/interface/vmcs_host/linux -I/usr/include/python2.7 -c RpiCamera/RpiCamera.c -o build/temp.linux-armv6l-2.7/RpiCamera/RpiCamera.o
In file included from ./externals/rpi-userland/interface/vcos/vcos.h:116:0,
                 from ./externals/rpi-userland/interface/mmal/mmal_common.h:40,
                 from ./externals/rpi-userland/interface/mmal/mmal.h:363,
                 from ./externals/rpi-userland/interface/mmal/util/mmal_util_params.h:31,
                 from RpiCamera/RpiCamera.c:36:
./externals/rpi-userland/interface/vcos/pthreads/vcos_platform.h: In function ‘vcos_event_signal’:
./externals/rpi-userland/interface/vcos/pthreads/vcos_platform.h:525:8: warning: variable ‘ok’ set but not used [-Wunused-but-set-variable]
In file included from /usr/include/python2.7/Python.h:8:0,
                 from RpiCamera/RpiCamera.h:35,
                 from RpiCamera/RpiCamera.c:42:
/usr/include/python2.7/pyconfig.h: At top level:
/usr/include/python2.7/pyconfig.h:1161:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
/usr/include/features.h:214:0: note: this is the location of the previous definition
In file included from ./externals/rpi-userland/interface/vmcs_host/vc_dispmanx.h:37:0,
                 from ./externals/rpi-userland/host_applications/linux/libs/bcm_host/include/bcm_host.h:46,
                 from RpiCamera/RpiCamera.h:41,
                 from RpiCamera/RpiCamera.c:42:
./externals/rpi-userland/interface/vchi/vchi.h:342:40: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
In file included from RpiCamera/RpiCamera.c:42:0:
RpiCamera/RpiCamera.h:207:1: error: unknown type name ‘PyModuleDef’
RpiCamera/RpiCamera.h:208:5: error: ‘PyModuleDef_HEAD_INIT’ undeclared here (not in a function)
RpiCamera/RpiCamera.h:209:5: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:209:5: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h:210:5: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:210:5: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h:211:5: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:211:5: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:212:5: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h:213:1: warning: excess elements in scalar initializer [enabled by default]
RpiCamera/RpiCamera.h:213:1: warning: (near initialization for ‘RpiCameraModule’) [enabled by default]
RpiCamera/RpiCamera.h: In function ‘PyInit_RpiCamera’:
RpiCamera/RpiCamera.h:222:9: warning: ‘return’ with a value, in function returning void [enabled by default]
RpiCamera/RpiCamera.h:233:5: warning: implicit declaration of function ‘PyModule_Create’ [-Wimplicit-function-declaration]
RpiCamera/RpiCamera.h:233:19: warning: initialization makes pointer from integer without a cast [enabled by default]
RpiCamera/RpiCamera.h:236:9: warning: ‘return’ with a value, in function returning void [enabled by default]
RpiCamera/RpiCamera.h:252:5: warning: ‘return’ with a value, in function returning void [enabled by default]
RpiCamera/RpiCamera.c: At top level:
RpiCamera/RpiCamera.h:106:1: warning: ‘switch_output__doc__’ defined but not used [-Wunused-variable]
error: command 'gcc' failed with exit status 1

berkowski
Posts: 3
Joined: Tue Aug 13, 2013 8:51 pm

Re: RpiCamera: A Python 3.x package for the RPi Camera modu

Wed Aug 14, 2013 5:56 pm

Code: Select all

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I./externals/rpi-userland -I./externals/rpi-userland/interface/vcos -I./externals/rpi-userland/interface/vcos/pthreads -I./externals/rpi-userland/interface/mmal -I./externals/rpi-userland/interface/vmcs_host/linux -I/usr/include/python2.7 -c RpiCamera/RpiCamera.c -o build/temp.linux-armv6l-2.7/RpiCamera/RpiCamera.o
.
.
.
RpiCamera/RpiCamera.h:207:1: error: unknown type name ‘PyModuleDef’
RpiCamera/RpiCamera.h:208:5: error: ‘PyModuleDef_HEAD_INIT’ undeclared here (not in a function)
Looks like you're building against Python 2.7 (the -I/usr/include/python2.7). I've only tested this against Python 3.3, and looking back through the docs now I see that modules are setup slightly differently between the two versions.

Guess that answers the question about working with Python 2.7, sorry. But I did post this as a Python 3.x package...

Return to “Python”