Guest User

code to control G12 for timelapse photography using ptp chdk

a guest
Sep 29th, 2011
408
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime
  2. import os
  3. import subprocess
  4. import sys
  5. import time
  6. import glob
  7. import re
  8.  
  9. frame_num_regex = re.compile(r'IMG_(\d+)\.')
  10. jpeg_frame_numbers = list(int(frame_num_regex.search(f).group(1)) for f in glob.glob('images/jpeg/IMG_*.*'))
  11. raw_frame_numbers = list(int(frame_num_regex.search(f).group(1)) for f in glob.glob('images/raw/IMG_*.*'))
  12. jpeg_frame_num = 1
  13. raw_frame_num = 1
  14. if jpeg_frame_numbers:
  15.     jpeg_frame_num = max(jpeg_frame_numbers) + 1
  16. if raw_frame_numbers:
  17.     raw_frame_num = max(raw_frame_numbers) + 1
  18. jpeg_info_file = open('images/jpeg/info.txt', 'a')
  19. raw_info_file = open('images/raw/info.txt', 'a')
  20.  
  21. def get_new_data(file):
  22.     last_file_pointer = 0
  23.     while True:
  24.         file.seek(last_file_pointer)
  25.         data = []
  26.         for line in file:
  27.             data.append(line)
  28.         yield ''.join(data)
  29.         last_file_pointer = file.tell()
  30.  
  31. proc = subprocess.Popen('chdkptp.exe -i -c', stdin=subprocess.PIPE)
  32. stdout_file = open('stdout.txt', 'r')
  33. stdout = get_new_data(stdout_file)
  34. time.sleep(1)
  35. print stdout.next()
  36.  
  37. def run_command(command, delay):
  38.     print command
  39.     proc.stdin.write('%s\n' % command)
  40.     proc.stdin.flush()
  41.     time.sleep(delay)
  42.     result = stdout.next()
  43.     print result
  44.     return result
  45.  
  46. print run_command('reboot\n', 10)
  47. print run_command('connect\n', 2)
  48. print run_command('luar switch_mode_usb(1);\n', 10)
  49.  
  50. # Camera Dials
  51. #   Mode Dial to Manual
  52. #   EV offset to 0
  53. #   ISO to 80
  54. # Camera Settings
  55. #   Mute On
  56. #   Power Save Off
  57. # Settings Menu
  58. #   Focus Bracketing Off
  59. #   Multiple Shots Off
  60. print run_command('luar ' +
  61.     # menu update script
  62.     'click("menu"); sleep(2000); ' + # open up the menu
  63.     # shooting settings
  64.     'click("down"); sleep(500); ' + # skip over af frame
  65.     'if get_prop(299) == 0 then click("down"); sleep(500); end; ' + # skip over af-point zoom
  66.     'click("down"); sleep(500); if get_prop(299)==1 then click("right"); sleep(500); end; ' + # set servo af off 0
  67.     'click("down"); sleep(500); if get_prop(12)==1 then click("right"); sleep(500); end; ' + # set continuous af off 0
  68.     'click("down"); sleep(500); if get_prop(5)==1 then click("right"); sleep(500); end; ' + # set af-assist beam off 0
  69.     'click("down"); sleep(500); ' + # skip over mf-point zoom
  70.     'click("down"); sleep(500); if get_prop(283)==1 then click("right"); sleep(500); end; ' + # set safety mf off 0
  71.     'click("down"); sleep(500); click("right"); ' + # enter flash control menu
  72.     'while get_prop(141) ~= 2 do click("right"); sleep(500); end; ' + # sets flash output to max 2
  73.     'click("down"); sleep(500); if get_prop(64)==1 then click("right"); sleep(500); end; ' + # set shutter sync to first curtain 0
  74.     'click("down"); sleep(500); if get_prop(217)==1 then click("right"); sleep(500); end; ' + # set red-eye lamp to off 0
  75.     'click("menu"); sleep(500); ' + # return from flash control menu
  76.     'click("down"); sleep(500); ' + # skip spot ae point
  77.     'click("down"); sleep(500); ' + # skip review
  78.     'click("down"); sleep(500); ' + # skip review-info
  79.     'click("down"); sleep(500); if get_prop(310)==1 then click("right"); sleep(500); end; ' + # set blink detection to off 0
  80.     'click("down"); sleep(500); ' + # skip custom display
  81.     'click("down"); sleep(500); ' + # skip reverse display no propcase
  82.     'click("down"); sleep(500); while get_prop(145) ~= 4 do click("right"); sleep(500); end; ' + # sets image stabalization mode to off 4 (continuous, ?, shoot only, panning, off)
  83.     'click("down"); sleep(500); while get_prop(60) ~= 0 do click("right"); sleep(500); end; ' + # sets converter to none 0
  84.     'click("down"); sleep(500); ' + # skip set func display
  85.     'click("down"); sleep(500); ' + # skip set shortcut button display
  86.     'click("down"); sleep(500); ' + # skip save settings display
  87.     'click("down"); sleep(500); ' + # return to top of menu
  88.     # camera settings
  89.     'click("right"); sleep(500); ' + # enter camera settings
  90.     'click("down"); sleep(500); click("right"); sleep(500); ' + # can't set mute to on 1 (as it defaults to off)
  91.     # end menu update script
  92.     'click("menu"); sleep(2000); ' + # close the menu
  93.     # settings update script
  94.     'click("set"); sleep(2000); ' + # open up the settings
  95.     'while get_prop(273) ~= 888 do click("right"); sleep(500); end; ' + # set white balance to flash 888
  96.     'click("down"); sleep(500); ' + # can't set focus bracket off no propcase
  97.     'click("down"); sleep(500); ' + # can't set to single shot mode no propcase
  98.     'click("down"); sleep(500); while get_prop(141) ~= 2 do click("right"); sleep(500); end; ' + # set flash output to max 2
  99.     'click("down"); sleep(500); if get_prop(196) == 1 then click("right"); sleep(500); end; ' + # set nd filter to off 0
  100.     'if get_prop(214) ~= 0 then click("down"); sleep(500); end; ' + # skip aspect ratio if we are in raw and jpeg so we can goto set the raw and jpeg setting then we'll come back
  101.     'click("down"); sleep(500); while get_prop(214) ~= 2 do click("right"); sleep(1000); end; ' + # set raw and jpeg to both on 2 (raw, jpeg, raw and jpeg)
  102.     'click("up"); sleep(500); while get_prop(300) ~= 0 do click("right"); sleep(500); end; ' + # set aspect ratio to 4:3 0 (4:3, 16:9, 3:2, 1:1, 4:5)
  103.     'click("down"); sleep(500); click("down"); sleep(500); while get_prop(222) ~= 0 do click("right"); sleep(500); end; ' + # set file size to L 0 (L, M1, M2, ?, S)
  104.     'click("down"); sleep(500); ' + # return to top of menu
  105.     # end settings menu update script
  106.     'click("set"); sleep(2000); ' + # close up the settings
  107.     # general stuff
  108.     'while get_prop(143) ~= 1 do click("right"); sleep(3000); end; ' + # sets flash mode to on 1
  109.     'if get_prop(6) == 1 then click("left") sleep(3000); end; ' + # sets macro mode to off 0
  110.     'if get_prop(133) == 0 then click("up") sleep(3000); end; ' + # sets manual focus mode to off 0
  111.     'while get_prop(227) ~= 0 do click("down"); sleep(3000) end; ' + # sets timer mode to off 0
  112.     'set_zoom(7); sleep(5000); ' + # zoom to proper depth 7
  113.     'sleep(1000); set_backlight(0);\n', 80)
  114.  
  115. while True:
  116.     folders_before = set(run_command('ls DCIM', 1).splitlines()[1:-1])
  117.     current_folder_before = max(folder[:-1] for folder in folders_before if folder != 'CANONMSC/')
  118.     files_before = set(run_command('ls DCIM/%s' % current_folder_before, 1).splitlines()[1:-1])
  119.     photo_datetime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  120.     run_command('luar ' +
  121.         'set_av96_direct(472); ' + # set the av to 96*log2(a^2) 2.8=>285 OR 8.0=>576 OR 5.5=>472 OR 6.5=>519 OR 6.2=>505 OR 6.0=>496
  122.         'set_tv96_direct(1053); ' + # set the exposure to 1/2000=>1053 by 96*log2(1/t) OR 1/1000=>957 OR 1/200=>734 OR 1/50=>542
  123.         'sleep(500); ' + # a little pause for good measure
  124.         'press("shoot_half"); sleep(2000); set_focus(1000); sleep(4000); ' + # set manual focus mode and focus to 1000 mm
  125.         'click("shoot_full"); sleep(4000); release("shoot_half"); sleep(2000); set_backlight(0);', 30)
  126.     folders_after = set(run_command('ls DCIM', 1).splitlines()[1:-1])
  127.     current_folder_after = max(folder[:-1] for folder in folders_after if folder != 'CANONMSC/')
  128.     if current_folder_before != current_folder_after:
  129.         files_before = set()
  130.     files_after = set(run_command('ls DCIM/%s' % current_folder_after, 1).splitlines()[1:-1])
  131.     for new_file in files_after.difference(files_before):
  132.         if new_file.endswith('.JPG'):
  133.             run_command('download A/DCIM/104___09/%s images/jpeg/IMG_%06i.%s' % (new_file, jpeg_frame_num, new_file[-3:]), 5)
  134.             jpeg_info_file.write('%06i\t%s\n' % (jpeg_frame_num, photo_datetime))
  135.             jpeg_info_file.flush()
  136.             jpeg_frame_num += 1
  137.         elif new_file.endswith('.CR2'):
  138.             run_command('download A/DCIM/104___09/%s images/raw/IMG_%06i.%s' % (new_file, raw_frame_num, new_file[-3:]), 20)
  139.             raw_info_file.write('%06i\t%s\n' % (raw_frame_num, photo_datetime))
  140.             raw_info_file.flush()
  141.             raw_frame_num += 1
  142.         run_command('luar os.remove("A/DCIM/104___09/%s");' % new_file, 2)
  143.     time.sleep(300)
  144.  
RAW Paste Data