# Need Help with RunScheduleTask to automate a camera snapshots

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], AbsoluteTime[]}], {1, 10}]
snapshots


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.

• 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. – C. E. Apr 25 '17 at 15:44
• Thanks for pointing this out to me. I want 10 shots taken every 3 seconds apart. – Jose Enrique Calderon Apr 25 '17 at 16:42

Solution #1:

Clear[photos]
camera = DeviceOpen["Camera"];

photos = {takePhoto[]};
Dynamic[photos]

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


Solution #2

takePhotos[interval_, n_] := Module[{photos},
takePhoto[dev_] := {DeviceRead[dev], DateList[]};
camera = DeviceOpen["Camera"];
photos =
Join[{takePhoto[]},
Table[(Pause[interval]; takePhoto[camera]), n - 1]];
DeviceClose["Camera"];
photos]
takePhotos[3, 10]

• 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. – Jose Enrique Calderon Apr 25 '17 at 16:49
• I have adjusted the interval. – 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

RemoveScheduledTask@task;


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)

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