Message Boards Message Boards

14
|
115189 Views
|
22 Replies
|
23 Total Likes
View groups...
Share
Share this post:

Snapping pictures with the Wolfram Language on the Raspberry Pi

Posted 11 years ago
This post shows how to use the standard Raspberry Pi camera with the Wolfram Language on a Raspberry Pi.

To recreate this experiment you will need the following hardware (in addition to the Raspberry Pi itself):

First configure your Raspberry Pi Camera Board following the instructions from the Raspberry Pi Camera web page.

From the desktop double click the 'Mathematica' icon or type 'mathematica' in a terminal to launch the notebook interface.

To take pictures, simply run the DeviceRead command:

DeviceRead["RaspiCam"]


POSTED BY: Arnoud Buzing
22 Replies

I'm trying this from the command line because I'm running my Pi headless. I've ssh'ed into it from my Mac and I'm running wolfram in a terminal. When I try 'DeviceRead["RaspiCam"], I get the following:

mmal: Function not implemented mmal: Function not implemented mmal: Function not implemented mmal: Could not set stereo mode : error 3 mmal: main: Failed to create camera component mmal: Failed to run camera app. Please check for firmware updates

Empty input file

Import::fmterr: Cannot import data as JPEG format.

Out[4]= $Failed

I've used python scripts that have streamed the camera to a web browser, so I know it works. Any ideas?

POSTED BY: Rod Schmidt

Hi Rod, I was wondering if you could give me a little bit more info about the issue you're having. I'm trying to replicate it so I can help you figure out what is going on.

  1. Which model Raspberry Pi are you using?
  2. What kernel are you running?
  3. You mentioned being able to use a Python script to use the camera successfully. Does the raspistill command work as well?
  4. Can you use your Python script successfully, then immediately get an error from Mathematica (ie, no rebooting or anything in between)?
  5. Are you using any GPIO devices on your Pi at the same time as your camera?

Thanks, Brett

POSTED BY: Brett Haines

The recent release of Data Drop has added new ways to interact with the camera module. Here is how to make a time lapse.

DatabinAdd Snaphot

POSTED BY: Bernat Espigulé
Posted 10 years ago
Phew! Thanks very much for posting this, I was on the brink of removing Mathematica from my raspi.

Hopefully this change will be reflected in a future update, the cam is a great toy to play with in Mathematica and such hiccoughs are likely to deter folks from getting into the swing of things.
POSTED BY: serge
Posted 10 years ago
I got an advice from someone anonymous to modify below file to make this DeviceRead to work.

/opt/Wolfram/WolframEngine/10.0/SystemFiles/Devices/DeviceDrivers/RaspiCam.m

The point is to change " -t 0 -o -" to " -t 1 -o -".

So this is something to do with delay?
Although there's a lag for obvious reason, DeviceRead works now.

I am using Linux Berry02 3.10.29+ #638 PREEMPT Wed Feb 12 20:16:25 GMT 2014 armv6l GNU/Linux.
POSTED BY: Sabotenboy L
It would be nice if support were added for external cameras, possibly through PTP, as part of the device connectivty effort.

Example application: I once wanted to control a DSLR using Mathematica to record a time lapse during sunset. (Why Mathematica?  Because it's a high level, easy to use language that I know well.)  Finally I gave up because (at that time) it would have been too much work to connect the device to Mathematica.  The point of controlling the camera through the computer was twofold: it didn't have a built-in intervalometer, and it didn't quite get the metering completely right throughout the changing lighting conditions.  I was planning to continuously vary the settings according to a pre-recorded profile from the previous day, and not use the camera metering at all.  To be fair, this kind of control is not really needed with a newer and higher quality camera that has a built-in intervalometer and more reliable metering.
POSTED BY: Szabolcs Horvát
Posted 9 years ago

I've just started and discovered how to run external commands:

<<"!command"

This means I can use fswebcam to capture an image which can then be imported. A work around where there's no webcam driver.

Best regards, Colin

POSTED BY: Colin Durrans
Nearly same for me. I have purchased my PI some days ago. I have installed the system using Noops. Without the updates I was able to get an image using DeviceRead. Then I have installed all the updates provided. Now DeviceRead didn't work. It's simply hanging. So I think there is an incompatibility with Mathematica an the new PI updates.
POSTED BY: Andre Koppel
I am also having the same experience as Bob L - the DeviceRead[ "Raspicam" ] hangs, but the Import from shell works as expected, taking ~0.5 secs.

I am running RPi MMA 10.0 for Linux ARM (32-bit) (Nov 19,2013). I am using latest NOOB with updates.
POSTED BY: Rick Gaitskell
I can confirm Ronda's experience - my camera hangs on
DeviceRead["Raspicam"]

but functions properly with
Import[ "!raspistill -n -w 600 -h 400 -t 1 -o -", "JPG"]
POSTED BY: BoB LeSuer
Thanks for the response, Lambert, but it wasn't a typo. I tried it because that was the name returned by $ImagingDevices and was the $DefaultImagingDevice. Out here in the near perfect  vacuum of documentation space, we have no way to know whether something is "supported" or not except to try it and see whether we can understand the response.

The only sensible way I've found so far to get an RPi camera image is to use Import as Arnoud suggested earlier in this thread and wrap it in a function that exposes the optional parameters of raspistill such as:

 (* 2014-01-17 Klingener
 AFAIC, this is CC0, but Wolfram has restrictions on use of RPiMma.
 Mathematica function to obtain an image from the Raspberry Pi camera *)
 
 Options[currentRPiImage] =
   {Width -> 640 (* pixels *)
    , Height -> 480 (* pixels default chip aspect ratio is 4:3 *)
    , Rotation -> 180 (* degrees, increments of 90 *)
    , Contrast -> 0    (* -100 to 100, default is 0 *)
   , Brightness -> 50 (* 0 to 100, default is 50 *)
   , Saturation -> 0 (* -100 to 100, default is 0 *)
   , ISO -> 100 (* 100 to 800, default is 100 *)};

currentRPiImage[opts : OptionsPattern[]] :=
Import[
  "!raspistill" <>
   " -n" <> (* no preview *)
   " -e jpg" <> (* jpg is the hardware-supported default *)
   " -t 0" <> (* no delay *)
   " -o -" <> (* output to stdout *)
   " -rot " <> ToString[OptionValue[Rotation]] <>
   " -w " <> ToString[OptionValue[Width]] <>
   " -h " <> ToString[OptionValue[Height]] <>
   " -co " <> ToString[OptionValue[Contrast]] <>
   " -br " <> ToString[OptionValue[Brightness]] <>
   " -sa " <> ToString[OptionValue[Saturation]] <>
   " -ISO " <> ToString[OptionValue[ISO]]
  , "JPG"]
In all of this, the most awkward feature is that there are so many ways to fail ugly - if the camera hangs, there's no way to recover except to reset the power on the board, and DeviceRead, as a blocking function, inherently fails ugly - hanging beyond <command .> and sometimes beyond kernel kill. There ought to be a way to first validate the camera state before requesting the image and second, to be able to soft reset it when it does hang. It would seem that the camera validation would be required first, because a hanging DeviceRead would prevent any way to DeviceWrite a reset to the camera (if indeed such a thing exists.) I suppose both of these could be done on the RPi in Python in a more graceful way and avoid the blocking of the Mma front end.

I haven't looked at whether Import or DeviceRead fail more gracefully than the other.

I've encountered two flavors of camera hang. One in which there's some kind of software loop, where the LED blinks on and off with a period of about a second (this one times out after a period of some minutes I haven't actually measured) and another flavor where the LED comes on and stays on, a condition requiring a power off reset.

Any ideas?

BTW, I don't know how or when I became "Updating Name."

Fred Klingener
POSTED BY: Fred Klingener
Your DeviceRead evaluation threw a message because there is a typo in the name.  It should be:
DeviceRead["RaspiCam"]

DeviceRead, not CurrentImage/ImageCapture is the supported way to read from the RaspiCam.
Happy coding!
POSTED BY: Lambert Chao
Posted 11 years ago
I'm experimenting with RPi's camera, running the RPi's kernel remotely via a notebook in Mathematica on a Macbook Pro. 

I've found the following oddity:
In[2]:= $ImagingDevices
Out[2]= {"Raspicam"}
In[3]:= DeviceRead["Raspicam"]
During evaluation of In[3]:= DeviceRead::ncdevx: There is no open device in the Raspicam class to perform this operation.
Out[3]= $Failed
but
DeviceRead["RaspiCam"]
works as intended.
Further:
In[7]:= $DefaultImagingDevice
Out[7]= "Raspicam"
In[8]:= CurrentImage[]
produces a camera image in a way indistinguishable from that produced by DeviceRead["RaspiCam"].

And apparently ImageCapture[] isn't related to any of this, producing on the one hand a nonopt message if either "Raspicam" or "RaspiCam" is specified as a parameter, and on the other, in the case of the default ImageCapture[], a hanging camera, cycling its LED on and off endlessly with a period of about a second. I haven't timed it, but the blinking condition will reset after some minutes.

If I run DeviceRead["RaspiCam"] while the camera LED is cycling, the command hangs in Mathematica and the RPi camera LED lights and stays on. Evidently, the only way out is to reset the power on the RPi and kill its kernel in the Mathematica notebook.

The only way out I've found is to cycle the RPi power and restart its kernel once the RPi has rebooted.
Hth,
Fred Klingener
POSTED BY: Updating Name
Kay,

Another way is to use ImportString like so:

image = Import[ "!raspistill -n -w 600 -h 400 -t 0 -o -", "JPG"];


The exclamation (!) before 'raspistill' tells Import to run the command and read the result from the standard output channel (stdout). In this case it is important to tell raspistill to send its result to stdout with '-o -'.
POSTED BY: Arnoud Buzing
Yes,
image = Import[ "!raspistill -n -w 600 -h 400 -t 0 -o -", "JPG"];
is excelant way to do it, but on my RPI option -t 0 freezes in shel and in Mathematica. So use time > 0 to avoid stupid problems:
image = Import[ "!raspistill -n -w 600 -h 400 -t 10 -o -", "JPG"];
POSTED BY: Zoran Grujic
Are you doing this in a Mathematica notebook on the pi?  What is the CPU Usage Monitor showing during the evaluation?
POSTED BY: Lambert Chao
Did you start Wolfram/Mathematica as sudo?
POSTED BY: BoB LeSuer
Posted 11 years ago
Still not working as root. As before, the red LED on the cam lights up but the evaluation doesn't stop...
POSTED BY: Robin Gruna
Posted 11 years ago
Unfortunately, I can't grab images this way. Grabbing images with the application
raspistill
works without any problems. However, evaluating
DeviceRead["RaspiCam"]
in MMA does evaluate forever and needs to be aborted after a few minutes. Any ideas? Greetings, R.
POSTED BY: Robin Gruna
This is not currently supported, but it is functionality we'll add.  Thanks for your suggestion.
POSTED BY: Lambert Chao
Posted 11 years ago
There is no exif metainformation in this image. Is it possible to have DeviceRead[] return the exif data just like the Raspicam tools do normally?

image = DeviceRead["RaspiCam"];
meta = OptionValue[OptionValue[Options, MetaInformation]];
retrurns only this error:
OptionValue::optnf: Option name MetaInformation not found in defaults for {ColorSpace -> RGB, Interleaving -> True}.

thanks.
POSTED BY: Glen Schler
I was trying
DeviceRead["RaspiCam"]
, but found that the pictures were underexposed. I couldn't figure out how to apply camera settings or parameters from within Mathematica so I now use a workaround:
Run["raspistill -t 1000 -w 600 -h 400 -rot 180 -o junk2.jpg"]
Import["junk2.jpg"]
As you can see, this way I can apply all the options of raspistill
POSTED BY: Kay Herbert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract