I need help with this application. I want to automate a Device camera to capture images to a List using ScheduledTask.

I know there must be other ways to automate a task in Mathematica, but this is the one that I consider will provide control of each image capture with other environmental variables surrounding the picture at each instance. In this code, I want to capture the AbsoluteTime.

Here is my work. I am trying to capture 10 images every 3 seconds.THe image is to be added to a list

 snapshots = {};
dev = DeviceOpen["Camera"]
AppendTo[snapshots, DeviceRead[dev]]
 AppendTo[snapshots, {DeviceRead[dev], AbsoluteTime[]}], {1, 10}]

The code is picking up a first image, but is not adding the other 9. Another issue is that the camera is not turning off after executing the RunScheduleTask. I used DeviceClose[] but this ends the program in the first image capture.

  • $\begingroup$ Ten images every three seconds, or one image every three seconds ten times? Your code seems to say that you want to capture one image every second, ten times. $\endgroup$ – C. E. Apr 25 '17 at 15:44
  • $\begingroup$ Thanks for pointing this out to me. I want 10 shots taken every 3 seconds apart. $\endgroup$ – Jose Enrique Calderon Apr 25 '17 at 16:42

Solution #1:

camera = DeviceOpen["Camera"];
takePhoto[] := DeviceRead[camera]

photos = {takePhoto[]};

RunScheduledTask[Set[photos, Join[photos, {takePhoto[]}]], 3];

Solution #2

takePhotos[interval_, n_] := Module[{photos},
  takePhoto[dev_] := {DeviceRead[dev], DateList[]};
  camera = DeviceOpen["Camera"];
  photos = 
    Table[(Pause[interval]; takePhoto[camera]), n - 1]];
takePhotos[3, 10]
| improve this answer | |
  • $\begingroup$ Thank you but is not quite doing what I need. I need 10 images taken 3 seconds apart. Also this code is dropping an error. $\endgroup$ – Jose Enrique Calderon Apr 25 '17 at 16:49
  • $\begingroup$ I have adjusted the interval. $\endgroup$ – UnchartedWorks Apr 25 '17 at 17:05

You can take a bunch of shots with DeviceReadList. Fore more efficient appending, I use an Association and have each snap associated with the time when it was taken.

ClearAll[camera, photos, snaps, task];

camera = DeviceOpen["Camera"];

photos = <||>;

snaps[n_Integer?NonNegative] := Association[
   Table[DateObject[] -> DeviceRead[camera, "Camera"], n]];

task = RunScheduledTask[photos = Join[photos, snaps[10]], 3];

I didn't attempt to have photos updated dynamically, which seems like it wouldn't be necessary for many use cases. When you get sick of taking pics, you can end it with


You can retrieve the pics from photos directly using Values@photos. Each photo will have a time stamp (DateObject) which you can examine programmatically using (DateValue)

| improve this answer | |
  • $\begingroup$ This looks good. But I really need to have the time attached to each image. I need 10 images taken 3 sec apart. $\endgroup$ – Jose Enrique Calderon Apr 25 '17 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.