Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Timelapse code needs python help  (Read 1286 times)

PTTG??

  • Bay Watcher
  • Kringrus! Babak crulurg tingra!
    • View Profile
    • http://www.nowherepublishing.com
Timelapse code needs python help
« on: January 30, 2011, 05:58:57 pm »

I need to improve this:

Code: [Select]
from VideoCapture import Device
import time

i = 0
cam = Device()

while 1:
print "Taking picture #" + str(i)
cam.saveSnapshot('images/image' + str(i) + '.jpg', timestamp=3, boldfont=1)
i += 1
time.sleep(10)

What I want to do is to make it only take pictures every other hour for a year. I know that all I need to do is change "while true:" to "while it is now an even number of hours since the new year, and I haven't taken a picture yet this hour:".
However, I don't speak python; I just borrowed this from Tahin. Anyone out there, including Tahin, know how to make this work?
Logged
A thousand million pool balls made from precious metals, covered in beef stock.

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Timelapse code needs python help
« Reply #1 on: January 30, 2011, 06:02:42 pm »

One thing is that this loop would never end unless you stopped the program, since 1 is always true.

Also, I'll have to research which unit the time.sleep function uses.

Ok, it's seconds. So you'll want to put a 3600 in there, since there are 3600 secs in an hour.
« Last Edit: January 30, 2011, 06:05:35 pm by Darvi »
Logged

Sir Pseudonymous

  • Bay Watcher
    • View Profile
Re: Timelapse code needs python help
« Reply #2 on: January 30, 2011, 06:15:26 pm »

Simplest: set it up with a "while not done:" loop, have it sleep for two hours at a time (inside that loop), and set done to true after it's run a number of times equal to half the number of hours in a year? Don't know how well it would work, I'm not sure how stable that would be, nor can I recall how python's sleep function works. Perhaps have it write the time and number of iterations to a file, so if it crashes it can be turned back on and pick up where it left off? What do you need it for?

Something like:
Code: [Select]
from time import sleep
SLEEPINTERVAL = 7200 #is sleep seconds or milliseconds? [s]I forget... I think it's milliseconds though[/s] never mind, it's seconds; this is also why you always put constants in their own variable, instead of in the code itself
TOTALITERATIONS = 4380 #or however many you want, that's half the hours in a (non-leap) year there
done = False
iterations = 0
while not done:
  functionthatdoeswhatyouwantdone()
  sleep(SLEEPINTERVAL)
  if iterations >= TOTALITERATIONS: done = True
  iterations += 1
« Last Edit: January 30, 2011, 06:20:42 pm by Sir Pseudonymous »
Logged
I'm all for eating the heart of your enemies to gain their courage though.

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Timelapse code needs python help
« Reply #3 on: January 30, 2011, 06:19:03 pm »

Sleep's seconds.

So:
Code: [Select]
from time import sleep
from VideoCapture import Device
TOTALITERATIONS = 8760
done = False
iterations = 0
while not done:
  print "Taking picture #" + str(i)
  cam.saveSnapshot('images/image' + str(i) + '.jpg', timestamp=3, boldfont=1)
  sleep(3600)
  if iterations >= TOTALITERATIONS: done = True
  iterations += 1
I guess?
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Timelapse code needs python help
« Reply #4 on: January 30, 2011, 06:19:10 pm »

I'm from the C++ world so I might suggest something more complicated than python requires. It's also late, I'm tired and just making this up as I go along.

I would suggest the following however:

Let the loop run every 60 seconds.
Keep a flag indicating if a snapshot has been taken the last hour
Use time.gmtime() to check if minutes past the hour passes 0, every time it does set the flag above to false
Check flag, if false take snapshot.

Other things:
Keep track of how many snapshots have been taken and at what hour. Store this in a file.
Use that so the program can resume at the correct index even if it has to be restarted.
« Last Edit: January 30, 2011, 06:21:28 pm by olemars »
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Timelapse code needs python help
« Reply #5 on: January 30, 2011, 06:21:04 pm »

I don't know why the program just can't loop every hour instead of every minute, but the backup code might be is a good idea.
Logged

PTTG??

  • Bay Watcher
  • Kringrus! Babak crulurg tingra!
    • View Profile
    • http://www.nowherepublishing.com
Re: Timelapse code needs python help
« Reply #6 on: January 30, 2011, 06:22:42 pm »

One issue is that this is a rural area and there is a chance of power failure. It's on a laptop with a good battery, but there's still a chance of a power failure it can't withstand.

Thus, I want it to pick up where it left off, including the numbering of the output files.

Once a minute is best, since if it for some reason misses a minute, it'll just get it next minute.
Logged
A thousand million pool balls made from precious metals, covered in beef stock.

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Timelapse code needs python help
« Reply #7 on: January 30, 2011, 06:25:27 pm »

Once a minute is best, since if it for some reason misses a minute, it'll just get it next minute.
Ah, k.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Timelapse code needs python help
« Reply #8 on: January 30, 2011, 06:27:04 pm »

I don't know why the program just can't loop every hour instead of every minute, but the backup code might be is a good idea.

Mostly for synchronization and consistency. This way every snapshot will be taken roughly the same time on the hour. If you just let it sleep for an hour you're bound to get skewing and unexpected gaps.
Logged

Derekristow

  • Bay Watcher
    • View Profile
    • Steam ID
Re: Timelapse code needs python help
« Reply #9 on: January 30, 2011, 06:32:07 pm »

One issue is that this is a rural area and there is a chance of power failure. It's on a laptop with a good battery, but there's still a chance of a power failure it can't withstand.

Thus, I want it to pick up where it left off, including the numbering of the output files.
Other things:
Keep track of how many snapshots have been taken and at what hour. Store this in a file.
Use that so the program can resume at the correct index even if it has to be restarted.

Then you will definitely want to do this.  I don't know how to read/write to a file in Python, unfortunately, but you will want to keep i in a file somewhere.  Is it absolutely critical that it take a picture every two hours, or can it skip a few if something goes wrong like it losing power?
Logged
So my crundles are staying intact unless they're newly spawned... until they are exposed to anything that isn't at room temperature.  This mostly seems to mean blood, specifically, their own.  Then they go poof very quickly.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Timelapse code needs python help
« Reply #10 on: January 30, 2011, 06:46:02 pm »

The most robust way would probably be to retrieve the time from the OS and compare that to the previous time (which you store in a file on the hard disk) to determine if the program should take another snapshot. That way you're sure to have the most recent time (assuming you're synchronizing the clock regularly). Another problem I foresee is that although the program may be able to run for such a long time, if you're using windows there's the chance you'll run into stability issues if you leave the laptop running for a few months, let alone a whole year.
Logged

PTTG??

  • Bay Watcher
  • Kringrus! Babak crulurg tingra!
    • View Profile
    • http://www.nowherepublishing.com
Re: Timelapse code needs python help
« Reply #11 on: January 30, 2011, 06:54:16 pm »

One issue is that this is a rural area and there is a chance of power failure. It's on a laptop with a good battery, but there's still a chance of a power failure it can't withstand.

Thus, I want it to pick up where it left off, including the numbering of the output files.
Other things:
Keep track of how many snapshots have been taken and at what hour. Store this in a file.
Use that so the program can resume at the correct index even if it has to be restarted.

Then you will definitely want to do this.  I don't know how to read/write to a file in Python, unfortunately, but you will want to keep i in a file somewhere.  Is it absolutely critical that it take a picture every two hours, or can it skip a few if something goes wrong like it losing power?

I can duplicate frames, especially during night, I just want to avoid that when possible.
As Virex said, it would be nice to be able to reboot if it runs into trouble.
Logged
A thousand million pool balls made from precious metals, covered in beef stock.