giovedì 29 dicembre 2011

Time Lapse con Android

un esempio di automatizzazione di Android (o meglio della fotocamera inserita nel telefono)

le foto sono state scattate al ritmo di una al minuto per circa due ore e successivamente rimontate in un filmato


lo script che genera la sequenza e'
----------------------------------------------------------------------

import android, time, os


droid = android.Android()


def titleAlert():
  title = 'Android Python Timelapse'
  message = 'Welcome to Android Python Timelapse.  In the following dialogs, you will be asked to choose your interval and the location where you would like your pictures saved.  Enjoy! Questions or comments? Please send to Support@SubmergedSpaceman.com'
  droid.dialogCreateAlert(title, message)
  droid.dialogSetPositiveButtonText('Continue')
  droid.dialogShow()
  response = droid.dialogGetResponse().result
  return response['which'] == 'positive'  


def chooseInterval():
  title = 'Please choose your picture interval in minutes.  To use a different value, choose Custom'
  droid.dialogCreateAlert(title)
  droid.dialogSetItems(['1', '5', '10','30','60','Custom'])
  droid.dialogShow()
  response = droid.dialogGetResponse().result['item']


  if response == 0:
    timer = 1
  elif response == 1:   
    timer = 5
  elif response == 2:   
    timer = 10
  elif response == 3:   
    timer = 30
  elif response == 4:   
    timer = 60
  elif response == 5:   
    timer = droid.dialogGetInput("please enter your interval in minutes").result
    timer = float(timer)
  print "interval is: "+str(timer) +" minutes"
  return timer


def chooseFolder():
  title = 'Please choose where you would like to store your pictures'
  captureTime = time.strftime("%Y-%m-%d--%H-%M-%S", time.localtime())
  droid.dialogCreateAlert(title)
  droid.dialogSetItems(['/sdcard/YYYY-MM-DD', '/sdcard/timelapse', 'Custom'])
  droid.dialogShow()
  response = droid.dialogGetResponse().result['item']


  if response == 0:
    folderStr = captureTime
  elif response == 1:   
    folderStr = "timelapse"
  elif response == 2:   
    folderStr = droid.dialogGetInput("please enter the name of the folder you would like to create:   /sdcard/").result
  
  print "folder set to: /sdcard/"+folderStr
  return folderStr


def takePics(picfolder="timelapse", lapse=30):
  loopvar = 'true'


  while loopvar == 'true':
    captureTime = time.strftime("%Y-%m-%d--%H-%M-%S", time.localtime())
    captureLocation = '/mnt/sdcard/'+picFolder+"/"+str(captureTime)+'.jpg'
    print "picture taken: "+captureLocation
    droid.cameraCapturePicture(captureLocation,True)
    interval = lapse * 60 
    time.sleep(interval)


titleAlert()
lapse = chooseInterval()
picFolder = chooseFolder()
#os.system("mkdir /mnt/sdcard/"+picFolder)
os.mkdir('mkdir /sdcard/1/')
#takePics(picFolder,lapse)